Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modifyF for Lens/Optional/Ix/ ... #149

Closed
jituanlin opened this issue Jan 22, 2021 · 1 comment
Closed

modifyF for Lens/Optional/Ix/ ... #149

jituanlin opened this issue Jan 22, 2021 · 1 comment
Assignees

Comments

@jituanlin
Copy link

For now, monocle-ts only support modifyF in Traversal(even in v3.0.0 branch).

But modifyF is required to make modify context, for example:
implementation:

import * as M from 'monocle-ts';
import * as F from 'fp-ts';

export const modifyF4Lens = <F extends F.hkt.URIS>(
  functor: F.functor.Functor1<F>
) => <A>(f: (a: A) => F.hkt.Kind<F, A>) => <S>(lens: M.Lens<S, A>) => (
  s: S
): F.hkt.Kind<F, S> => {
  return F.function.pipe(lens.get(s), f, fa =>
    functor.map(fa, (a: A) => lens.set(a)(s))
  );
};

testing:

import * as F from 'fp-ts';
import * as M from 'monocle-ts';
import {modifyF4Lens} from './index';

describe('modify f', () => {
  test('modifyF in Lens', () => {
    interface Account {
      name: string;
    }

    const nameM: M.Lens<Account, string> = M.Lens.fromProp<Account>()('name');
    const nameModified = modifyF4Lens(F.option.Functor)(name =>
      F.option.some(`${name}Modified`)
    )(nameM)({name: 'monocle'});

    expect(nameModified).toEqual(F.option.some({name: 'monocleModified'}));
  });
});
@gcanti
Copy link
Owner

gcanti commented Jan 22, 2021

@jituanlin I will add modifyF to the experimental modules

@gcanti gcanti self-assigned this Jan 22, 2021
gcanti added a commit that referenced this issue Jan 22, 2021
@gcanti gcanti closed this as completed in 5c9d692 Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants