Skip to content

Instantly share code, notes, and snippets.

@igal
Created June 9, 2009 09:46
Show Gist options
  • Save igal/126387 to your computer and use it in GitHub Desktop.
Save igal/126387 to your computer and use it in GitHub Desktop.
List to text transformation with Clojure and Ruby
; Goal: Turn Array `["foo", "bar", "baz"]` into String `"ordered: 1=foo 2=bar 3=baz"`.
; Clojure
;; Read arguments from command line
;; (def argv *command-line-args*)
;; Fake command-line arguments for use in REPL
(def argv (seque ["foo" "bar" "baz"]))
(println "ordered:"
(apply str
(interpose " "
(map
#(str (first %) "=" (last %))
(map vector
(iterate inc 0)
argv)))))
# Plain Ruby
a = []
%w[foo bar baz].each_with_index do |word, i|
a << "#{i}=#{word}"
end
puts "ordered: #{a.join(' ')}"
# Ruby with Facets
require 'rubygems'
require 'facets/enumerable/map_with_index'
puts "ordered: " + %w[foo bar baz].map_with_index{|word, i| "#{i}=#{word}"}.join(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment