I saw the following example in Rich's video on sequences http://blip.tv/file/734409 about 33-36 minutes into it:
(first "abcd") => \a
Now, he say that this expands to (sort of):
(first "abcd") => (first (seq "abcd")) => (first '(\a \b \c \d))
So, it looks like an O(N)
operation because the full copy of the string is being made. First of all, if a String
is immutable, then why is it copied? (Edit: based on an answer, it probably is not; just looked that way when printed.) Secondly, suppose first
operated on something else in Java that is mutable, say a linked list of integers. Should first
act in a lazy manner (e.g. create a persistent sequence first)? Would not it make sense to evaluate it right away and save it? It would be some sort of a hack that would break the nice abstraction, but get the job done fast, I think. When you call (seq "abcd")
, you do not know how it will be used. When you call a first
on a seq
, you know what to do. But, when you call first
on "abcd"
, I think that performing a hacky and fast "grab and save it", approach is better than grab a sequence and then call first
.
Am I missing something? Did Rich Hickey skip some steps?
Let me know if I have questions. Thanks!