With the dartz Dart lib, how can you construct an IMap from key-value pairs?

76 Views Asked by At

I see the factory constructor factory IMap.fromPairs(FoldableOps<dynamic, Tuple2<K, V>> foldableOps, Order<K> kOrder), but how do you use FoldableOps to be able to pass an Iterable<Tuple2<X,Y>> into it?

1

There are 1 best solutions below

0
Abion47 On

After consulting the aforementioned art installation (you know, in lieu of official documentation) as well as some sample code in the repo, I believe this is the way to do it:

iList<Tuple<K, T>> tuples = ...;
IMap<K, T> map = IMap.fromPairs(tuples, Order<K>);

Where what you pass for Order<K> depends on the key type in the tuples. If it's a native type like int or String, you can pass IntOrder or StringOrder. Otherwise, you will need to create an Order implementation for that type, e.g.:

class Foo extends Comparable {
  ...
}
Order<Foo> fooOrder = ComparableOrder<Foo>();
iList<Tuple<Foo, dynamic>> tuples = ...;
iMap<Foo, dynamic> map = iMap.fromPairs(tuples, fooOrder);