Skip to content

Implementation of Applicative Map when Key is Monoid #1540

Answered by ingun37
ingun37 asked this question in Q&A
Discussion options

You must be logged in to vote

I think I've done it

function getApplicativeMap<K, T>(
  kMon: Monoid<K>,
  kOrd: Ord<K>
): Applicative2C<map.URI, K> {
  return {
    _E: kMon.empty,
    of(x) {
      const t = tup(kMon.empty, x);
      return pipe([t], fromTuples(kOrd));
    },
    map(fa, f) {
      return map.map(f)(fa);
    },
    URI: map.URI,
    ap(fab, fa) {
      return pipe(
        fab,
        map.toArray(kOrd),
        array.chain(([k1, ab]) =>
          pipe(
            fa,
            map.toArray(kOrd),
            array.map(([k2, a]) => tup(kMon.concat(k1, k2), ab(a)))
          )
        ),
        fromTuples(kOrd)
      );
    },
  };
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ingun37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant