Skip to content

Instantly share code, notes, and snippets.

@andremartingo
Last active November 29, 2018 20:23
Show Gist options
  • Select an option

  • Save andremartingo/95c9eaa00addd43fa5f196b984545ab7 to your computer and use it in GitHub Desktop.

Select an option

Save andremartingo/95c9eaa00addd43fa5f196b984545ab7 to your computer and use it in GitHub Desktop.

🤓 Articles Forward application |> and method composition >>> https://www.pointfree.co/episodes/ep1-functions Single-type composition <> https://www.pointfree.co/episodes/ep2-side-effects Single-type composition with UIKit https://www.pointfree.co/episodes/ep3-uikit-styling-with-functions

🏗 Libraries swift-overture: https://github.com/pointfreeco/swift-overture

>>> : Compõe funções. Se tiveres duas funções g e f, a sua composição g >>> f resulta em g(f(x)) (edited) O <> é semelhante mas é chamado de single-type composition

Enquanto que o primeiro te deixa fazer A -> B e B -> C, o segundo transforma sempre o mesmo tipo, ou seja A -> A Mas se vires, internamente é igual ao >>> Temos uma excepção que essa não é tão comum, que é A -> Void Mas o resultado é o mesmo, aplicamos primeiro f e depois g

precedencegroup ForwardApplication {
associativity: left
higherThan: AssignmentPrecedence
}
infix operator |> : ForwardApplication
// MARK: - Forward function application
/// Forward function application.
///
/// Applies the function on the right to the value on the left.
/// Functions of >1 argument can be applied by placing their arguments in a tuple on the left hand side.
///
/// This is a useful way of clarifying the flow of data through a series of functions.
/// For example, you can use this to count the base-10 digits of an integer:
///
/// `let digits = 100 |> toString |> count // => 3`
func |> <A, B> (a: A, f: (A) -> B) -> B {
return f(a)
}
@discardableResult
func |> <A: AnyObject> (a: A, f: (A) -> Void) -> A {
f(a)
return a
}
infix operator ?|> : ForwardApplication
func ?|> <A, B> (a: A?, f: (A) -> B) -> B? {
guard let a = a else { return nil }
return f(a)
}
@discardableResult
func ?|> <A: AnyObject> (a: A?, f: (A) -> Void) -> A? {
guard let a = a else { return nil }
f(a)
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment