Skip to content

add icompose #55

Open
Open
@Roxxik

Description

@Roxxik

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)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions