Add Each interface

This commit is contained in:
Kiana Sheibani 2023-04-25 13:40:03 -04:00
parent 810404b305
commit 4c19ed5209
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
7 changed files with 69 additions and 5 deletions

View file

@ -2,6 +2,7 @@ module Control.Lens
import public Control.Lens.At
import public Control.Lens.Cons
import public Control.Lens.Each
import public Control.Lens.Equality
import public Control.Lens.Fold
import public Control.Lens.Getter

34
src/Control/Lens/Each.idr Normal file
View file

@ -0,0 +1,34 @@
module Control.Lens.Each
import Control.Monad.Identity
import Control.Applicative.Const
import Control.Lens.Optic
import Control.Lens.Iso
import Control.Lens.Lens
import Control.Lens.Optional
import Control.Lens.Traversal
%default total
||| An interface for accessing every element of a container.
|||
||| This can be thought of as a generalized version of `traversed` for
||| containers that do not have a `Traversable` implementation.
public export
interface Each s t a b | s where
||| Access every element of a container at the same time.
|||
||| This can be thought of as a generalized version of `traversed` for
||| containers that do not have a `Traversable` implementation.
each : Traversal s t a b
public export
Each (Identity a) (Identity b) a b where
each = Id_
public export
Each (Const a b) (Const c d) a c where
each = Const_

View file

@ -4,6 +4,8 @@ import Data.Maybe
import Data.Contravariant
import Data.Tensor
import Data.Profunctor
import Control.Monad.Identity
import Control.Applicative.Const
import Control.Lens.Optic
import Control.Lens.Equality
@ -145,6 +147,15 @@ non : Eq a => a -> Iso' (Maybe a) a
non x = iso (fromMaybe x) (\y => guard (x /= y) $> y)
public export
Id_ : Iso (Identity a) (Identity b) a b
Id_ = iso runIdentity Id
public export
Const_ : Iso (Const a b) (Const c d) a c
Const_ = iso runConst MkConst
-- Mapping
||| Lift an isomorphism through a `Functor`.