Re:Crafting Code in Clojure
by markov_twain on Friday February 01, 2013 @07:40PM
(#5154519)
so this is horribly non-idiomatic ruby, but it was fun
edit: a little bit better.
https://gist.github.com/66b45e765a2aa6a97143
[ Parent
| on HN ]
slightly more idiomatic, though perhaps not quite as clear as the clojure:
def unique_keys(*hashes)
result = {}
hashes.each do |hash|
result.merge! hash
end
return result.keys.map(&:to_s).sort.join(', ') unless result.empty?
'<none>'
end
https://gist.github.com/4695388I think accumulating an initialized variable in an imperative #each loop is decidedly unidiomatic, just something we tend to do until we really embrace things like #map, #partition, #select, #reduce, etc. (And what a glorious day that is!)
I reckon what makes his first example somewhat unidiomatic is the arbitrary lambda and the use & inner return from the #taps block.
His updated example is just good ol tacit Ruby. But then again the amount of times I see an #each block with inner logic used instead of a simple point-free one-liner in Ruby really just proves your point.
Re:Crafting Code in Clojure
by markov_twain on Saturday February 02, 2013 @12:09AM
(#5155242)
well, I hate to beat a dead horse, but here's a one-liner (ruby):
(map1.keys | map2.keys).map(&:to_s).sort.join(", ").instance_eval { empty? ? "<none>" : self }