Created
October 2, 2011 12:16
-
-
Save CrypticSwarm/1257396 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
// Flow is a construct that can chain together functions. | |
// Flow denotes regular function composition. | |
var chain1 = | |
Flow <- f1 | |
<- f2 | |
<- f3 | |
chain1(123) // same as f3(f2(f1(123))) | |
// Can also chain together multiple things that go to one. | |
var chain2 = | |
Flow <- f1 & f2 | |
<- f3 | |
chain2(123) // same as f3(f1(123), f2(123)) | |
//Now the interesting part is what if you don't want functions brought together in this way? | |
//Say you want f3([f1(123), f2(123)]) to output as an array instead of separate args. | |
//Just define a new flow construct | |
var chain3 = | |
FlowArr <- f1 & f2 | |
<- f3 | |
chain3(123) | |
// same as f3([f1(123), f3(123)]) | |
//Since each part acts on functions and returns a function, you can comprise more than one type together. | |
var chain4 = | |
Flow | |
<- f1 & f2 | |
<- f3 | |
(FlowArr | |
<- f4 & f5 | |
<- f6) | |
chain4(123) | |
//same as | |
// var temp = f3(f1(123), f2(123)) | |
// f6([f4(temp), f5(temp)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment