Skip to content

Commit

Permalink
Affine.zip + a few additions
Browse files Browse the repository at this point in the history
  • Loading branch information
Elviro Rocca committed Aug 3, 2018
1 parent a19cbf2 commit c5313a9
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 16 deletions.
32 changes: 32 additions & 0 deletions Sources/FunctionalKit/Affine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,38 @@ public extension Array {
}
}

public extension Affine where S == T, A == B {
static func zip <X,Y> (_ a: Affine<S,X>, _ b: Affine<S,Y>) -> Affine<S,Inclusive<X,Y>> where A == Inclusive<X,Y> {
return Affine<S,Inclusive<X,Y>>.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

/*:
Expand Down
13 changes: 13 additions & 0 deletions Sources/FunctionalKit/Collections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 16 additions & 0 deletions Sources/FunctionalKit/Combinators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public extension f {
static func flip<A,B,C>(_ function: @escaping (A) throws -> (B) throws -> C) -> (B) throws -> (A) throws -> C {
return { b in { a in try function(a)(b) } }
}

static func map <A, B> (_ transform: @escaping (A) -> B) -> ([A]) -> [B] {
return { $0.map(transform) }
}

static func map <A, B> (_ transform: @escaping (A) throws -> B) -> ([A]) throws -> [B] {
return { try $0.map(transform) }
}

static func filter <A> (_ predicate: @escaping (A) -> Bool) -> ([A]) -> [A] {
return { $0.filter(predicate) }
}

static func filter <A> (_ predicate: @escaping (A) throws -> Bool) -> ([A]) throws -> [A] {
return { try $0.filter(predicate) }
}
}

public func <<< <A,B,C> (second: @escaping (B) -> C, first: @escaping (A) -> B) -> (A) -> C {
Expand Down
12 changes: 12 additions & 0 deletions Sources/FunctionalKit/Fixed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,16 @@ public extension f {
static func asTuple <A,B,C> (_ function: @escaping (A,B) -> C) -> ((A,B)) -> C {
return { function($0.0,$0.1) }
}

static func embedFirst <A,B> (_ a: A) -> (B) -> (A,B) {
return { b in
(a,b)
}
}

static func embedSecond <A,B> (_ b: B) -> (A) -> (A,B) {
return { a in
(a,b)
}
}
}
6 changes: 6 additions & 0 deletions Sources/FunctionalKit/FunctionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public extension FunctionType {
return dimap(transform, f.identity)
}

func carryOver() -> Function<SourceType, Product<SourceType, TargetType>> {
return Function<SourceType, Product<SourceType, TargetType>>.init { source in
Product.init(source, self.call(source))
}
}

func toFunction() -> Function<SourceType,TargetType> {
return dimap(f.identity, f.identity)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/FunctorTransformers2.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/FunctorTransformers3.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/MonadTransformers2.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/MonadTransformers3.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down
2 changes: 1 addition & 1 deletion Sources/FunctionalKit/ReaderTransformer.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down
23 changes: 23 additions & 0 deletions Sources/FunctionalKit/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Failure, Optional<ParameterType>> {
return self
.map(Optional.init)
.fallback(to: nil)
}

func coalesce(_ other: @autoclosure () -> Result) -> Result {
return self.fold(
onSuccess: Result.success,
onFailure: { _ in other() })
}
}

22 changes: 11 additions & 11 deletions Tests/FunctionalKitTests/LawsTests.generated.swift
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -37,6 +37,16 @@ class LawsTests: XCTestCase {
















Expand Down Expand Up @@ -351,11 +361,6 @@ class LawsTests: XCTestCase {











Expand Down Expand Up @@ -457,11 +462,6 @@ class LawsTests: XCTestCase {








//MARK: - Product - Bifunctor

func testProductBifunctorIdentity() {
Expand Down

0 comments on commit c5313a9

Please sign in to comment.