8

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)

1 Answer 1

26

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.

1
  • 1
    Is drop just as lasy as rest? Or is drop more like next? Jul 20, 2016 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.