forked from aristanetworks/purescript-backend-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshot.Fusion02.purs
70 lines (60 loc) · 2.02 KB
/
Snapshot.Fusion02.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- @inline export mapU arity=1
-- @inline export filterMapU arity=1
-- @inline export filterU arity=1
-- @inline export fromArray arity=1
-- @inline export toArray arity=1
-- @inline export overArray arity=1
module Snapshot.Fusion02 (test) where
import Prelude
import Data.Array as Array
import Data.Exists (Exists, mkExists, runExists)
import Data.List as List
import Data.Maybe (Maybe(..))
import Data.String as String
import Partial.Unsafe (unsafePartial)
type Step a s = forall r. (Unit -> r) -> (s -> a -> r) -> r
data Unfold' a s = Unfold s (s -> Step a s)
type Unfold a = Exists (Unfold' a)
mapU :: forall a b. (a -> b) -> Unfold a -> Unfold b
mapU f = runExists \(Unfold s1 step) ->
mkExists $ Unfold s1 \s2 nothing just ->
step s2 nothing \s3 a ->
just s3 (f a)
filterMapU :: forall a b. (a -> Maybe b) -> Unfold a -> Unfold b
filterMapU f = runExists \(Unfold s1 step) ->
mkExists $ Unfold s1 \s2 nothing just -> do
let
loop s3 =
step s3 nothing \s4 a ->
case f a of
Nothing ->
loop s4
Just b ->
just s4 b
loop s2
filterU :: forall a. (a -> Boolean) -> Unfold a -> Unfold a
filterU p = filterMapU \a -> if p a then Just a else Nothing
fromArray :: forall a. Array a -> Unfold a
fromArray arr = mkExists $ Unfold 0 \ix nothing just ->
if ix == Array.length arr then
nothing unit
else
just (ix + 1) (unsafePartial Array.unsafeIndex arr ix)
toArray :: forall a. Unfold a -> Array a
toArray = runExists \(Unfold s1 step) -> do
let
loop s2 acc =
step s2
(\_ -> Array.reverse (List.toUnfoldable acc))
(\s3 a -> loop s3 (List.Cons a acc))
loop s1 List.Nil
overArray :: forall a b. (Unfold a -> Unfold b) -> Array a -> Array b
overArray unfold = toArray <<< unfold <<< fromArray
test :: Array Int -> Array String
test = overArray do
mapU (add 1)
>>> mapU show
>>> filterMapU (String.stripPrefix (String.Pattern "1"))
>>> mapU (append "2")
>>> filterU (_ /= "wat")
>>> mapU (flip append "1")