Live data from Hacker News

Programming Idioms

programming-idioms.org

1–10 of 19 posts

Re: Programming Idioms

#3
These kinds of collections can be nice, but I think it’s fair to warn against looking at them too closely. Sometimes they’re not actually idiomatic and often the implementations have differences in them that see non-obvious.

Re: Programming Idioms

#7
The clojure one to reverse a string makes it a sequence of characters, then reverses that sequence and puts it back together again as a string..

Why not jsut:

     (require '[clojure.string :as str])
     (let [s "hello"]
         (str/reverse s))
     => "olleh"
Reversing a string is in the core library...

Re: Programming Idioms

#8

These kinds of collections can be nice, but I think it’s fair to warn against looking at them too closely. Sometimes they’re not actually idiomatic and often the implementations have differences in them that see non-obvious.

And some have problems: the Python example of reading a file into a string is:

    lines = open(f).read()
which doesn't close the file. The second C example of this task is tricky, and seems to miss an offset.

The C and Go implementations of uniform random integer include the upper limit, the Python and Rust implementations don't.

And that's just from quickly browsing.

Re: Programming Idioms

#10
post #7

The clojure one to reverse a string makes it a sequence of characters, then reverses that sequence and puts it back together again as a string.. Why not jsut: (require '[clojure.string :as str]) (let [s "hello"] (str/reverse s)) => "olleh" Reversing a string is in the core library...

Well, clojure.string/reverse is relying on StringBuilder so I guess you could call it not just Clojure but Clojure + Java really. Source for clojure.string/reverse is this: `(.toString (.reverse (StringBuilder. s))))`

(The implementation of clojure.string/reverse) would only work on Clojure running on JVM. Clojure-clr, ClojureScript or any of the other ones, would use a difference implementation, not using StringBuilder. `(apply str (reverse s))` is then I guess "more" general across Clojure implementations. So Clojure is using StringBuilder, ClojureScript is using regexes + split to list + reverse + rejoin, ClojureCLR I'm not sure, but probably something C# specific.

But in the end, that's all semantics and the point is moot. Since Clojure is a pragmatic language, using an already defined function is obviously better, so no reason why you wouldn't use clojure.string/reverse, all the Clojure implementations have it exposed already.

Post reply on HN