From: systho@... Date: 2015-08-07T07:21:07+00:00 Subject: [ruby-core:70264] [Ruby trunk - Feature #6284] Add composition for procs Issue #6284 has been updated by Philippe Van Eerdenbrugghe. Martin D��rst wrote: > I'm teaching Haskell in a graduate class, so I'm quite familiar with function composition and use it a lot, but the original example isn't convincing at all. For me, in Ruby, something like val.strip.capitalize reads much, much better than some artificially forced function composition. If there were a method String#prepend, it would be even more natural: val.strip.capitalize.prepend('Title: '). > > If there are better examples that feel more natural in Ruby, please post them here. I fully agree with you that a good example really helps to understand the problem which lead to a better solution. The few times I've missed function composition is ruby is always when I wanted to replace the same pattern : ~~~ res = step1(base) res = step2(res) res = step3(res) ... res ~~~ I hate having to mutate the res variable and I would hate even more creating different temp variables especially when I do not know how many steps there will be. I would like to be able to combine the steps then apply the step combination to *base* There are a lot of examples for this pattern so here is one : data retrieval with ActiveRecord ( I assume most people know this library but this is only an example ) ~~~ messages = all_messages messages = restrict_to_owner(messages, owner) filters.each do |filter| messages = filter.apply(messages) end messages = messages.order(created_at: :asc) messages = paginate(messages) ~~~ ---------------------------------------- Feature #6284: Add composition for procs https://bugs.ruby-lang.org/issues/6284#change-53689 * Author: Pablo Herrero * Status: Feedback * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- =begin It would be nice to be able to compose procs like functions in functional programming languages: to_camel = :capitalize.to_proc add_header = ->val {"Title: " + val} format_as_title = add_header << to_camel << :strip instead of: format_as_title = lambda {|val| "Title: " + val.strip.capitalize } It's pretty easy to implement in pure ruby: class Proc def << block proc { |*args| self.call( block.to_proc.call(*args) ) } end end =end ---Files-------------------------------- 0002-proc.c-Implement-Method-for-Method-composition.patch (2.71 KB) 0001-proc.c-Implement-Proc-for-Proc-composition.patch (3.73 KB) 0003-proc.c-Support-any-callable-when-composing-Procs.patch (4.01 KB) -- https://bugs.ruby-lang.org/