Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created August 23, 2016 14:38
Show Gist options
  • Save skatenerd/6fc88253708c36f01e9a1919384bb3db to your computer and use it in GitHub Desktop.
Save skatenerd/6fc88253708c36f01e9a1919384bb3db to your computer and use it in GitHub Desktop.
pipeline vs composition
-- pipelining
let x = 1
doubled = x * 2
squared = doubled * doubled
decremented = squared - 1
in decremented
-- composition
let x = 1
double = (*) 2
square x = x * x
decrement x = x - 1
in (decrement . square . double) x
@raganwald
Copy link

Yes, and the vital difference between them is that the composition version creates a new first-class that can be named, passed as a value, &tc., while the pipelined version does not. Furthermore, the composition version can be dynamically composed, while most languages have difficulty dynamically pipelining things.

Both do the exact same thing in the small, but the compositional version can be much better if there is some wonderful thing enabled by making something first-class. And it can be worse if that is a distraction to the primary responsibility fo the code.

@skatenerd
Copy link
Author

@raganwald, Thanks for responding!

I'm not sure I understand the distinction. In this case, both let statements could just be the body of a first-class function, which could be named, passed around, composed, etc.

Maybe this example is just too small.

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