Select Page
Latest 1.1.2 https://github.com/davidlivadaru/DLInterval MIT tvos 10.0

This Swift module aims to provide a solution to easily create mathematical intervals.

## Usage

### Creation

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 open
let 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: Creating an interval with a closed boundary using infinity is ill-formed.

### Check

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

### Unions

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: union is a new data type called UnionInterval.

### Intersections

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: Interval‘s intersection returns an Interval? and UnionInterval‘s returns UnionInterval.

### Clipping values

Available from v1.1.1.

An interval can clip a value within its boundaries:

let interval = [0..1]
let newValue = interval.clipValue(-0.5) // 0.0

Note: Open boundaries returns the closest value to boundary:

let interval = (-1..1)
let newValue = interval.clipValue(2.0) // 0.99999999

## Installation

Module requires Swift 4.0.

OS requirements:

• iOS 10.0 and later.
• watchOS 3.0 and later.
• tvOS 10.0 and later.
• macOS 10.12 and later.
• Ubuntu – check official site to find which versions support Swift 4.0.

### 1. Carthage

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 import the module using:

import DLInterval_iOS
import DLInterval_macOS
import DLInterval_watchOS
import DLInterval_tvOS

### 2. CocoaPods

pod 'DLInterval_[iOS|macOS|watchOS|tvOS]' // use the OS the dependency refers to

You must import the module using:

import DLInterval_iOS
import DLInterval_macOS
import DLInterval_watchOS
import DLInterval_tvOS

### 3. Swift Package Manager

dependencies: [
]

targets: [
.target(
name: "YourTargetName",
dependencies: ["DLInterval"])),
]

You must import the module using:

import DLInterval

## Contribution

Module is covered by unit tests, however, bugs always slip through.
If you find a bug in the module create an issue.

If you want to contribute on fixing bugs or implementing new features then create a pull request.

### Latest podspec

{
"name": "DLInterval_tvOS",
"version": "1.1.2",
"summary": "A Swift module for tvOS which provides an abstractions of mathematical intervals.",
"type": "MIT",
},
"platforms": {
"tvos": "10.0"
},
"source": {