putlenses-0.1.1: Put-based lens library

Stabilityprovisional
MaintainerHugo Pacheco <hpacheco@nii.ac.jp>
Safe HaskellNone

Generics.Putlenses.Examples.Examples

Contents

Description

Collection of put-based programming examples

Synopsis

Lists

unzipPut :: (Monad m, Eq a, Eq b) => PutlensM m ([a], [b]) [(a, b)]Source

Unzips a list into two lists (creating empty tail lists by default)

mapPut :: (Monad m, Eq a) => PutlensM m b a -> PutlensM m [b] [a]Source

Putlens version of map

unfoldrPut :: (Monad m, Eq a, Eq b) => PutlensM m (b, a) a -> a -> PutlensM m [b] aSource

Putlens version of foldr

unfoldrsPut :: (Monad m, Eq a, Eq b) => PutlensM m (b, a) a -> a -> PutlensM m [b] aSource

Variant of unfoldrPut that tries to present the original source branching even when the view value matches the stop condition Useful for example for cases when we want to always preserve the length of the original source list.

Constructor/destructor putlenses for lists

nilPut :: Monad m => PutlensM m [a] ()Source

consPut :: Monad m => PutlensM m [a] (a, [a])Source

unnilPut :: Monad m => PutlensM m () [a]Source

unconsPut :: Monad m => PutlensM m (a, [a]) [a]Source

unheadPut :: (Monad m, Eq a) => PutlensM m [a] aSource

untailPut :: (Monad m, Eq a) => PutlensM m [a] [a]Source

keepfstPut2 :: (Monad m, Eq v) => PutlensM m (s1, v) vSource

wrapPut :: (Monad m, Eq a) => PutlensM m [a] aSource

unwrapPut :: (Monad m, Eq a) => PutlensM m a [a]Source

example of automatically deriving constructor/destructor putlenses for binary trees

data Tree a Source

Constructors

Empty 
Node a (Tree a) (Tree a) 

Instances

unnodePut :: forall m a. Monad m => PutlensM m (a, (Tree a, Tree a)) (Tree a)Source

nodePut :: forall m a. Monad m => PutlensM m (Tree a) (a, (Tree a, Tree a))Source

unemptyPut :: forall m a. Monad m => PutlensM m () (Tree a)Source

emptyPut :: forall m a. Monad m => PutlensM m (Tree a) ()Source

List concatenation

catPut :: (Monad m, Eq a) => PutlensM m ([a], [a]) [a]Source

List concatenation (positional)

exCatPut2 :: Identity ([Integer], [Integer])Source

exCatPut3 :: Identity ([Integer], [Integer])Source

exCatPut4 :: Identity ([Integer], [Integer])Source

catPutN :: (Monad m, Eq a) => (Maybe ([a], [a]) -> [a] -> Int) -> PutlensM m ([a], [a]) [a]Source

List concatenation (split the view list at position n-1)

catPutN' :: (Monad m, Eq a) => PutlensStateM m Int ([a], [a]) [a]Source

catPut2 :: PutlensM Identity ([Integer], [Integer]) [Integer]Source

List concatenation (split the view list in half)

exCatPut22 :: Identity ([Integer], [Integer])Source

exCatPut23 :: Identity ([Integer], [Integer])Source

exCatPut24 :: Identity ([Integer], [Integer])Source

catPutP :: (Monad m, Eq a) => (Maybe (Either [a] [a]) -> a -> m Bool) -> PutlensM m ([a], [a]) [a]Source

List concatenation (puts elements to the left while satisfying a predicate)

catPutSame :: (Monad m, Eq a) => PutlensM m ([a], [a]) [a]Source

List concatenation (puts elements to the left while being equal)

catPutPred :: (Monad m, Ord a) => a -> PutlensM m ([a], [a]) [a]Source

List concatenation (puts elements to the left while smaller than a particular value)

filterlPut :: (Monad m, Eq a, Eq b) => PutlensM m [Either a b] [a]Source

Left list filtering lens (but will drop some right elements if the view list is smaller). The argument passed to keepfstOrPut can be undefined because it will never be used

filterrPut :: (Monad m, Eq a, Eq b) => PutlensM m [Either a b] [b]Source

Right list filtering lens (but will drop some left elements if the view list is smaller). The argument passed to keepsndOrPut can be undefined because it will never be used

filterPut :: (Monad m, Eq a, Eq b) => Bool -> PutlensM m [Either a b] ([a], [b])Source

List filtering lens that splits a list of eithers into a list two lists. The boolean argument allows controlling the priority of left/right values in the source list.

filterleftPut :: (Monad m, Eq a, Eq b) => PutlensM m [Either a b] [a]Source

Left list filtering lens (that recovers all right elements).

filterrightPut :: (Monad m, Eq a, Eq b) => PutlensM m [Either a b] [b]Source

Right list filtering lens (that recovers all left elements).

Integers

Naturals

data Nat Source

Constructors

ZeroN 
SuccN Nat 

Instances

unsuccNPut :: forall m. Monad m => PutlensM m Nat NatSource

succNPut :: forall m. Monad m => PutlensM m Nat NatSource

unzeroNPut :: forall m. Monad m => PutlensM m () NatSource

zeroNPut :: forall m. Monad m => PutlensM m Nat ()Source

List length

lengthNatPut :: (Monad m, Eq a) => (Int -> a) -> PutlensM m [a] NatSource

Length as a natural number

List lookup

embedAtPut :: (Monad m, Eq a) => Int -> PutlensM m [a] aSource

Embeds a value at a fixed position in a list

embedAtPut1 :: (Monad m, Eq a) => Int -> PutlensM m [a] aSource

Embeds a value at a fixed position in a list (supports extending the length original list)

embedAtPut2 :: (Monad m, Eq a) => Int -> PutlensM m [a] aSource

Embeds a value at a fixed position in a list (source induction)

embedAtPut2' :: (Monad m, Eq a) => PutlensM m (Int, [a]) aSource

embedAtPut3 :: (Monad m, Eq a) => Int -> PutlensM m [a] aSource

Embeds a value at a fixed position in a list (supports extending the length original list) (source induction)

embedAtPut3' :: (Monad m, Eq a) => PutlensStateM m a (Int, [a]) aSource

embedAtPut4 :: (Monad m, Eq a) => Int -> PutlensM m [a] aSource

Embeds a value at a fixed position in a list (splitAt approach)

splitAtPut :: (Monad m, Eq a) => PutlensM m (Int, [a]) ([a], [a])Source

List summation

splitPut :: (Monad m, Integral a) => (a -> a -> m a) -> PutlensM m (a, a) aSource

Splits a view number into two summands (by adding an offset to the original first value)

summandsPut1 :: (Monad m, Integral a) => PutlensM m [a] aSource

Updates the sum of a list (preserves the original source and appends the difference to the end)

summandsPut2 :: (Monad m, Integral a) => PutlensM m [a] aSource

Updates the sum of a list (distributes the difference by dividing it by two at each recursive step) half of the difference is added to the first element of the source, a quarter to the second, and so on until the remainder is 0

summandsPut3 :: (Monad m, Integral a) => PutlensM m [a] aSource

Updates the sum of a list (distributes the difference by dividing it by the length of the original list) distributes the difference evenly among original list numbers

summandsPut4 :: (Monad m, Integral a) => PutlensM m [a] aSource

Updates the sum of a list (distributes the difference by dividing it by the length of the original list, always preserving the size f the original list even when the view is zero)

Replicate

replicatePut :: (Monad m, Eq a) => PutlensM m (a, Int) [a]Source

Replicate

replicateListPut :: (Monad m, Eq a) => PutlensM m [(a, Int)] [a]Source

Replicates a list of elements into a sequence of replicated elements

recoverzerosPut :: (Monad m, Eq a) => PutlensM m [(a, Int)] [(a, Int)]Source

splitListPut :: (Monad m, Eq a) => PutlensM m [[a]] [a]Source

Halve

halvePut :: (Monad m, Eq a) => a -> PutlensM m [a] [a]Source

Takes the first half of a list (with a default empty element)

exHalvePut1 :: Identity [Char]Source

exHalvePut2 :: Identity [Char]Source

halvePut2 :: (Monad m, Eq a) => a -> PutlensM m [a] [a]Source

Takes the first half of a list (using an increasing counter with each consumed element in the forward direction)

halvePut2' :: (Monad m, Eq a) => a -> PutlensM m ([a], Int) ([a], Int)Source

exHalvePut21 :: Identity [Char]Source

exHalvePut22 :: Identity [Char]Source

exHalvePut23 :: Identity [Char]Source

halvePut3 :: (Monad m, Eq a) => a -> PutlensM m [a] [a]Source

Takes the first half of a list (using a decreasing counter with the size of the input list, decreased 2 by 2, in the forward direction)

halvePut3' :: (Monad m, Eq a) => a -> PutlensStateM m Int ([a], Int) [a]Source

exHalvePut31 :: Identity [Char]Source

exHalvePut32 :: Identity [Char]Source

exHalvePut33 :: Identity [Char]Source

isumPut :: Monad m => PutlensM m [Int] [Int]Source

Incremental summation

exIsumPut2 :: Identity [Int]Source

Sorting

iunsortPut1 :: (Monad m, Ord a) => PutlensM m [a] [a]Source

Insertion sort (put according to the original relative source order)

delPut1 :: (Monad m, Ord a) => PutlensM m (a, [a]) [a]Source

iunsortPut2 :: (Monad m, Ord a) => PutlensM m [a] [a]Source

Insertion sort (identity backward transformation)

delPut2 :: (Monad m, Ord a) => PutlensM m (a, [a]) [a]Source

qsortPut :: (Monad m, Ord a) => PutlensM m [a] [a]Source

Quicksort (put according to the original relative source order)

partitionPut :: (Monad m, Ord a) => PutlensM m (a, [a]) (a, ([a], [a]))Source

Partition a list into smaller and bigger elements than a given element

catPutNonEmptyRight :: (Monad m, Eq a) => PutlensM m ([a], [a]) [a]Source

positionsPut :: (Monad m, Eq a) => PutlensM m [a] [(Pos, a)]Source

Adds positions to a list (using an higher-order function)

positionsPut' :: (Monad m, Eq a) => Pos -> PutlensM m [a] [(Pos, a)]Source

positionsPut2 :: (Monad m, Eq a) => PutlensM m [a] [(Pos, a)]Source

Adds positions to a list (using environment)

positionsPut2' :: (Monad m, Eq a) => PutlensReaderM m [(Pos, a)] (Int, [a]) [(Pos, a)]Source

Exception handling

appendWithSepPut :: Monad m => String -> PutlensM m (String, String) StringSource

Appends two strings with a separating space

readPut :: (MonadPlus m, Read a, Show a) => PutlensM m a StringSource

Parsingpretty-printing lens version using @read@show

unwordsIntBool :: PutlensM Maybe [Either Int Bool] StringSource

Parses a string into a sequence of Int or Bool values separated by a single space

unwordsPut :: PutlensM Maybe [String] StringSource

Putlens inverse of unwords

unfoldr1Put :: (MonadPlus m, Eq a) => PutlensM m (a, a) a -> PutlensM m [a] aSource

Putlens inverse of foldr1, a specialization of foldr for non-empty lists

Maximum segment sum

type Pos = IntSource

segments :: [a] -> [[a]]Source

mssPut :: Monad m => PutlensM m [Int] IntSource

Updating maximum segment sum when the sum increases, update only the largest segment when the sum decreases, update all segments that surpass the new maximum, from smallest to largest

mssPut' :: Monad m => Bool -> PutlensM (ReaderT Int m) [Int] IntSource

lookupSegPut :: (Monad m, Eq a) => PutlensM m (Map Int a, [Int]) [a]Source

fromListPut :: (Monad m, Eq a, Ord k) => PutlensM m [(k, a)] (Map k a)Source

toListPut :: (Monad m, Eq a, Ord k) => PutlensM m (Map k a) [(k, a)]Source