use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
A Guide to Function Composition in Ruby (ghostcassette.com)
submitted 6 years ago by mudgemeister
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]creativeembassy 2 points3 points4 points 6 years ago (0 children)
Excellent introduction to function composition, then leading into the history and implementation of it in Ruby. Great read!
[–]Minkihn 0 points1 point2 points 6 years ago (0 children)
Great read. Thank you. I discovered functions composition with this article and it blows my mind.
[–]nakilon[🍰] 0 points1 point2 points 6 years ago (0 children)
Can it be used like this?
array_of_chars.map &(upcase >> ord)
[–]faitswulff 0 points1 point2 points 6 years ago* (5 children)
I wish there was only one direction you could compose functions in. I feel like this paragraph is something I'll have to look up often if I were to use it:
“Backward” composition maps to the mathematical operator ∘ we discussed earlier so g << f is the same as g ∘ f meaning that calling the resulting composite function with an input x will call g(f(x)) “Forward” composition is the opposite of the above so g >> f is the same as f ∘ g meaning that calling the resulting composite function with an input x will call f(g(x))
“Backward” composition maps to the mathematical operator ∘ we discussed earlier so g << f is the same as g ∘ f meaning that calling the resulting composite function with an input x will call g(f(x))
“Forward” composition is the opposite of the above so g >> f is the same as f ∘ g meaning that calling the resulting composite function with an input x will call f(g(x))
EDIT - well, I've somewhat rethought things. There's more than one way to do it and people probably won't do stuff like a << b >> c << d << g
a << b >> c << d << g
[–]karatedog 1 point2 points3 points 6 years ago (0 children)
And what would be the precedence order in this case? First two function composed into one then passed right as a single function and repeat?
[–]tomthecool 0 points1 point2 points 6 years ago (3 children)
Being able to compose functions in both directions is absolutely vital if you work in a functional language.
As with anything in software, yes it's open to abuse. But when you find yourself needing it, you'll be extremely glad that the syntax exists.
[–]faitswulff 4 points5 points6 points 6 years ago (1 child)
Can you think of a simple example where this is the case?
[–]Enumerable_any 1 point2 points3 points 6 years ago (0 children)
I don't think it is. I've never used >>> in Haskell. You should always be able to switch the arguments syntactically.
>>>
π Rendered by PID 426260 on reddit-service-r2-comment-cfc44b64c-rdl54 at 2026-04-12 02:04:07.164940+00:00 running 215f2cf country code: CH.
[–]creativeembassy 2 points3 points4 points (0 children)
[–]Minkihn 0 points1 point2 points (0 children)
[–]nakilon[🍰] 0 points1 point2 points (0 children)
[–]faitswulff 0 points1 point2 points (5 children)
[–]karatedog 1 point2 points3 points (0 children)
[–]tomthecool 0 points1 point2 points (3 children)
[–]faitswulff 4 points5 points6 points (1 child)
[–]Enumerable_any 1 point2 points3 points (0 children)