[rust-dev] purely function red-black tree

Steve Jenson stevej at fruitless.org
Sat Dec 15 16:45:15 PST 2012


On Sat, Dec 15, 2012 at 8:58 AM, Benjamin Striegel
<ben.striegel at gmail.com>wrote:

> Would this trait:
>
>     pub trait Map<K: Copy Eq Ord, V: Copy> {
>       pure fn get(k: K) -> @Option<V>;
>       pure fn put(k: K, v: V) -> self;
>       pure fn delete(k: K) -> self;
>       pure fn traverse(f: fn((&K), (@Option<V>)));
>     }
>
> be something that ought to live somewhere in the standard library?
>

I just renamed it to PersistentMap as a mutable Map would not typically
return itself on modification, that's a hallmark of a functional persistent
data structure.

Also, traverse should probably belong in its own trait.



> I also see that you have an impl of this trait in which you also define a
> bunch of methods that don't belong to the trait. I didn't even know that
> was possible! If you feel that those methods (blacken, modifiedWith,
> balance, modWith) don't belong in the Map trait, perhaps split their
> definitions off into a separate "anonymous" impl for now, and then when 0.5
> rolls around you can define a trait that inherits from the Map trait and
> requires those additional methods.
>

The private methods are part of the RBMap specialization of the Map trait
and are required to reduce boilerplate. They don't belong in a Map trait
proper.

There's a design choice here about implementations of traits that isn't
clear to me. Why restrict any given implementation of them to publicly
defined methods?

Also, can you show me an example of using an anonymous trait? That seems
neat.

Best,
Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20121215/907d8df3/attachment.html>


More information about the Rust-dev mailing list