Last active
August 6, 2023 12:02
-
-
Save ShalokShalom/cb8ae4f911559a9baa38b6b6addedf58 to your computer and use it in GitHub Desktop.
About pipes, curry and partial application
This file contains 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
// The following example shows an effect of auto-currying in fsharp. | |
let add x y = x + y | |
let substract x y = x - y | |
// So that could look like a totally viable definition to you. | |
// We define an add, and a substract function for later use. | |
// But now watch. | |
let calculate = | |
10 | |
|> add 2 | |
|> substract 2 | |
// What do you think, does come out of that? | |
// You could think, 10 pipes into `add 2`, that makes 12. | |
// And then, 12 pipes into `substract 2`, and that makes 10. | |
val calculate: int = -10 | |
// So no. Its -10. Why is that? | |
// Because pipes are gentle. | |
// See for yourself; | |
let calculate = | |
10 | |
|> add 2 // add 2 10 = 12 | |
|> subtract 2 // subtract 2 12 = -10 | |
// It actually goes this way. ^ | |
// The value gets set on the first open position, reading left to right. | |
// In this case, its the open "y" position of the add function. | |
// `add` applies 2 at first to its x position. Then only comes the value from the pipe. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment