Is there an `id` of `prism' id Just`?

78 views Asked by At

I have a function called:

mapRouteEncoder ::
  Prism' FilePath FilePath ->
  Prism' r1 r2 ->
  (b -> a) ->
  RouteEncoder a r1 ->
  RouteEncoder b r2

This function is often used by specifying "identity" prisms (if we can call it that) in the first two arguments because only the 3rd function is specified, viz.:

    stringRouteEncoder
      & mapRouteEncoder (prism' id Just) (prism' id Just) changeModel

I use optics-core, and is there anything in that library to replace prism' id Just something more idiomatic?

1

There are 1 answers

3
dfeuer On BEST ANSWER

I'm not familiar with the idioms for that package, but I'd be tempted to use castOptic equality to turn the identity Iso into a Prism. That seems to get the idea across pretty well. However, I would expect that in most contexts you wouldn't actually need to convert it to a Prism; you can presumably use polymorphic functions (like preview and review) that will accept an Iso as well as a Prism.

In your case, I suspect the right approach would be to write something like

mapRouteEncoder ::
  ( p1 `Is` A_Prism
  , p2 `Is` A_Prism) =>
  Optic' p1 ixy1 FilePath FilePath ->
  Optic' p2 ixy2 r1 r2 ->
  (b -> a) ->
  RouteEncoder a r1 ->
  RouteEncoder b r2