Hacker Newsnew | comments | show | ask | jobs | submit login

Such a great site. The page on flattening arrays blows me away at how different and beautiful some languages can be (especially Ruby and Erlang's take).

http://rosettacode.org/wiki/Flatten_a_list




I'll give you Erlang, but the Ruby solution is cheating rather.

-----


Here's a better Ruby version:

    module Enumerable
      def custom_flatten
        inject([]) do |result, element|
          if element.respond_to?(:custom_flatten)
            result.concat(element.custom_flatten)
          else
            result.push(element)
          end
        end
      end
    end
Works on all Enumerables (Array, Hash, Set, etc)

-----


How is Ruby's method and different than Groovy or JavaScript? I think their all pretty neat. Goes to show that there is always more than one way to skin a list.

-----


True. I guess built-in functions shouldn't count.

-----




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: