-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSnapshot.Fusion01.purs
55 lines (45 loc) · 1.47 KB
/
Snapshot.Fusion01.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- @inline export overArray arity=1
module Snapshot.Fusion01 (test) where
import Prelude
import Data.Array as Array
import Data.List as List
import Data.Maybe (Maybe(..))
import Data.String as String
import Partial.Unsafe (unsafePartial)
newtype Fold a = Fold (forall r. (a -> r -> r) -> r -> r)
mapF :: forall a b. (a -> b) -> Fold a -> Fold b
mapF f (Fold next) = Fold \cons nil -> next (cons <<< f) nil
filterMapF :: forall a b. (a -> Maybe b) -> Fold a -> Fold b
filterMapF f (Fold next) = Fold \cons nil ->
next
( \a as ->
case f a of
Just b ->
cons b as
Nothing ->
as
)
nil
filterF :: forall a. (a -> Boolean) -> Fold a -> Fold a
filterF p = filterMapF (\a -> if p a then Just a else Nothing)
fromArray :: forall a. Array a -> Fold a
fromArray arr = Fold \cons nil -> do
let
loop n acc
| n == 0 =
acc
| otherwise =
loop (n - 1) $ cons (unsafePartial (Array.unsafeIndex arr n)) acc
loop (Array.length arr - 1) nil
toArray :: forall a. Fold a -> Array a
toArray (Fold next) = Array.reverse $ List.toUnfoldable $ next List.Cons List.Nil
overArray :: forall a b. (Fold a -> Fold b) -> Array a -> Array b
overArray fold = toArray <<< fold <<< fromArray
test :: Array Int -> Array String
test = overArray do
mapF (add 1)
>>> mapF show
>>> filterMapF (String.stripPrefix (String.Pattern "1"))
>>> mapF (append "2")
>>> filterF (_ /= "wat")
>>> mapF (flip append "1")