fp-ts-std icon indicating copy to clipboard operation
fp-ts-std copied to clipboard

Record.mergeWith (using Ord)

Open anthonyjoeseph opened this issue 3 years ago • 1 comments

Adapts lodash's mergeWith using fp-ts idioms & type safety

import * as S from "fp-ts/string";

const test = mergeWith(
  { a: { b: 'hello' } },
  { a: { b: ' world', c: 123 }, e: false },
  { a: { b: S.Semigroup } }
)
/* test = {
  a: {
      b: "hello world",
      c: 123
  },
  e: false
} */ 

runnable implementation (playground)

maybe a good candidate for a 'Struct' module, as per https://github.com/samhh/fp-ts-std/issues/110?

anthonyjoeseph avatar Mar 25 '22 15:03 anthonyjoeseph

Deeply merging makes me nervous knowing TypeScript.

I wonder if there might be value in a PartialSemigroup typeclass. It'd be bridging the same gap but potentially in a more general way. The desired function would be provided by concat itself.

If Semigroup is a binary operation on A, PartialSemigroup would need to operate over an A and a Partial<A>? You lose deep merging.

Though that then makes me question why A isn't using Options at which point it could just use Semigroup. :wink:

samhh avatar Mar 28 '22 13:03 samhh