Add union to TreeMap#83
Conversation
There was a problem hiding this comment.
Lets call this union. This is the defition of union in Haskell, https://downloads.haskell.org/~ghc/6.12.2/docs/html/libraries/containers-0.3.0.0/Data-Map.html#v%3Aunion:
union :: Ord k => Map k a -> Map k a -> Map k a
O(n+m). The expression (union t1 t2) takes the left-biased union of t1 and t2. It prefers t1 when duplicate keys are encountered, i.e. (union == unionWith const). The implementation uses the efficient hedge-union algorithm. Hedge-union is more efficient on (bigset `union` smallset).
union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
Please review and let me know when this is updated. Please update the javadoc, return type (it should return TreeMap<K, V>) and method body.
There is an opportunity to implement some of the Haskell combine methods: unionWith, unionWithKey, unions and unionsWith.
There are also lots of other methods we could add from the Haskell doc above.
There was a problem hiding this comment.
Should it left biased in that case?
On Jan 15, 2015 3:16 AM, "Mark Perry" notifications@github.com wrote:
In core/src/main/java/fj/data/TreeMap.java
#83 (diff)
:
- /**
- * Extend this TreeMap to include all mapping from another TreeMap. Where a
- * mapping exists in both this TreeMap and the other one, the value from the
- * other one will replace the value in this one.
- * @param other
- * @return
- */
- public Object setAll(TreeMap<K, V> other) {
TreeMap<K, V> result = this;for(P2<K,V> p : other) {result = result.set(p._1(), p._2());}return result;- }
Lets call this union. This is the defition of union in Haskell,
https://downloads.haskell.org/~ghc/6.12.2/docs/html/libraries/containers-0.3.0.0/Data-Map.html#v%3Aunion
https://downloads.haskell.org/%7Eghc/6.12.2/docs/html/libraries/containers-0.3.0.0/Data-Map.html#v%3Aunion
:union :: Ord k => Map k a -> Map k a -> Map k a
O(n+m). The expression (union t1 t2) takes the left-biased union of t1 and t2. It prefers t1 when duplicate keys are encountered, i.e. (union == unionWith const). The implementation uses the efficient hedge-union algorithm. Hedge-union is more efficient on (bigsetunionsmallset).union (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
Please review and let me know when this is updated. Please update the
javadoc, return type (it should return TreeMap) and method body.There is an opportunity to implement some of the Haskell combine methods:
unionWith, unionWithKey, unions and unionsWith.There are also lots of other methods we could add from the Haskell doc
above.—
Reply to this email directly or view it on GitHub
https://github.com/functionaljava/functionaljava/pull/83/files#r23003389
.
There was a problem hiding this comment.
Yes, only add the key value pair if the key does not exist in the first map.
There was a problem hiding this comment.
For later reference, see "Implementing Sets Efficiently in a Functional Language" by Stephen Adams as mentioned here, purescript-deprecated/purescript-maps#1.
A current link to the paper is:
Implementation in Haskell:
|
@dobesv Any news of progress on this issue? |
|
@mperry OK I pushed some changes, please take a look. I had a case where I wanted to just merge in a list of pairs, and I added that as well. |
|
Thanks @dobesv for your contribution. |
No description provided.