4 ms·
Functional Programming With Ruby
- stuffihavemade 15y agoI like Ruby, and I like functional programming, but if you try to shoehorn statelessness into your Ruby programs, you will be fighting the language, and you will feel a lot of pain. All of the data structures are ephermal, even strings. So, the language will not give thet persisitent, relatively perfomant strutuctes that Haskell or Clojure provides. If you want functional Ruby, just go use Clojure.
- rlander 15y agoWhile I do agree with you that trying to shoehorn ruby into a pure functional style is a bad idea, I believe it's generally a good thing to try to write functional-style ruby: Functional in the small and OO in the large [1]. For example, you can have referential transparency in your methods, which will allow for easier debugging. You can also strive for simpler data structures and even freeze objects to make them immutable[2]. [1] http://www.johndcook.com/blog/2009/03/23/functional-in-the-small-oo-in-the-large/ http://www.johndcook.com/blog/2009/03/23/functional-in-the-s... [2] http://ontwik.com/ruby/better-ruby-through-functional-programming-by-dean-wampler/ http://ontwik.com/ruby/better-ruby-through-functional-progra...
- jhawthorn 15y agoOther than recommending #map, #select, et al, the rest is poor advice. Consider the following recommendation input.inject({}) do |hash, item| hash.merge(item => process(item)) end Far, far slower, more memory intensive (big deal, given ruby's gc), and uglyer than the sane (and commonly accepted) Hash[input.map do |item| [item, process(item)] end]