From: Brian Candler Date: 2012-09-15T05:40:55+09:00 Subject: Re: inject is pathetic? Josh Cheek wrote in post #1075977: > But in Ruby > > strings.inject("") { |joined, current| joined + current } > > is slow and wasteful because it created all the intermediate forms: "", > "a", "ab", "abc", "abcd", "abcde" > Whereas each_with_object only creates: "abcde" Correct, in ruby, because ruby isn't optimised for funtional use. Another example is that there is no tail-recursion unless you build ruby with a special compile-time option. But in other languages, what you've written is actually efficient. For example, Erlang has a specific optimisation for binaries which are appended to, where no reference remains to the previous incarnation of the binary. > Your example is compelling to you, because you're enamored with the > implementation I'd say the opposite; in ruby the implementation is not so good, but it's the concept, the abstraction, which is attractive. foo.each => do something for each element of an array foo.inject => calculate a summary value across all elements of an array In fully functional languages, foo.each makes no sense because "do something" makes no sense - functions don't have side-effects. (Erlang is *not* such a language, by the way, but is part way because it doesn't have mutable variables). -- Posted via http://www.ruby-forum.com/.