diff --git a/Sources/FunctionalKit/Affine.swift b/Sources/FunctionalKit/Affine.swift index fc471fa..1e3234a 100644 --- a/Sources/FunctionalKit/Affine.swift +++ b/Sources/FunctionalKit/Affine.swift @@ -136,6 +136,38 @@ public extension Array { } } +public extension Affine where S == T, A == B { + static func zip (_ a: Affine, _ b: Affine) -> Affine> where A == Inclusive { + return Affine>.init( + tryGet: { s in + switch (a.tryGet(s), b.tryGet(s)) { + case let (.some(aValue), .some(bValue)): + return .some(.center(aValue, bValue)) + case let (.some(aValue), .none): + return .some(.left(aValue)) + case let (.none, .some(bValue)): + return .some(.right(bValue)) + case (.none, .none): + return .none + } + }, + trySet: { inclusive in + { s in + switch inclusive { + case let .left(value): + return a.trySet(value)(s) + case let .right(value): + return b.trySet(value)(s) + case let .center(aValue, bValue): + return Optional.pure(s) + .flatMap(a.trySet(aValue)) + .flatMap(b.trySet(bValue)) + } + } + }) + } +} + // MARK: - Affine Laws /*: diff --git a/Sources/FunctionalKit/Collections.swift b/Sources/FunctionalKit/Collections.swift index ef24850..aa05e4a 100644 --- a/Sources/FunctionalKit/Collections.swift +++ b/Sources/FunctionalKit/Collections.swift @@ -90,6 +90,19 @@ public extension Sequence where Iterator.Element == Bool { } } +public extension Sequence where Iterator.Element: CoproductType { + func partition() -> Product<[Iterator.Element.LeftType], [Iterator.Element.RightType]> { + + let initial = Product<[Iterator.Element.LeftType], [Iterator.Element.RightType]>.init([], []) + + return self.reduce(initial) { reducer, coproduct in + return coproduct.fold( + onLeft: { left in reducer.mapFirst { $0.appending(left) }}, + onRight: { right in reducer.mapSecond { $0.appending(right) }}) + } + } +} + public extension RandomAccessCollection { func getSafely(at index: Index) -> Iterator.Element? { guard indices.contains(index) else { return nil } diff --git a/Sources/FunctionalKit/Combinators.swift b/Sources/FunctionalKit/Combinators.swift index e7152b5..fe6dd90 100644 --- a/Sources/FunctionalKit/Combinators.swift +++ b/Sources/FunctionalKit/Combinators.swift @@ -35,6 +35,22 @@ public extension f { static func flip(_ function: @escaping (A) throws -> (B) throws -> C) -> (B) throws -> (A) throws -> C { return { b in { a in try function(a)(b) } } } + + static func map (_ transform: @escaping (A) -> B) -> ([A]) -> [B] { + return { $0.map(transform) } + } + + static func map (_ transform: @escaping (A) throws -> B) -> ([A]) throws -> [B] { + return { try $0.map(transform) } + } + + static func filter (_ predicate: @escaping (A) -> Bool) -> ([A]) -> [A] { + return { $0.filter(predicate) } + } + + static func filter (_ predicate: @escaping (A) throws -> Bool) -> ([A]) throws -> [A] { + return { try $0.filter(predicate) } + } } public func <<< (second: @escaping (B) -> C, first: @escaping (A) -> B) -> (A) -> C { diff --git a/Sources/FunctionalKit/Fixed.swift b/Sources/FunctionalKit/Fixed.swift index 4dde52f..bc6ecbd 100644 --- a/Sources/FunctionalKit/Fixed.swift +++ b/Sources/FunctionalKit/Fixed.swift @@ -124,4 +124,16 @@ public extension f { static func asTuple (_ function: @escaping (A,B) -> C) -> ((A,B)) -> C { return { function($0.0,$0.1) } } + + static func embedFirst (_ a: A) -> (B) -> (A,B) { + return { b in + (a,b) + } + } + + static func embedSecond (_ b: B) -> (A) -> (A,B) { + return { a in + (a,b) + } + } } diff --git a/Sources/FunctionalKit/FunctionType.swift b/Sources/FunctionalKit/FunctionType.swift index eb0b6da..3dcca55 100644 --- a/Sources/FunctionalKit/FunctionType.swift +++ b/Sources/FunctionalKit/FunctionType.swift @@ -32,6 +32,12 @@ public extension FunctionType { return dimap(transform, f.identity) } + func carryOver() -> Function> { + return Function>.init { source in + Product.init(source, self.call(source)) + } + } + func toFunction() -> Function { return dimap(f.identity, f.identity) } diff --git a/Sources/FunctionalKit/FunctorTransformers2.generated.swift b/Sources/FunctionalKit/FunctorTransformers2.generated.swift index 88fcef1..ba6792c 100644 --- a/Sources/FunctionalKit/FunctorTransformers2.generated.swift +++ b/Sources/FunctionalKit/FunctorTransformers2.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/Sources/FunctionalKit/FunctorTransformers3.generated.swift b/Sources/FunctionalKit/FunctorTransformers3.generated.swift index aa94614..2489a9e 100644 --- a/Sources/FunctionalKit/FunctorTransformers3.generated.swift +++ b/Sources/FunctionalKit/FunctorTransformers3.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/Sources/FunctionalKit/MonadTransformers2.generated.swift b/Sources/FunctionalKit/MonadTransformers2.generated.swift index aa7906e..4aa35d3 100644 --- a/Sources/FunctionalKit/MonadTransformers2.generated.swift +++ b/Sources/FunctionalKit/MonadTransformers2.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/Sources/FunctionalKit/MonadTransformers3.generated.swift b/Sources/FunctionalKit/MonadTransformers3.generated.swift index b441ced..65ee9db 100644 --- a/Sources/FunctionalKit/MonadTransformers3.generated.swift +++ b/Sources/FunctionalKit/MonadTransformers3.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/Sources/FunctionalKit/ReaderTransformer.generated.swift b/Sources/FunctionalKit/ReaderTransformer.generated.swift index f699dc3..80da343 100644 --- a/Sources/FunctionalKit/ReaderTransformer.generated.swift +++ b/Sources/FunctionalKit/ReaderTransformer.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/Sources/FunctionalKit/Result.swift b/Sources/FunctionalKit/Result.swift index ac32591..fc35807 100644 --- a/Sources/FunctionalKit/Result.swift +++ b/Sources/FunctionalKit/Result.swift @@ -275,5 +275,28 @@ public extension Result { } } } + + func `do`(onSuccess: (Parameter) -> Void, onFailure: (Failure) -> Void) -> Result { + switch self { + case let .success(value): + onSuccess(value) + case let .failure(error): + onFailure(error) + } + + return self + } + + func orNil() -> Result> { + return self + .map(Optional.init) + .fallback(to: nil) + } + + func coalesce(_ other: @autoclosure () -> Result) -> Result { + return self.fold( + onSuccess: Result.success, + onFailure: { _ in other() }) + } } diff --git a/Tests/FunctionalKitTests/LawsTests.generated.swift b/Tests/FunctionalKitTests/LawsTests.generated.swift index 3f40e04..9e6b71f 100644 --- a/Tests/FunctionalKitTests/LawsTests.generated.swift +++ b/Tests/FunctionalKitTests/LawsTests.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT @@ -37,6 +37,16 @@ class LawsTests: XCTestCase { + + + + + + + + + + @@ -351,11 +361,6 @@ class LawsTests: XCTestCase { - - - - - @@ -457,11 +462,6 @@ class LawsTests: XCTestCase { - - - - - //MARK: - Product - Bifunctor func testProductBifunctorIdentity() {