At Sun, 24 May 2015 17:26:12 -0700,
Alexis King wrote:
>
> > 1. What if you do care about the order? IOW should there also be
> > generic "cons" and "snoc"?
> 
> Having a generic cons is a good idea, and I will consider a good way to 
> incorporate it. However, there are lots of tradeoffs in various approaches, 
> so let me try and outline those quickly.
> 
> 1. The smaller the actual interface, the better. Right now,
> gen:sequence only contains five functions in it. This is fantastic for
> keeping code complexity down, and it heavily reduces the burden for
> implementing new collections.
> 
> 2. The tradeoff, of course, is that adopting a "one size fits all"
> model loses possible optimization that can come out of more specific
> implementations. Maintaining efficiency in this case loses accuracy,
> as you've described.

With fallback methods, you can get the best of both worlds.

You can have a base set of methods that implementers of the interface
need to write, and from which everything else can be derived. The
derived methods can also be present in the interface, which allows
implementers to override them if, e.g., their particular data structure
can implement them more efficiently.

For example, let's say you're implementing a vector-like data
structure. You can implement, say, `map` efficiently by allocating a
vector of the right size at the start, then filling it up. By overriding
the `map` method, this more efficient version will be used instead of
the fallback.

The `set` library uses this pattern.

> > Also, in my (admittedly limited) time so far with Clojure, I've
> > sometimes been confused when eager collections automagically change
> > into lazy sequences. If I add side effects (like debugging printfs) to
> > code I don't realize is lazy, I utter many WTFs.  I'm not sure to what
> > extent that can happen with your library, but if it can, this might be
> > good to point out in margin notes and examples.
> 
> Yes, this is something I hope to probe out some more with user
> feedback. If someone uses it and runs into something really
> WTF-worthy, I'll certainly try and figure out what the right way to
> handle it is. Currently, everything that can return lazy sequences is
> noted in the API reference, but making it more prominent is a good
> idea.
> 
> As for why the laziness is required to begin with, it's mostly just
> because lazy sequences are a very effective way of effectively
> implementing iterators without needing iterators. A lazy sequence can
> be turned into a concrete sequence with `extend`, and it doesn't need
> to allocate an entire intermediate sequence, which is quite
> appealing.

FWIW, that's pretty close to what Racket's `sequence` datatype (not the
interface) does. I haven't seen much code that uses them directly.

Vincent

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to