I don't know if it is idiomatic, but it certainly looks like a good way to
achieve the desired effect.

If you chained together calls to several functions like your example
only-evens, it would not be lazy, and it would build up a separate instance
of collections of the original type at each step of the way.  Likely it
would be more efficient to delay the conversion back to the original
collection type until after the last sequence operation you wanted to
perform.

Andy


On Sat, Nov 16, 2013 at 4:01 PM, Alexandru Nedelcu <a...@bionicspirit.com>wrote:

> Hi,
>
> I'm trying to understand the design of Clojure's collections and one
> thing that I find odd is the return of functions operating on sequences.
> Like for instance a call such as this will return a lazy-seq and not a
> vector:
>
>     (drop 2 [1 2 3 4])
>
> The reason why I find it odd is that data-structures have different
> characteristics and one may want to use a vector because it supports
> efficient indexing and appending to the end.
>
> Of course, dropping 2 elements like above from a vector is probably
> going to have O(n) complexity and thus returning something lazy is more
> appropriate. And while there are some operations, like "conj", "pop" and
> "peek" that preserve the type, functions such as map and filter also
> return lazy-seq. And I worry that the property of the collection you
> start with is lost, given that this returns a "cons":
>
>      (conj (filter even? [1 2 3 4 5]) 6)
>
> So lets say that I want to write a generic function that preserves the
> type of that collection. Is something like this idiomatic?
>
>      (defn only-evens [coll]
>        (into (empty coll) (filter even? coll)))
>
> Thanks,
>
> --
> Alexandru Nedelcu
> www.bionicspirit.com
>
> PGP Public Key:
> https://bionicspirit.com/key.aexpk
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to