Created
June 9, 2009 09:46
-
-
Save igal/126387 to your computer and use it in GitHub Desktop.
List to text transformation with Clojure and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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