Latest 1.0.1
Homepage https://github.com/davidlivadaru/DLInterval
License MIT
Platforms ios 10.0, osx 10.11, watchos 3.0, tvos 10.0

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

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 that creating an interval with a closed boundary using infinity will fail.

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

Intersections

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.

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.11 and later.
  • Ubuntu check official site which provides support for 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.

You must to import the module using:

import DLInterval_iOS
import DLInterval_macOS
import DLInterval_watchOS
import DLInterval_tvOS

2. CocoaPods

Add the dependency in your Podfile.

pod 'DLInterval'

You must to import the module using:

import 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"])),
    ]

You must to import the module using:

import DLInterval

Contribution

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.

License

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

Latest podspec

{
    "name": "DLInterval",
    "version": "1.0.1",
    "summary": "A Swift module which provides an abstractions of mathematical intervals.",
    "homepage": "https://github.com/davidlivadaru/DLInterval",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "David Livadaru",
    "platforms": {
        "ios": "10.0",
        "osx": "10.11",
        "watchos": "3.0",
        "tvos": "10.0"
    },
    "source": {
        "git": "https://github.com/davidlivadaru/DLInterval.git",
        "tag": "1.0.1"
    },
    "source_files": [
        "DLInterval",
        "Sources/**/*.{h,m,swift}"
    ],
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This