Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ReadMe examples as HeaderDocs #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions OptionalExtensions/Source/OptionalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,114 @@

public extension Optional {

/** ```
let number: Int? = 3
let biggerThan2 = number.filter { $0 > 2 } // .Some(3)
let biggerThan3 = number.filter { $0 > 3 } // .None */
func filter(_ predicate: (Wrapped) -> Bool) -> Optional {
return map(predicate) == .some(true) ? self : .none
}

/** ```
let number: Int? = 3
number.mapNil { 2 } // .Some(3)

let nilledNumber: Int? = nil
nilledNumber.mapNil { 2 } // .Some(2) */
func mapNil(_ predicate: () -> Wrapped) -> Optional {
return self ?? .some(predicate())
}

/** ```
let number: Int? = 3
number.flatMapNil { .Some(2) } // .Some(3)

let nilledNumber: Int? = nil
nilledNumber.flatMapNil { .Some(2) } // .Some(2) */
@available(iOS 9.3, *)
func compactMapNil(_ predicate: () -> Optional) -> Optional {
return self ?? predicate()
}

/// Meet the old boss, same as the new boss.
@available(iOS, deprecated: 9.0)
func flatMapNil(_ predicate: () -> Optional) -> Optional {
return self ?? predicate()
}

/**
Similar to `[T]`'s `forEach`.
```
let number: Int? = 3
number.then { print($0) } // prints "3"

let nilledNumber: Int? = nil
nilledNumber.then { print($0) } // print won't be called */
func then(_ f: (Wrapped) -> Void) {
if let wrapped = self { f(wrapped) }
}

/**
Similar to Haskell's `maybe`.

```
let number: Int? = 3
number.maybe(100) { $0 + 1 } // 4

let nilledNumber: Int? = nil
nilledNumber.maybe(100) { $0 + 1 } // 100 */
func maybe<U>(_ defaultValue: U, f: (Wrapped) -> U) -> U {
return map(f) ?? defaultValue
}

/**
Injects a side effect in the `.Some` branch.

```
let number: Int? = 3
let sameNumber = number.onSome { print($0) } // prints "3" & returns .Some(3)

let nilledNumber: Int? = nil
let sameNilledNumber = nilledNumber.onSome { print($0) } // .None */
func onSome(_ f: (Wrapped) -> Void) -> Optional {
then(f)
return self
}

/**
Injects a side effect in the `.None` branch.

```
let number: Int? = 3
let sameNumber = number.onNone { print("Hello World") } // .Some(3)

let nilledNumber: Int? = nil
let sameNilledNumber = nilledNumber.onNone { print("Hello World") } // prints "Hello World" & returns .None */
func onNone(_ f: () -> Void) -> Optional {
if isNone { f() }
return self
}

/**
Plain English sugar for `!= nil`.
```
let number: Int? = 3
let isSome = number.isSome // true

let nilledNumber: Int? = nil
let isSome = nilledNumber.isSome // false */
var isSome: Bool {
return self != nil
}

/**
Plain English sugar for `== nil`.
```
let number: Int? = 3
let isSome = number.isNone // false

let nilledNumber: Int? = nil
let isSome = nilledNumber.isNone // true */
var isNone: Bool {
return !isSome
}
Expand Down