From 1446657bd1c2bdccae3d6fcb5440a38a700ee2d0 Mon Sep 17 00:00:00 2001 From: AmitaiB Date: Mon, 18 Feb 2019 13:33:50 -0500 Subject: [PATCH 1/3] Ports ReadMe examples to HeaderDocs --- .../Source/OptionalExtensions.swift | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/OptionalExtensions/Source/OptionalExtensions.swift b/OptionalExtensions/Source/OptionalExtensions.swift index 4dc469d..9be80d5 100644 --- a/OptionalExtensions/Source/OptionalExtensions.swift +++ b/OptionalExtensions/Source/OptionalExtensions.swift @@ -8,40 +8,107 @@ 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) */ 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(_ 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 } From 82b603cce45a07a224ae251f5d4726752d4aa273 Mon Sep 17 00:00:00 2001 From: AmitaiB Date: Mon, 18 Feb 2019 13:36:11 -0500 Subject: [PATCH 2/3] Adds compactMapNil for iOS 9.3+, mimics Foundation [flatMap](https://developer.apple.com/documentation/swift/sequence/2907182-flatmap#) deprecated in 9.0 [compactMap](https://developer.apple.com/documentation/swift/anybidirectionalcollection/2957784-compactmap#) introduced in iOS 9.3 --- OptionalExtensions/Source/OptionalExtensions.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/OptionalExtensions/Source/OptionalExtensions.swift b/OptionalExtensions/Source/OptionalExtensions.swift index 9be80d5..de1c154 100644 --- a/OptionalExtensions/Source/OptionalExtensions.swift +++ b/OptionalExtensions/Source/OptionalExtensions.swift @@ -32,6 +32,13 @@ public extension Optional { let nilledNumber: Int? = nil nilledNumber.flatMapNil { .Some(2) } // .Some(2) */ + @available(iOS 9.3, *) + func compactMapNil(_ predicate: () -> Optional) -> Optional { + return flatMapNil(predicate) + } + + /// Meet the old boss, same as the new boss. + @available(iOS, deprecated: 9.0) func flatMapNil(_ predicate: () -> Optional) -> Optional { return self ?? predicate() } From be210514f0de7f6cfd8bdb52cf200d9091866c9b Mon Sep 17 00:00:00 2001 From: AmitaiB Date: Mon, 18 Feb 2019 13:47:52 -0500 Subject: [PATCH 3/3] Removes use of deprecated method --- OptionalExtensions/Source/OptionalExtensions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OptionalExtensions/Source/OptionalExtensions.swift b/OptionalExtensions/Source/OptionalExtensions.swift index de1c154..870ab2e 100644 --- a/OptionalExtensions/Source/OptionalExtensions.swift +++ b/OptionalExtensions/Source/OptionalExtensions.swift @@ -34,7 +34,7 @@ public extension Optional { nilledNumber.flatMapNil { .Some(2) } // .Some(2) */ @available(iOS 9.3, *) func compactMapNil(_ predicate: () -> Optional) -> Optional { - return flatMapNil(predicate) + return self ?? predicate() } /// Meet the old boss, same as the new boss.