Skip to content

Instantly share code, notes, and snippets.

@DharmeshRathod712
Last active February 6, 2022 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DharmeshRathod712/02f36dfdc98ff1c4d0f5030b1fd064c3 to your computer and use it in GitHub Desktop.
Save DharmeshRathod712/02f36dfdc98ff1c4d0f5030b1fd064c3 to your computer and use it in GitHub Desktop.
precedencegroup ForwardPipe {
associativity: left
}
infix operator |> : ForwardPipe
func |> <T, U>(value: T, function: ((T) -> U)) -> U {
return function(value)
}
func add(val: Int) -> Int {
return val + 3
}
func multiply(val: Int) -> Int {
return val * 3
}
// **** Ordinary way of calling composed functions 😒 ****
let newVal = add(val: multiply(val: add(val: 2)))
print(newVal) //It will print 18
// **** Calling composed functions using Forward Pipe 😁 ****
let value = 2 |> add |> multiply |> add
print(value) //It will print 18
// **** Calling composed functions with multiple parameters 🤩 ****
func diagonalLength(width: Double, height: Double) -> Double {
return sqrt(width * width + height * height)
}
let length = (3, 4) |> diagonalLength
print(length) // It will print 5.0
@Viranchee
Copy link

Pointfree is cool!

@DharmeshRathod712
Copy link
Author

Pointfree is cool!

I was not aware of it. But surely try it. Thanks for your suggestion. ;)

@Viranchee
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment