TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2018 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✓ |
Maintained by David Livadaru.
This Swift module aims to provide a solution to easily create mathematical intervals.
Interval
has constructors to create intervals using the notations we're used to from mathmatics:
[1, 2]
and (1, 2)
let closedClosed = Interval([1..2]) // [1, 2]
let openOpen = Interval((1..2)) // (1, 2)
For convenience, range operators may be used:
let closedClosed: Interval = 1...2 // [1, 2]
let closedOpen: Interval = 1..<2 // [1, 2)
For half open intervals there are some new operators:
.>.
- first boundary is open.<.
- second boundary is open.><.
- both boundaries are openlet closedOpen: Interval = 1.<.2 // [1, 2)
let openClosed: Interval = 1.>.2 // (1, 2]
let openOpen: Interval = 1.><.2 // (1, 2)
To create intervals with infinity as boundaries:
let negativeInfinity: Interval = -Double.infinity.>.0 // (-inf, 0]
let positiveInfinity: Interval = 0.><.Double.Infinity // (0, +inf)
Note that creating an interval with a closed boundary using infinity will fail.
You may check if an interval contains a double value:
let closedOpen: Interval = 1.<.2
closedOpen.contains(1) // true
closedOpen.contains(2) // false
closedOpen.contains(1.1) // true
Checking infinity values:
let closedOpen: Interval = 1.<.2
closedOpen.contains(Double.infinity) // false
closedOpen.contains(-Double.infinity) // false
let positiveInfinity: Interval = 0.><.Double.Infinity
positiveInfinity.contains(Double.infinity) // true
positiveInfinity.contains(-Double.infinity) // false
Creating a union from 2 intervals:
let firstInterval: Interval = -Double.infinity.>.0 // (-inf, 0]
let secondInterval: Interval = 0.><.1 // (0, 1)
let union = firstInterval.formUnion(secondInterval) // (-inf, 1)
Note that union
is a new data type called UnionInterval
.
To find intersection of 2 intervals:
let firstInterval: Interval = -Double.infinity.><.1 // (-inf, 1)
let secondInterval: Interval = -1.><.5.0 // (-1, 5)
let intersection = firstInterval.intersection(with: secondInterval) // (-1, 1)
Note that Interval
's intersection returns an Interval?
and UnionInterval
's returns UnionInterval
.
Module requires Swift 4.0.
OS requirements:
Choose your preferred dependency manager:
Add the dependency in your Cartfile
.
github "davidlivadaru/DLInterval"
If you need the framework only for a single OS, then I propose to use --platform [iOS|macOS|watchOS|tvOS]
specifier when your perform carthage update
.
You must to import the module using:
import DLInterval_iOS
import DLInterval_macOS
import DLInterval_watchOS
import DLInterval_tvOS
Add the dependency in your Podfile
.
pod 'DLInterval'
You must to import the module using:
import DLInterval
Add the the following dependecy in your Package.swift
:
dependencies: [
.package(url: "https://github.com/davidlivadaru/DLInterval.git", .upToNextMinor(from: "1.0.0"))
]
and update your target's dependencies:
targets: [
.target(
name: "YourTargetName",
dependencies: ["DLInterval"])),
]
You must to import the module using:
import DLInterval
Module is covered by units, however, bugs always slip through.
If you find a bug in the module create an issue.
If you want contribute on fixing bugs or implementing new features then create a pull request.
DLInterval is released under MIT license. See LICENSE for details.