profunctor-optics-0.0.1: An optics library compatible with the typeclasses in 'profunctors'.

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.View

Contents

Synopsis

Types

type View s a = forall p. (Strong p, forall x. Contravariant (p x)) => Optic' p s a Source #

type Ixview i s a = forall p. (Strong p, forall x. Contravariant (p x)) => IndexedOptic' p i s a Source #

type PrimView s t a b = forall p. (Profunctor p, forall x. Contravariant (p x)) => Optic p s t a b Source #

type Review t b = forall p. (Closed p, Bifunctor p) => Optic' p t b Source #

type Cxview k t b = forall p. (Closed p, Bifunctor p) => CoindexedOptic' p k t b Source #

type PrimReview s t a b = forall p. (Profunctor p, Bifunctor p) => Optic p s t a b Source #

Constructors

to :: (s -> a) -> PrimView s t a b Source #

Obtain a View from an arbitrary function.

to f . to g ≡ to (g . f)
a ^. to f ≡ f a
>>> ("hello","world") ^. to snd
"world"
>>> 5 ^. to succ
6
>>> (0, -5) ^. second' . to abs
5
to :: (s -> a) -> View s a

ito :: (s -> (i, a)) -> Ixview i s a Source #

TODO: Document

from :: (b -> t) -> PrimReview s t a b Source #

Obtain a Review from an arbitrary function.

fromre . to
>>> (from Prelude.length) #^ [1,2,3]
3
from :: (b -> t) -> Review t b

kfrom :: ((k -> b) -> t) -> Cxview k t b Source #

TODO: Document

cloneView :: AView s a -> PrimView s s a a Source #

TODO: Document

cloneView ::             AView s a -> View s a
cloneView :: Monoid a => AView s a -> Fold s a

cloneReview :: AReview t b -> PrimReview t t b b Source #

TODO: Document

Optics

like :: a -> PrimView s t a b Source #

Obtain a constant-valued (index-preserving) View from an arbitrary value.

This can be useful as a second case failing a Fold e.g. foo failing like 0

like a . like b ≡ like b
a ^. like b ≡ b
a ^. like b ≡ a ^. to (const b)
like :: a -> View s a

ilike :: i -> a -> Ixview i s a Source #

TODO: Document

relike :: t -> PrimReview s t a b Source #

Obtain a constant-valued (index-preserving) Review from an arbitrary value.

relike a . relike b ≡ relike a
relike a # b ≡ a
relike a # b ≡ from (const a) # b

klike :: t -> Cxview k t b Source #

Obtain a constant-valued Cxview from an arbitrary value.

toProduct :: AView s a1 -> AView s a2 -> PrimView s t (a1, a2) b Source #

Combine two Views into a View to a product.

toProduct :: View s a1 -> View s a2 -> View s (a1 , a2)

fromSum :: AReview t b1 -> AReview t b2 -> PrimReview s t a (b1 + b2) Source #

Combine two Reviews into a Review from a sum.

fromSum :: Review t b1 -> Review t b2 -> Review t (b1 + b2)

Primitive operators

withPrimView :: APrimView r s t a b -> (a -> r) -> s -> r Source #

TODO: Document

withPrimReview :: APrimReview s t a b -> (t -> r) -> b -> r Source #

TODO: Document

Operators

(^.) :: s -> AView s a -> a infixl 8 Source #

View the focus of an optic.

Fixity and semantics are such that subsequent field accesses can be performed with (.).

>>> ("hello","world") ^. second'
"world"
>>> 5 ^. to succ
6
>>> import Data.Complex
>>> ((0, 1 :+ 2), 3) ^. first' . second' . to magnitude
2.23606797749979

(^%) :: (Additive - Monoid) i => s -> AIxview i s a -> (Maybe i, a) infixl 8 Source #

View the focus of an indexed optic along with its index.

>>> ("foo", 42) ^% ifirst
(Just (),"foo")

view :: MonadReader s m => AView s a -> m a Source #

A prefix alias for ^..

view . toid
>>> view second' (1, "hello")
"hello"
>>> view (to succ) 5
6
>>> view (second' . first') ("hello",("world","!!!"))
"world"

iview :: MonadReader s m => (Additive - Monoid) i => AIxview i s a -> m (Maybe i, a) Source #

A prefix alias for ^%.

>>> iview ifirst ("foo", 42)
(Just (),"foo")

views :: MonadReader s m => Optic' (Star (Const r)) s a -> (a -> r) -> m r Source #

Map each part of a structure viewed to a semantic editor combinator.

'views o f ≡ withFold o f'
foldMap = views folding'
>>> views both id (["foo"], ["bar", "baz"])
["foo","bar","baz"]

iviews :: MonadReader s m => (Additive - Monoid) i => IndexedOptic' (Star (Const r)) i s a -> (i -> a -> r) -> m r Source #

Bring a function of the index and value of an indexed optic into the current environment.

iviewsiwithFold

Use iview if there is a need to disambiguate between zero as a miss vs. as a return value.

use :: MonadState s m => AView s a -> m a Source #

TODO: Document

iuse :: MonadState s m => (Additive - Monoid) i => AIxview i s a -> m (Maybe i, a) Source #

Bring the index and value of an indexed optic into the current environment as a pair.

uses :: MonadState s m => Optic' (Star (Const r)) s a -> (a -> r) -> m r Source #

Use the target of a Lens, Iso or View in the current state, or use a summary of a Fold or Traversal that points to a monoidal value.

>>> evalState (uses first' length) ("hello","world!")
5

iuses :: MonadState s m => (Additive - Monoid) i => IndexedOptic' (Star (Const r)) i s a -> (i -> a -> r) -> m r Source #

Bring a function of the index and value of an indexed optic into the current environment.

(#^) :: AReview t b -> b -> t infixr 8 Source #

Dual to ^..

from f #^ x ≡ f x
o #^ x ≡ x ^. re o

This is commonly used when using a Prism as a smart constructor.

>>> left' #^ 4
Left 4

review :: MonadReader b m => AReview t b -> m t Source #

A prefix alias of #^.

reviewview . re
review . fromid
>>> review (from succ) 5
6

kview :: MonadReader b m => ACxview k t b -> m (k -> t) Source #

Bring a function of the index of a co-indexed optic into the current environment.

reviews :: MonadReader b m => AReview t b -> (t -> r) -> m r Source #

Turn an optic around and look through the other end, applying a function.

reviewsviews . re
reviews (from f) g ≡ g . f
>>> reviews left' isRight "mustard"
False
>>> reviews (from succ) (*2) 3
8

kviews :: MonadReader b m => ACxview k t b -> ((k -> t) -> r) -> m r Source #

Bring a continuation of the index of a co-indexed optic into the current environment.

kviews :: ACxview k t b -> ((k -> t) -> r) -> b -> r

reuse :: MonadState b m => AReview t b -> m t Source #

Turn an optic around and use a value (or the current environment) through it the other way.

reuseuse . re
reuse . fromgets
>>> evalState (reuse left') 5
Left 5
>>> evalState (reuse (from succ)) 5
6

reuses :: MonadState b m => AReview t b -> (t -> r) -> m r Source #

Turn an optic around and use the current state through it the other way, applying a function.

reusesuses . re
reuses (from f) g ≡ gets (g . f)
>>> evalState (reuses left' isLeft) (5 :: Int)
True

kuse :: MonadState b m => ACxview k t b -> m (k -> t) Source #

TODO: Document

kuses :: MonadState b m => ACxview k t b -> ((k -> t) -> r) -> m r Source #

TODO: Document

MonadIO

throws :: MonadIO m => Exception e => AReview e b -> b -> m r Source #

Throw an exception described by an optic.

throws o e `seq` x  ≡ throws o e

throws_ :: MonadIO m => Exception e => AReview e () -> m r Source #

Variant of throws for error constructors with no arguments.

throwsTo :: MonadIO m => Exception e => ThreadId -> AReview e b -> b -> m () Source #

Raise an Exception specified by an optic in the target thread.

throwsTo thread o ≡ throwTo thread . review o