开发者

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

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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜