5 ms·
A Ruby version that attempts to be as close to the elegant and readable Clojure solution as possible via similar 'crafting': def sorted_key_list(maps)
by petercooper 14y ago
A Ruby version that attempts to be as close to the elegant and readable Clojure solution as possible via similar 'crafting':
def sorted_key_list(maps)
maps.map(&:keys)
.reduce(:&)
.sort
.tap { |a| a.push "<none>" if a.empty? }
.join ","
end
Works out at 24 syntax-related punctuation symbols for Ruby, 20 for Clojure (ignoring the replace-empty function, admittedly).
- awj 14y agoYou actually want ".reduce(:|)", & does intersection on arrays. You can replace that .tap block with gsub abuse for a bit more golfing, and can replace the map/reduce combo with flat_map/uniq to eliminate one symbol-to-proc: def sorted_key_list(maps) maps.flat_map(&:keys) .uniq .sort .join(",").gsub /^$/, "<none>" end
- petercooper 14y agoD'oh, I misread the spec; this is where understanding the Clojure properly may have helped too. I thought the keys had to be in both maps, not the keys from both maps. Good call on the | and flat_map then :-)