Latest 0.1.1
Homepage https://github.com/marciok/OptionalExtras
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0
Authors

CI Status
Version
License
Platform

Handful of functions that will help you work with Optional types. 100% inspired by std::option – Rust

Examples

expect(_ desrcription: String) -> Wrapped

Unwraps an Optional, yielding the content of a .some. Or throws fatalError with given description

let x: String? = nil
x.expect("the world is ending")

unwrapOr(_ def: Wrapped) -> Wrapped

Returns the contained value or a default.

let x: Int? = nil
let r = x.unwrapOr(20) // r == 20

unwrapOrElse(_ f: () -> Wrapped) -> Wrapped

Returns the contained value or computes it from a closure.

let x: Int? = nil
let r = x.unwrapOrElse { 2 * 2 } // r == 4

isNone() -> Bool

Returns true if the Optional is nil

let x: String? = nil
let r = x.isNone() // r == true

isSome() -> Bool

Returns true if the Optional is .some

let x: String? = "foo"
let r = x.isSome() // r == true

mapOr( def: T, f: (Wrapped) -> T) -> T

Applies a function to the contained value (if any), or returns a default (if not).

let x: String? = "foo"
let r = x.mapOr(42) { (v: String) -> Int in
   v.characters.count
} // r == 3

mapOrElse( def: () -> T, f: (Wrapped) -> T) -> T

Applies a function to the contained value (if any), or computes a default (if not).

let x: String? = nil
let r = x.mapOrElse({
    3 * 3
},
{ (v: String) -> Int in
    v.characters.count
}) // r == 9

and(_ optb: Optional) -> Optional

Returns .none if the Optional is nil, otherwise returns optb.

let x: Int? = 10
let y: Int? = nil

let r = x.and(y) // r == nil

or(_ optb: Optional) -> Optional

Returns nil if the Optional is .none, otherwise returns optb.

let x: Int? = 18
let y: Int? = 10
let r = x.or(y) // r == 18

orElse(_ f: () -> Optional) -> Optional

Returns the Optional if it contains a value, otherwise calls f and returns the result.

let x: String? = nil
let r = x.orElse { "foo" } // r! == "foo"

Installation

OptionalExtras is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "OptionalExtras"

Or through Carthage:

github "marciok/OptionalExtras"

Author

Marcio Klepacz, [email protected]

License

OptionalExtras is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "OptionalExtras",
    "version": "0.1.1",
    "summary": "Useful functions for Swift Optional types",
    "description": "Handful of functions that will help you work with Optional types. 100% inspired by std::option - Rust",
    "homepage": "https://github.com/marciok/OptionalExtras",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Marcio Klepacz": "[email protected]"
    },
    "source": {
        "git": "https://github.com/marciok/OptionalExtras.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source_files": "OptionalExtras/*.swift"
}

Pin It on Pinterest

Share This