Skip to content

Commit

Permalink
Introduce functional getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Raphaël Bordet committed Jul 13, 2020
1 parent 72be474 commit 7a87d4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Caprice/ForwardComposition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,41 @@ import Foundation
infix operator *~: ForwardComposition

public func *~ <A, B> (lhs: Lens<A, B>, rhs: B) -> (A) -> A {
return { a in lhs.set(rhs, a) }
return { a in
lhs.set(rhs, a)
}
}

prefix operator ^

public prefix func ^ <A, B> (lhs: Lens<A, B>) -> (A) -> B {
return { a in
lhs.get(a)
}
}

infix operator >>>: ForwardComposition

public func >>> <A>(f: @escaping (A) -> A,
g: @escaping (A) -> A) -> (A) -> A {
public func >>> <A>(
f: @escaping (A) -> A,
g: @escaping (A) -> A) -> (A) -> A {
return { a in
g(f(a))
}
}

public func >>> <A, B, C>(f: @escaping (A) -> B,
g: @escaping (B) -> C) -> (A) -> C {
public func >>> <A, B, C>(
f: @escaping (A) -> B,
g: @escaping (B) -> C) -> (A) -> C {
return { a in
g(f(a))
}
}

public func >>> <A, B, C> (_ lhs: Lens<A, B>, _ rhs: Lens<B, C>) -> Lens<A, C> {
public func >>> <A, B, C> (
_ lhs: Lens<A, B>,
_ rhs: Lens<B, C>
) -> Lens<A, C> {
Lens<A, C>(
get: { a in rhs.get(lhs.get(a)) },
set: { (c, a) in lhs.set(rhs.set(c, lhs.get(a)), a) }
Expand Down
4 changes: 4 additions & 0 deletions CapriceDemo/View Controllers/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class ViewController: UIViewController {
Alarm.morningStar
|> Alarm.titleLens *~ "new morning star"
|> Alarm.enabledLens *~ false

let result = Alarm.morningStar |> ^Alarm.titleLens

print(result)

mainLabel.text = newMorningStar.title
}
Expand Down

0 comments on commit 7a87d4a

Please sign in to comment.