Last active
February 6, 2022 10:02
-
-
Save DharmeshRathod712/02f36dfdc98ff1c4d0f5030b1fd064c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Pointfree is cool!
I was not aware of it. But surely try it. Thanks for your suggestion. ;)
Try watching this: https://www.pointfree.co/episodes/ep1-functions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pointfree is cool!