Tue 2010-05-04 ( En pr )

Licenser:

(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.”

Say something! / Sag was!

Reviewing the ruby code I notice two things:
I.<3 "cookies" will return "I love cookies and cookies." right?
Second I < X will break :P. I know it is just mean to point out this for silly code but a true compaurisons should spawn (nearly) equal results ;)
Licenser @ 19:31 on Saturday, 2010-05-08
Here you go :)
murphy @ 22:21 on Saturday, 2010-05-08
Hey, found this on a random google (well, maybe less than random...) and thought I'd chip in. A clojure version nearer to the ruby one (actually, slightly better :]) would be: 

(defn <3 [& loves]
  (apply str
	 `("I love " ~@(interpose ", " (butlast loves)) " and " ~(last loves) ".")))

And a perhaps more idiomatic clojure version (though probably not - I'm still learning!)

(defn <3 [& loves]
  (let [ls (butlast loves)
	end (cond (empty? loves) " nothing"
		  ls " and ")]
    (apply str `("I love " ~@(interpose ", " ls) ~end ~(last loves) "."))))

Still beats the ruby one, I reckon!
JWM @ 20:31 on Tuesday, 2010-05-18
Actually, leave off the space before "nothing" in line 3. Other than that, the results seem to match!
JWM @ 20:37 on Tuesday, 2010-05-18
Hmm. I think the lispier way would be:

user> (defn <3 [& loves]
        (apply clojure.contrib.pprint/cl-format (list* nil "I love~#[ nothing~; ~A~; ~A and ~A~:; ~A~@{~#[~; and ~A~:;~^, ~A~]~}~]." loves)))
#'user/<3
user> (<3)
"I love nothing."
user> (<3 "cookies")
"I love cookies."
user> (<3 "cookies" "apples")
"I love cookies and apples."
user> (<3 "cookies" "apples" "pies")
"I love cookies, apples and pies."
user> (<3 "cookies" "apples" "pies" "even strawberries")
"I love cookies, apples, pies and even strawberries."
user> 
MHOOO @ 01:29 on Wednesday, 2010-08-18
1
aYlNlfdX @ 19:32 on Tuesday, 2024-08-06
1
aYlNlfdX @ 19:32 on Tuesday, 2024-08-06
1
aYlNlfdX @ 19:36 on Tuesday, 2024-08-06
1
aYlNlfdX @ 19:36 on Tuesday, 2024-08-06
1
aYlNlfdX @ 20:03 on Tuesday, 2024-08-06
1
aYlNlfdX @ 20:03 on Tuesday, 2024-08-06

No markup, just plain monospace text. / Kein Markup, nur Normschrift-Klartext.