Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic in Clojure: (drop 1 str) or (rest str)?

Tags:

idioms

clojure

Simple question here for Clojure. Which is more idiomatic when dealing with strings? Which is more idiomatic when dealing with other data types? Which is more efficient?

(drop 1 str)

or

(rest str)
like image 765
Havvy Avatar asked Apr 11 '11 01:04

Havvy


1 Answers

Whatever you're dealing with, rest will be more efficient, but the difference will be so trivial as to be uninteresting.

Make sure you know whether you want rest or next, because in some cases it matters. rest is lazier, and is probably right more often, but if you use rest when you need next the results can be hard to track down.

For strings...neither! Use subs, the built-in substring operator:

user> (subs "hears" 1)
"ears"

rest is fine, but it would yield a seq of characters instead of a string; usually you'll find a string easier to deal with, and it's simple enough to turn it back into a seq of characters sometime later.

like image 130
amalloy Avatar answered Sep 30 '22 18:09

amalloy