Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions core/src/main/java/fj/data/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,37 @@ public <W> TreeMap<K, W> map(final F<V, W> f) {
return new TreeMap<K, W>(tree.map(TreeMap.<K, Option<W>>ord(o), g));
}

/**
* The expression <code>t1.union(t2)</code> takes the left-biased union of <code>t1</code>
* and <code>t2</code>. It prefers <code>t1</code> when duplicate keys are encountered.
*
* @param t2 The other tree we wish to combine with this one
* @return The combined TreeMap
*/
public TreeMap<K, V> union(TreeMap<K, V> t2) {
// TODO This could be implemented more efficiently using "hedge union"
TreeMap<K, V> result = t2;
for(P2<K,V> p : this) {
result = result.set(p._1(), p._2());
}
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (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) 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
.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, only add the key value pair if the key does not exist in the first map.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
* The expression <code>t1.union(t2)</code> takes the left-biased union of <code>t1</code>
* and <code>t2</code>. It prefers <code>t1</code> when duplicate keys are encountered.
*
* @param t2 The other list/set of pairs we wish to combine with this one
* @return The combined TreeMap
*/
public TreeMap<K, V> union(Iterable<P2<K, V>> t2) {
TreeMap<K, V> result = this;
for(P2<K,V> p : t2) {
if(!this.contains(p._1())) {
result = result.set(p._1(), p._2());
}
}
return result;
}

}