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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

lens that fails to focus raises no signals #117

Closed
lanyusan opened this issue Apr 27, 2020 · 4 comments
Closed

lens that fails to focus raises no signals #117

lanyusan opened this issue Apr 27, 2020 · 4 comments

Comments

@lanyusan
Copy link

This could be bug or just a design feature that should be improved.

馃悰 Bug report

When use a lens to modify some data, if the path doesn't exist, lens.modify doesn't do anything but return the existing object.

Is this the right behavior?

I think an exception should be thrown or use either to return a left value indicating a lens has failed to focus.

@gcanti
Copy link
Owner

gcanti commented Apr 27, 2020

Please provide a reproducible example

@lanyusan
Copy link
Author

Here you go:

Test code:

import { Lens, fromTraversable, Prism } from "monocle-ts";
import { array } from "fp-ts/lib/Array";

const lensData = {
  a: {
    b: {
      c: {
        d: 3,
      },
    },
    p: [
      {
        id: 1,
        name: "Jack",
      },
      {
        id: 2,
        name: "Jon",
      },
      {
        id: 3,
        name: "Jay",
      },
    ],
  },
};

try {
  const r = Lens.fromProp<any>()("a")
    .composeLens(Lens.fromProp<any>()("p"))
    .composeTraversal(fromTraversable(array)<any>())
    .composePrism(Prism.fromPredicate((child: any) => child.id === 4))
    .modify((child) => ({ ...child, name: "Josh" }))(lensData);

  console.log("Failed prism returned old value silently");
  console.log(JSON.stringify(r, null, 2));

  const r2 = Lens.fromProp<any>()("a")
    .composeLens(Lens.fromProp<any>()("p"))
    .composeTraversal(fromTraversable(array)<any>())
    .composePrism(Prism.fromPredicate((child: any) => child.id === 2))
    .modify((child) => ({ ...child, name: "Josh" }))(lensData);

  console.log("Successfully prism modified value of a -> p -> id = 2 from Jon to Josh");
  console.log(JSON.stringify(r2, null, 2));
} catch (e) {
  console.log("Caught error");
}

run with yarn ts-node test.ts

result with no exception

Failed prism returned old value silently
{
  "a": {
    "b": {
      "c": {
        "d": 3
      }
    },
    "p": [
      {
        "id": 1,
        "name": "Jack"
      },
      {
        "id": 2,
        "name": "Jon"
      },
      {
        "id": 3,
        "name": "Jay"
      }
    ]
  }
}
Successfully prism modified value of a -> p -> id = 2 from Jon to Josh
{
  "a": {
    "b": {
      "c": {
        "d": 3
      }
    },
    "p": [
      {
        "id": 1,
        "name": "Jack"
      },
      {
        "id": 2,
        "name": "Josh"
      },
      {
        "id": 3,
        "name": "Jay"
      }
    ]
  }
}
Done in 0.99s.

@gcanti
Copy link
Owner

gcanti commented Apr 27, 2020

This

const r = Lens.fromProp<any>()("a")
    .composeLens(Lens.fromProp<any>()("p"))
    .composeTraversal(fromTraversable(array)<any>())
    .composePrism(Prism.fromPredicate((child: any) => child.id === 4))

is not a Lens, is a Traversal so is working as expected

@gcanti gcanti closed this as completed Apr 27, 2020
@lanyusan
Copy link
Author

Ok. I thought they were all called lens in a broader scope.

Can I suggest to make an enhancement to raise a signal if the traversal then modify fails to find the element to update, as in relational database, an update sql could return how many rows have been modified?

It would be nice for some use cases.

Thanks.

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