Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active August 15, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/d305b42f5b542e9de1eaa8e56ba6bdd7 to your computer and use it in GitHub Desktop.
Save havenwood/d305b42f5b542e9de1eaa8e56ba6bdd7 to your computer and use it in GitHub Desktop.
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
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
Copy link
Author

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