(defn <3 [love & loves] (loop [l (str "I love " love) loves loves] (let [[love & loves] loves] (if (nil? love) (str l ".") (if (empty? loves) (str l " and " love ".") (recur (str l ", " love) loves)))))) (<3 "cookies" "cake" "chocolat") ;; => "I love cookies, cake and chocolat."
murphy:
I = Object.new def I.<(three, *args) puts "I love " + args[0..-2].join(', ') + ' and ' + args.last + '.' end I.<3, 'test', 'foo' #~> I love test and foo.
I like the Clojure one better, even without syntax highlighting :)
Update: Licenser complained, so here’s an improved version:
class Lover def <(three, *loves) text = 'I love ' text << loves[0..-2].join(', ') if loves.size >= 2 text << ' and ' << loves.last elsif loves.first text << loves.first else text << 'nothing' end text << '.' text end end
I = Lover.new
I.<3 # => “I love nothing.”
I.<3, ‘test’ # => “I love test.”
I.<3, ‘test’, ‘foo’ # => “I love test and foo.”
I.<3, ‘test’, ‘foo’, ‘bar’ # => “I love test, foo and bar.”