Skip to content

Instantly share code, notes, and snippets.

@havenwood havenwood/compose_procs.rb
Last active Aug 15, 2018

Embed
What would you like to do?
class Proc
def >> other
-> arg { other.to_proc.call call arg }
end
def << other
-> arg { call other.to_proc.call arg }
end
end
add_forty_two = -> x { x + 42 }
[1, 3, 6].map &:abs2.to_proc << add_forty_two
#=> [1849, 2025, 2304]
[1, 3, 6].map &:abs2.to_proc >> add_forty_two
#=> [43, 51, 78]
[:hi, :there].map &:to_s.to_proc << :upcase
#=> ["HI", "THERE"]
[:hi, :there].map &:to_s.to_proc >> :upcase >> :reverse
#=> ["IH", "EREHT"]
[:hi, :there].map &:reverse.to_proc << :upcase << :to_s
#=> ["IH", "EREHT"]
@printercu

This comment has been minimized.

Copy link

printercu commented Aug 9, 2018

Looks like Proc#>> and Proc#<< are swapped - results are not the same as in comments.

Symbol methods are not required, because only first symbol need to be converted explicitly:

[:hi, :there].map &:to_s.to_proc << :upcase << :reverse

This is be better because symbols should not be tied with functional computations. At least in stdlib.

@havenwood

This comment has been minimized.

Copy link
Owner Author

havenwood commented Aug 15, 2018

Ah, interesting. Thank you for the feedback! I'll update the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.