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

add icompose #55

Open
Roxxik opened this issue Jan 23, 2017 · 3 comments
Open

add icompose #55

Roxxik opened this issue Jan 23, 2017 · 3 comments

Comments

@Roxxik
Copy link
Contributor

Roxxik commented Jan 23, 2017

i just found some functions that might be worth being added. But i don't really know where to add them and don't have the time to prepare a PR. So here we go:

mapIndex :: forall i j s t a b.
  (i -> j) -> IndexedTraversal i s t a b -> IndexedTraversal j s t a b
mapIndex fi tr = iwander \f -> itraverseOf tr (f <<< fi)

icompose :: forall i j k s' t' s t a b.
  (i -> j -> k) ->
  IndexedTraversal i s' t' s t ->
  IndexedTraversal j s t a b ->
  IndexedTraversal k s' t' a b
icompose ijk trO trI = iwander \f -> itraverseOf trO \i -> itraverseOf trI \j -> f (ijk i j)

icompose is inspired by
http://hackage.haskell.org/package/lens-4.15.1/docs/Control-Lens-Indexed.html#v:icompose

this way it's possible to combine IndexedTraversals:

test :: Array (Array Int)
test = [[1,2,3],[4,5,6]]

nested :: forall t1 t2 a b. (Traversable t1, Traversable t2) => IndexedTraversal (Tuple Int Int) (t1 (t2 a)) (t1 (t2 b)) a b
nested = icompose Tuple (positions traversed) (positions traversed)

comTest :: List (Tuple (Tuple Int Int) Int)
comTest = itoListOf nested test

-- comTest = ((Tuple (Tuple 0 0) 1) : (Tuple (Tuple 0 1) 2) : (Tuple (Tuple 0 2) 3) : (Tuple (Tuple 1 0) 4) : (Tuple (Tuple 1 1) 5) : (Tuple (Tuple 1 2) 6) : Nil)
-- or prettied up: [((0,0),1), ((0,1),2), ((0,2),3), ((1,0),4), ((1,1),5), ((1,2),6)]
@paf31
Copy link
Contributor

paf31 commented Jan 23, 2017

👍 Sounds good, although maybe the implementation could be simpler?

@Roxxik
Copy link
Contributor Author

Roxxik commented Jan 30, 2017

i just looked at it again, and it's type is the original icompose specialized to IndexedTraversals. So the story for all the other indexed optics is missing. some more specialized combinators could be written, but what about combining an indexed lens with an indexed Traversal? I think some general combinator for IndexedOptics is needed.

@UltimateDude101
Copy link

i just looked at it again, and it's type is the original icompose specialized to IndexedTraversals. So the story for all the other indexed optics is missing. some more specialized combinators could be written, but what about combining an indexed lens with an indexed Traversal? I think some general combinator for IndexedOptics is needed.

I think the following works, though it might be able to be simplified further.

icompose :: forall p i j k s' t' s t a b. Profunctor p => 
  (i -> j -> k) ->
  IndexedOptic p i s' t' s t ->
  IndexedOptic (Indexed p i) j s t a b ->
  IndexedOptic p k s' t' a b
icompose f x y = x <<< y <<< Indexed <<< Indexed <<< lcmap (\(Tuple a (Tuple b c)) -> Tuple (f a b) c) <<< un Indexed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants