Effect CommunityEC
Effect Community2y ago
17 replies
nemmind1

Seeking help with using @fp-ts/optic package for refactoring fp-ts projects

Hi! I'm in progress of refactoring all my fp-ts projects to effect. One of these projects heavily use monocle-ts traversal functionality. I've seen the new @fp-ts/optic package has also the traversal functionality, but cannot make it work. Can someone help me with the following code?

import { pipe } from 'effect'; // v2.1.0
import * as Optic from '@fp-ts/optic'; // v0.22.0

interface TestInner {
  readonly value: number;
}
interface Test {
  readonly map: Readonly<Record<string, TestInner>>;
  readonly array: readonly TestInner[];
}

const test: Test = {
  map: {
    foo: { value: 1 },
    bar: { value: 2 },
  },
  array: [{ value: 3 }, { value: 4 }],
};

const double = (n: number) => n * 2;

const mapValuesOptic = Optic.id<Test>().at('map'); // TODO: traversal on all map values
const arrayValuesOptic = Optic.id<Test>().at('array'); // TODO: traversal on all array values

const actual = pipe(test, Optic.modify(mapValuesOptic)(double), Optic.modify(arrayValuesOptic)(double));

const expected: Test = {
  map: {
    foo: { value: 2 },
    bar: { value: 4 },
  },
  array: [{ value: 6 }, { value: 8 }],
};
Was this page helpful?