4 people like it.

In-memory joins

Left join (2 maps) and inner join (3 maps)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
module Map =    
    /// Left join lhs map with rhs using a rhs map key selector
    let leftJoin2 joinOn left right =
      let leftKeyVal = Map.toSeq left
      seq {
        for (key, lv) in leftKeyVal do
          let joinKey = joinOn (key, lv)
          let rightValueOpt = Map.tryFind joinKey right
          yield key, lv, rightValueOpt
        }
      
    /// Inner join lhs map with 2 rhs maps with 'k key selection on lhs map
    let innerJoin3 joinOn left (right1: Map<'k,_>) (right2: Map<'k,_>) =
      let leftKeyVal = Map.toSeq left
      seq {
        for (key, lv) in leftKeyVal do
          let joinKey = joinOn (key, lv)
          let r1Value = Map.tryFind joinKey right1
          let r2Value = Map.tryFind joinKey right2
          match (r1Value, r2Value) with
          | Some r1, Some r2 -> yield key, lv, r1, r2
          | _ -> ()
        }
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IReadOnlyDictionary<'Key,'Value>
  interface IReadOnlyCollection<KeyValuePair<'Key,'Value>>
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  ...

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val leftJoin2 : joinOn:('a * 'b -> 'c) -> left:Map<'a,'b> -> right:Map<'c,'d> -> seq<'a * 'b * 'd option> (requires comparison and comparison)


 Left join lhs map with rhs using a rhs map key selector
val joinOn : ('a * 'b -> 'c) (requires comparison and comparison)
val left : Map<'a,'b> (requires comparison)
val right : Map<'c,'d> (requires comparison)
val leftKeyVal : seq<'a * 'b> (requires comparison)
val toSeq : table:Map<'Key,'T> -> seq<'Key * 'T> (requires comparison)
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
val key : 'a (requires comparison)
val lv : 'b
val joinKey : 'c (requires comparison)
val rightValueOpt : 'd option
val tryFind : key:'Key -> table:Map<'Key,'T> -> 'T option (requires comparison)
val innerJoin3 : joinOn:('a * 'b -> 'k) -> left:Map<'a,'b> -> right1:Map<'k,'c> -> right2:Map<'k,'d> -> seq<'a * 'b * 'c * 'd> (requires comparison and comparison)


 Inner join lhs map with 2 rhs maps with 'k key selection on lhs map
val joinOn : ('a * 'b -> 'k) (requires comparison and comparison)
val right1 : Map<'k,'c> (requires comparison)
val right2 : Map<'k,'d> (requires comparison)
val joinKey : 'k (requires comparison)
val r1Value : 'c option
val r2Value : 'd option
union case Option.Some: Value: 'T -> Option<'T>
val r1 : 'c
val r2 : 'd
Raw view Test code New version

More information

Link:http://fssnip.net/7Zt
Posted:4 years ago
Author:Swoorup Joshi
Tags: yield