Safe Haskell | None |
---|---|
Language | Haskell2010 |
promoted iterator functions
Synopsis
- data Scanl p q r
- data ScanN n p q
- data ScanNA q
- data FoldN n p q
- data Foldl p q r
- data Unfoldr p q
- data IterateUntil p f
- data IterateWhile p f
- data IterateNWhile n p f
- data IterateNUntil n p f
- data UnfoldN n p s
- data Para (ps :: [k])
- data ParaN (n :: Nat) p
- data DoN (n :: Nat) p
- data Repeat (n :: Nat) p
Documentation
similar to scanl
>>>
pz @(Scanl (Snd :+ Fst) Fst Snd) ([99],[1..5])
Val [[99],[1,99],[2,1,99],[3,2,1,99],[4,3,2,1,99],[5,4,3,2,1,99]]
>>>
pl @(Scanl (Snd :+ Fst) Fst Snd) ([99],[])
Present [[99]] (Scanl [[99]] | b=[99] | as=[]) Val [[99]]
iterates n times keeping all the results
>>>
pz @(ScanN 4 Succ Id) 'c'
Val "cdefg"
>>>
pz @(Dup >> ScanN 4 (Pred *** Succ) Id) 'g'
Val [('g','g'),('f','h'),('e','i'),('d','j'),('c','k')]
>>>
pz @(ScanN 4 Succ Id) 4
Val [4,5,6,7,8]
>>>
pz @('(0,1) >> ScanN 20 '(Snd, Fst + Snd) Id >> Map Fst) "sdf"
Val [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765]
>>>
pl @(ScanN 2 Succ Id) 4
Present [4,5,6] (Scanl [4,5,6] | b=4 | as=[1,2]) Val [4,5,6]
>>>
pl @(ScanN 5 Id Id) 4
Present [4,4,4,4,4,4] (Scanl [4,4,4,4,4,4] | b=4 | as=[1,2,3,4,5]) Val [4,4,4,4,4,4]
>>>
pl @(ScanN 2 Succ Id >> PadR 10 (MEmptyT Ordering) Id) LT
Present [LT,EQ,GT,EQ,EQ,EQ,EQ,EQ,EQ,EQ] ((>>) [LT,EQ,GT,EQ,EQ,EQ,EQ,EQ,EQ,EQ] | {PadR 10 pad=EQ [LT,EQ,GT,EQ,EQ,EQ,EQ,EQ,EQ,EQ] | [LT,EQ,GT]}) Val [LT,EQ,GT,EQ,EQ,EQ,EQ,EQ,EQ,EQ]
>>>
pl @(ScanN 4 Pred Id) 99
Present [99,98,97,96,95] (Scanl [99,98,97,96,95] | b=99 | as=[1,2,3,4]) Val [99,98,97,96,95]
tuple version of ScanN
>>>
pl @(ScanNA Succ) (4,'a')
Present "abcde" (Scanl "abcde" | b='a' | as=[1,2,3,4]) Val "abcde"
>>>
pl @(ScanNA Tail) (4,"abcd")
Present ["abcd","bcd","cd","d",""] (Scanl ["abcd","bcd","cd","d",""] | b="abcd" | as=[1,2,3,4]) Val ["abcd","bcd","cd","d",""]
>>>
pl @(Len &&& Id >> ScanNA Tail) "abcd"
Present ["abcd","bcd","cd","d",""] ((>>) ["abcd","bcd","cd","d",""] | {Scanl ["abcd","bcd","cd","d",""] | b="abcd" | as=[1,2,3,4]}) Val ["abcd","bcd","cd","d",""]
iterates n times keeping only the last result
>>>
pz @(FoldN 4 Succ Id) 'c'
Val 'g'
>>>
pz @(ReadP Day Id >> Id ... FoldN 5 Succ Id) "2020-07-27"
Val [2020-07-27,2020-07-28,2020-07-29,2020-07-30,2020-07-31,2020-08-01]
>>>
pl @(FoldN 2 Succ Id) LT
Present GT ((>>) GT | {Last GT | [LT,EQ,GT]}) Val GT
>>>
pl @(FoldN 30 Succ Id) LT
Error Succ IO e=Prelude.Enum.Ordering.succ: bad argument (Scanl) Fail "Succ IO e=Prelude.Enum.Ordering.succ: bad argument"
>>>
pl @(FoldN 6 Succ Id) 'a'
Present 'g' ((>>) 'g' | {Last 'g' | "abcdefg"}) Val 'g'
>>>
pl @(FoldN 6 Pred Id) 'a'
Present '[' ((>>) '[' | {Last '[' | "a`_^]\\["}) Val '['
>>>
pl @(FoldN 0 Succ Id) LT
Present LT ((>>) LT | {Last LT | [LT]}) Val LT
>>>
pl @(FoldN 2 Succ Id >> FoldN 2 Pred Id) LT
Present LT ((>>) LT | {Last LT | [GT,EQ,LT]}) Val LT
>>>
pz @(FoldN 4 (Id <> Id) Id) "abc" -- same as above
Val "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
Foldl similar to foldl
>>>
pl @(Foldl (Fst + Snd) 0 (1 ... 10)) ()
Present 55 ((>>) 55 | {Last 55 | [0,1,3,6,10,15,21,28,36,45,55]}) Val 55
>>>
pz @(Foldl (Snd :+ Fst) '[99] (1 ... 10)) ()
Val [10,9,8,7,6,5,4,3,2,1,99]
>>>
pl @(Foldl Fst '() (EnumFromTo 1 9999)) ()
Error Scanl list size exceeded (max is 100) Fail "Scanl list size exceeded"
>>>
pl @(Foldl (Guard "someval" (Fst < Snd) >> Snd) Head Tail) [1,4,7,9,16]
Present 16 ((>>) 16 | {Last 16 | [1,4,7,9,16]}) Val 16
>>>
pl @(Foldl (Guard (PrintT "%d not less than %d" Id) (Fst < Snd) >> Snd) Head Tail) [1,4,7,6,16]
Error 7 not less than 6 (Scanl) Fail "7 not less than 6"
>>>
pl @(Foldl (If (L11 && (Snd > L12)) '( 'True, Snd) '( 'False, L12)) '( 'True, Head) Tail) [1,4,7,9,16]
Present (True,16) ((>>) (True,16) | {Last (True,16) | [(True,1),(True,4),(True,7),(True,9),(True,16)]}) Val (True,16)
>>>
pl @(Foldl (If (L11 && (Snd > L12)) '( 'True, Snd) '( 'False, L12)) '( 'True, Head) Tail) [1,4,7,9,16,2]
Present (False,16) ((>>) (False,16) | {Last (False,16) | [(True,1),(True,4),(True,7),(True,9),(True,16),(False,16)]}) Val (False,16)
>>>
pl @(Foldl (Snd :+ Fst) (MEmptyT [_]) Id) [1..5]
Present [5,4,3,2,1] ((>>) [5,4,3,2,1] | {Last [5,4,3,2,1] | [[],[1],[2,1],[3,2,1],[4,3,2,1],[5,4,3,2,1]]}) Val [5,4,3,2,1]
>>>
pl @('Just Uncons >> Foldl (If L11 (If (L12 < Snd) '( 'True,Snd) '( 'False, Snd)) Fst) '( 'True,Fst) Snd) [-10,-2,2,3,4,10,9,11]
Present (False,9) ((>>) (False,9) | {Last (False,9) | [(True,-10),(True,-2),(True,2),(True,3),(True,4),(True,10),(False,9),(False,9)]}) Val (False,9)
>>>
pl @('Just Uncons >> Foldl (If L11 (If (L12 < Snd) '( 'True,Snd) '( 'False, Snd)) Fst) '( 'True,Fst) Snd) [-10,2,3,4,10,11]
Present (True,11) ((>>) (True,11) | {Last (True,11) | [(True,-10),(True,2),(True,3),(True,4),(True,10),(True,11)]}) Val (True,11)
similar to unfoldr
>>>
pz @(Unfoldr (MaybeBool (Not Null) (SplitAt 2 Id)) Id) [1..5]
Val [[1,2],[3,4],[5]]
>>>
pl @(Unfoldr (If Null (MkNothing _) ('(Take 3 Id, Drop 1 Id) >> MkJust Id)) Id) "abcdefghi"
Present ["abc","bcd","cde","def","efg","fgh","ghi","hi","i"] (Unfoldr "abcdefghi" ["abc","bcd","cde","def","efg","fgh","ghi","hi","i"] | s="abcdefghi") Val ["abc","bcd","cde","def","efg","fgh","ghi","hi","i"]
>>>
pl @(Unfoldr (If Null (MkNothing _) (Pure _ (SplitAt 2 Id))) Id) [1..5]
Present [[1,2],[3,4],[5]] (Unfoldr [1,2,3,4,5] [[1,2],[3,4],[5]] | s=[1,2,3,4,5]) Val [[1,2],[3,4],[5]]
>>>
pl @(Unfoldr (MaybeBool (Not Null) (SplitAt 2 Id)) Id) [1..5]
Present [[1,2],[3,4],[5]] (Unfoldr [1,2,3,4,5] [[1,2],[3,4],[5]] | s=[1,2,3,4,5]) Val [[1,2],[3,4],[5]]
>>>
pl @(Unfoldr (If Null (MkNothing _) (Guard "yy" (Len < 3) >> Pure _ (SplitAt 2 Id))) Id) [1..5]
Error yy (Unfoldr [1,2,3,4,5]) Fail "yy"
>>>
pl @(Unfoldr (MaybeBool (Not Null) (Guard "yy" (Len < 3) >> SplitAt 2 Id)) Id) [1..5]
Error yy (Unfoldr [1,2,3,4,5]) Fail "yy"
>>>
pl @(Unfoldr (Guard "xx" (Len > 4) >> Uncons) Id) [1..10]
Error xx (Unfoldr [1,2,3,4,5,6,7,8,9,10]) Fail "xx"
>>>
pl @(Unfoldr Uncons Id) [1..10]
Present [1,2,3,4,5,6,7,8,9,10] (Unfoldr [1,2,3,4,5,6,7,8,9,10] [1,2,3,4,5,6,7,8,9,10] | s=[1,2,3,4,5,6,7,8,9,10]) Val [1,2,3,4,5,6,7,8,9,10]
>>>
pan @(Unfoldr (If (Id < 1) (MkNothing _) (MkJust (DivMod Id 2 >> Swap))) Id) 8
P Unfoldr 8 [0,0,0,1] | +- P Id 8 | +- P i=1: If 'False Just (0,4) | +- P i=2: If 'False Just (0,2) | +- P i=3: If 'False Just (0,1) | +- P i=4: If 'False Just (1,0) | `- P i=5: If 'True Nothing Val [0,0,0,1]
data IterateUntil p f #
unfolds a value applying f
until the condition p
is true
>>>
pl @(IterateUntil (Id < 90) Pred) 94
Present [94,93,92,91,90] (Unfoldr 94 [94,93,92,91,90] | s=94) Val [94,93,92,91,90]
Instances
P (IterateUntilT p f) x => P (IterateUntil p f :: Type) x # | |
Defined in Predicate.Data.Iterator type PP (IterateUntil p f) x :: Type # eval :: MonadEval m => proxy (IterateUntil p f) -> POpts -> x -> m (TT (PP (IterateUntil p f) x)) # | |
Show (IterateUntil p f) # | |
Defined in Predicate.Data.Iterator showsPrec :: Int -> IterateUntil p f -> ShowS # show :: IterateUntil p f -> String # showList :: [IterateUntil p f] -> ShowS # | |
type PP (IterateUntil p f :: Type) x # | |
Defined in Predicate.Data.Iterator |
data IterateWhile p f #
unfolds a value applying f
while the condition p
is true
>>>
pl @(IterateWhile (Id > 90) Pred) 94
Present [94,93,92,91] (Unfoldr 94 [94,93,92,91] | s=94) Val [94,93,92,91]
Instances
P (IterateWhileT p f) x => P (IterateWhile p f :: Type) x # | |
Defined in Predicate.Data.Iterator type PP (IterateWhile p f) x :: Type # eval :: MonadEval m => proxy (IterateWhile p f) -> POpts -> x -> m (TT (PP (IterateWhile p f) x)) # | |
Show (IterateWhile p f) # | |
Defined in Predicate.Data.Iterator showsPrec :: Int -> IterateWhile p f -> ShowS # show :: IterateWhile p f -> String # showList :: [IterateWhile p f] -> ShowS # | |
type PP (IterateWhile p f :: Type) x # | |
Defined in Predicate.Data.Iterator |
data IterateNWhile n p f #
unfolds a value applying f
while the condition p
is true or n
times
>>>
pl @(IterateNWhile 10 (Id > 90) Pred) 95
Present [95,94,93,92,91] ((>>) [95,94,93,92,91] | {Map [95,94,93,92,91] | [(10,95),(9,94),(8,93),(7,92),(6,91)]}) Val [95,94,93,92,91]
>>>
pl @(IterateNWhile 3 (Id > 90) Pred) 95
Present [95,94,93] ((>>) [95,94,93] | {Map [95,94,93] | [(3,95),(2,94),(1,93)]}) Val [95,94,93]
Instances
P (IterateNWhileT n p f) x => P (IterateNWhile n p f :: Type) x # | |
Defined in Predicate.Data.Iterator type PP (IterateNWhile n p f) x :: Type # eval :: MonadEval m => proxy (IterateNWhile n p f) -> POpts -> x -> m (TT (PP (IterateNWhile n p f) x)) # | |
Show (IterateNWhile n p f) # | |
Defined in Predicate.Data.Iterator showsPrec :: Int -> IterateNWhile n p f -> ShowS # show :: IterateNWhile n p f -> String # showList :: [IterateNWhile n p f] -> ShowS # | |
type PP (IterateNWhile n p f :: Type) x # | |
Defined in Predicate.Data.Iterator |
data IterateNUntil n p f #
unfolds a value applying f
until the condition p
is true or n
times
>>>
pl @(IterateNUntil 10 (Id <= 90) Pred) 95
Present [95,94,93,92,91] ((>>) [95,94,93,92,91] | {Map [95,94,93,92,91] | [(10,95),(9,94),(8,93),(7,92),(6,91)]}) Val [95,94,93,92,91]
>>>
pl @(IterateNUntil 3 (Id <= 90) Pred) 95
Present [95,94,93] ((>>) [95,94,93] | {Map [95,94,93] | [(3,95),(2,94),(1,93)]}) Val [95,94,93]
>>>
pl @(IterateNUntil 9999 'False Id) 1
Error Unfoldr (9999,1):recursion limit i=100 (Unfoldr (9999,1)) Fail "Unfoldr (9999,1):recursion limit i=100"
Instances
P (IterateNUntilT n p f) x => P (IterateNUntil n p f :: Type) x # | |
Defined in Predicate.Data.Iterator type PP (IterateNUntil n p f) x :: Type # eval :: MonadEval m => proxy (IterateNUntil n p f) -> POpts -> x -> m (TT (PP (IterateNUntil n p f) x)) # | |
Show (IterateNUntil n p f) # | |
Defined in Predicate.Data.Iterator showsPrec :: Int -> IterateNUntil n p f -> ShowS # show :: IterateNUntil n p f -> String # showList :: [IterateNUntil n p f] -> ShowS # | |
type PP (IterateNUntil n p f :: Type) x # | |
Defined in Predicate.Data.Iterator |
run p
n
times with state s
>>>
:m + System.Random
>>>
pz @(UnfoldN 10 (RandomRNext Int 1 100 Id) Id) (mkStdGen 3)
Val [64,94,33,26,12,8,81,41,21,89]
runs values in parallel unlike Do
which is serial
>>>
pz @(Para '[Id,Id + 1,Id * 4]) [10,20,30]
Val [10,21,120]
>>>
pz @(Para '[Id,Id + 1,Id * 4]) [10,20,30,40]
Fail "Para:invalid length(4) expected 3"
>>>
pl @(Para '[W 'True, Ge 12, W 'False, Lt 2]) [1,2,-99,-999]
Present [True,False,False,True] (Para(0) [True,False,False,True] | [1,2,-99,-999]) Val [True,False,False,True]
>>>
pl @(Para '[W 'True, Ge 12, W 'False, Lt 2]) [1,2,-99]
Error Para:invalid length(3) expected 4 Fail "Para:invalid length(3) expected 4"
>>>
pl @(Para '[W 'True, Ge 12, W 'False, Lt 2]) [1,2,-99,-999,1,1,2]
Error Para:invalid length(7) expected 4 Fail "Para:invalid length(7) expected 4"
leverages Para
for repeating expressions (passthrough method)
>>>
pz @(ParaN 4 Succ) [1..4]
Val [2,3,4,5]
>>>
pz @(ParaN 4 Succ) "azwxm"
Fail "Para:invalid length(5) expected 4"
>>>
pz @(ParaN 4 Succ) "azwx"
Val "b{xy"
>>>
pl @(ParaN 5 (Guard "0-255" (Between 0 255 Id))) [1,2,3,4,12]
Present [1,2,3,4,12] (Para(0) [1,2,3,4,12] | [1,2,3,4,12]) Val [1,2,3,4,12]
>>>
pl @(ParaN 5 (Guard "0-255" (Between 0 255 Id))) [1,2,3,400,12]
Error 0-255 (Guard | 400 | Para(3 of 4)) Fail "0-255"
>>>
pz @(ParaN 5 (Guard (PrintF "bad value %d" Id) (Between 0 255 Id))) [1,2,3,400,12]
Fail "bad value 400"
>>>
pl @(ParaN 4 (PrintF "%03d" Id)) [141,21,3,0]
Present ["141","021","003","000"] (Para(0) ["141","021","003","000"] | [141,21,3,0]) Val ["141","021","003","000"]
leverages Do
for repeating predicates (passthrough method)
same as DoN n p == FoldN n p Id
but more efficient
>>>
pz @(DoN 4 Succ) 'c'
Val 'g'
>>>
pz @(DoN 4 (Id <> " | ")) "abc"
Val "abc | | | | "
>>>
pz @(DoN 4 (Id <> "|" <> Id)) "abc"
Val "abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc|abc"
>>>
pl @(DoN 4 (Id + 4)) 1
Present 17 ((>>) 17 | {13 + 4 = 17}) Val 17
>>>
pl @(DoN 4 (Id + 7)) 3
Present 31 ((>>) 31 | {24 + 7 = 31}) Val 31
>>>
pl @(DoN 4 9) ()
Present 9 ((>>) 9 | {'9}) Val 9
>>>
pl @(DoN 4 "xy") 3
Present "xy" ((>>) "xy" | {'"xy"}) Val "xy"
creates a promoted list of predicates and then evaluates them into a list. see PP instance for '[k]
>>>
pz @(Repeat 4 Succ) 'c'
Val "dddd"
>>>
pz @(Repeat 4 "abc") ()
Val ["abc","abc","abc","abc"]
>>>
pl @(Repeat 4 "xy") 3
Present ["xy","xy","xy","xy"] ('["xy","xy","xy","xy"] ('"xy") | 3) Val ["xy","xy","xy","xy"]