DLInterval_macOS

A Swift module for macOS which provides an abstractions of mathematical intervals.


Keywords
carthage, cocoapods, cross-platform, interval, math, swift-package-manager, swift4
License
MIT
Install
pod try DLInterval_macOS

Documentation

DLInterval Logo

Carthage compatible CocoaPods compatible Swift Package Manager Swift 4.0

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

Table of contents

Usage

Importing the module

You can import this module using:

import DLInterval

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 = Interval([0..1])
let newValue = interval.clipValue(-0.5) // 0.0

Note: Open boundaries returns the closest value to boundary:

let interval = 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.

Choose your preferred dependency manager:

1. Carthage

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.

2. CocoaPods

Add the dependency in your Podfile.

pod 'DLInterval'

3. Swift Package Manager

Add the the following dependecy in your Package.swift:

dependencies: [
    .package(url: "https://github.com/davidlivadaru/DLInterval.git", .upToNextMajor(from: "1.0.0"))
]

and update your target's dependencies:

targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["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.

License

DLInterval is released under MIT license. See LICENSE for details.