On Nov 22, 4:59 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> On Nov 22, 9:48 pm, Rich Hickey <[EMAIL PROTECTED]> wrote:
>
> > map and filter don't modify anything. What does it mean to filter a
> > vector?
>
> Yes yes, I know that. Still in English its sometimes easier to be
> sloppy and pretend that I modify something even though I get another
> vector.
>
> > In any case, the vector itself is immutable, so calling vec on the
> > return value of filter is fine.
>
> Well yes its fine but what happens is that filter returns a (lazy) seq
> and calling vec on that seq will allocate a new array of the same
> length and copy every item over.
>

I still don't understand your expectation here. If the filter does any
filtering, it won't return everything in the vector, so the new result
will have fewer items. Do you want a vector with fewer items? Then
you'll have a new vector.

> > As far as map, if you have a function that will only affect a few of
> > the items in the vector, you might want to reduce, making 'changes' as
> > you go.
>
> Thanks. That sounds like it would work out nicely. Still in general my
> issue seems to be that data strucutres turn into seqs and this can
> cause subtle issues as for instance the different behaviour of conj
> and a simple rule such as calling vec on the seq seems to be to
> costly.

Nothing turns anything into seqs - a seq is a view on a collection.

If you are 'modifying' every element, then you'll have to have an
entirely new vector. Calling a mapping function and pouring the result
into a new vector will be more efficient than pretending to modify
each element in place with assoc.


In general, it can be efficient to perform a series of transformations
in the seq space and then realize a vector at the end if you want. The
lazy seqs just act as a cursor over the data, and a stream of results,
and building a new vector from scratch by appending, as vec or (into
[] ...) would do, is more efficient than element 'replacement'.

If you don't expect a copy when mapping across an entire vector, and
you want to use the immutable data structures, what alternative do you
imagine?

Rich

--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to