core icon indicating copy to clipboard operation
core copied to clipboard

[Feature Request]: provide @immut/sorted_map.T::intersection_map2

Open hackwaly opened this issue 1 year ago • 0 comments

pub intersection_map2[K, V, M](
  self : T[K, V],
  other : T[K, V],
  f : (V, V) -> M
) -> T[K, M]

The result type of intersection_map2 can be Iter or Array.

Use case in moonyacc:

fn weak_compat(set1 : EncodedLR1ItemSet, set2 : EncodedLR1ItemSet) -> Bool {
  let common = @immut/sorted_set.intersection(
      @immut/sorted_set.from_iter(set1.keys()), 
      @immut/sorted_set.from_iter(set2.keys())
    )
    .to_array()
    .map(fn(key) { (key, set1._[key].unwrap(), set2._[key].unwrap()) })
  for i in 0..<common.length() {
    let (_, la_set1i, la_set2i) = common[i]
    for j in i..<common.length() {
      guard i != j else { continue }
      let (_, la_set1j, la_set2j) = common[j]
      let compat = (la_set1i.disjoint(la_set2j) && la_set2i.disjoint(la_set1j)) ||
        not(la_set1i.disjoint(la_set1j)) ||
        not(la_set2i.disjoint(la_set2j))
      if not(compat) {
        return false
      }
    }
  }
  true
}

If we have intersection_map2

fn weak_compat(set1 : EncodedLR1ItemSet, set2 : EncodedLR1ItemSet) -> Bool {
  let common = @immut/sorted_map.intersection_map2(set1, set2, fn (v1, v2) { (v1, v2) })
    .to_array()
  for i in 0..<common.length() {
    let (_, (la_set1i, la_set2i)) = common[i]
    for j in i..<common.length() {
      guard i != j else { continue }
      let (_, (la_set1j, la_set2j)) = common[j]
      let compat = (la_set1i.disjoint(la_set2j) && la_set2i.disjoint(la_set1j)) ||
        not(la_set1i.disjoint(la_set1j)) ||
        not(la_set2i.disjoint(la_set2j))
      if not(compat) {
        return false
      }
    }
  }
  true
}

hackwaly avatar Dec 08 '24 14:12 hackwaly