on master help sign in
tapes, cords and sum types
commitd4b214ea1a96ebcb1fcfbb0cbc7c2b5f85a82fa7 authoriko <mail@iko.soy> dateWed 31 May 2023, 16:50 (3 years ago) parent672e348
2 files changed+90−3
src/Urbit/Deconstructor.elm+44−3 tests/Test/Urbit.elm+46
src/Urbit/Deconstructor.elm+44−3
@@ -1,17 +1,21 @@
11module Urbit.Deconstructor exposing
22 ( Deconstructor
33 , alt
4+ , bytes
45 , cell
6+ , const
7+ , cord
58 , float32
69 , float64
710 , int
811 , list
912 , llec
1013 , map
14+ , oneOf
1115 , run
1216 , runBytes
1317 , sig
14− , string
18+ , tape
1519 , tar
1620 )
1721
@@ -38,8 +42,25 @@ runBytes (Deconstructor f) bs =
3842 (\noun -> f noun identity)
3943
4044
41−string : Deconstructor (String -> a) a
42−string =
45+const : Deconstructor (a -> a) a -> a -> Deconstructor c c
46+const (Deconstructor f) value =
47+ Deconstructor
48+ (\noun c ->
49+ case f noun identity of
50+ Just a ->
51+ if a == value then
52+ Just c
53+
54+ else
55+ Nothing
56+
57+ Nothing ->
58+ Nothing
59+ )
60+
61+
62+cord : Deconstructor (String -> a) a
63+cord =
4364 Deconstructor
4465 (\x f ->
4566 case x of
@@ -52,6 +73,11 @@ string =
5273 )
5374
5475
76+tape : Deconstructor (String -> a) a
77+tape =
78+ list cord |> fmap String.concat
79+
80+
5581int : Deconstructor (Int -> a) a
5682int =
5783 Deconstructor
@@ -158,6 +184,16 @@ alt (Deconstructor f) (Deconstructor g) =
158184 )
159185
160186
187+oneOf : List (Deconstructor a b) -> Deconstructor a b
188+oneOf l =
189+ case l of
190+ [] ->
191+ Deconstructor (\_ _ -> Nothing)
192+
193+ x :: xs ->
194+ alt x (oneOf xs)
195+
196+
161197tar : Deconstructor a a
162198tar =
163199 Deconstructor (\_ a -> Just a)
@@ -192,3 +228,8 @@ cell (Deconstructor l) (Deconstructor r) =
192228map : a -> Deconstructor a b -> Deconstructor (b -> c) c
193229map a (Deconstructor f) =
194230 Deconstructor (\noun g -> f noun a |> Maybe.map g)
231+
232+
233+fmap : (a -> b) -> Deconstructor (a -> c) c -> Deconstructor (b -> c) c
234+fmap f (Deconstructor g) =
235+ Deconstructor (\noun h -> g noun (f >> h))
tests/Test/Urbit.elm+46
@@ -93,6 +93,52 @@ tests =
9393 (Bytes.fromByteValues [ 0x01, 0x1F, 0x00, 0x00, 0x20, 0x08, 0x7C, 0x00, 0x00, 0x98, 0x20 ])
9494 )
9595 )
96+ , test "[8 'hi']"
97+ (\() ->
98+ Expect.equal
99+ (Just ( 8, "hi" ))
100+ (D.runBytes
101+ (D.cell D.int D.cord |> D.map Tuple.pair)
102+ (Bytes.fromByteValues [ 0x41, 0x10, 0x3C, 0x5A, 0x1A ])
103+ )
104+ )
105+ , test "[8 \"hi\"]"
106+ (\() ->
107+ Expect.equal
108+ (Just ( 8, "hi" ))
109+ (D.runBytes
110+ (D.cell D.int D.tape |> D.map Tuple.pair)
111+ (Bytes.fromByteValues [ 0x41, 0x30, 0x38, 0x3A, 0x78, 0x5A ])
112+ )
113+ )
114+ , describe "sum types"
115+ [ test "[%tape \"hi\"]"
116+ (\() ->
117+ Expect.equal
118+ (Just "hi")
119+ (D.runBytes
120+ (D.oneOf
121+ [ D.cell (D.const D.cord "tape") D.tape
122+ , D.cell (D.const D.cord "cord") D.cord
123+ ]
124+ )
125+ (Bytes.fromByteValues [ 0x01, 0x9F, 0x2E, 0x0C, 0xAE, 0x1C, 0x1C, 0x1D, 0x3C, 0x2D ])
126+ )
127+ )
128+ , test "[%cord 'hi']"
129+ (\() ->
130+ Expect.equal
131+ (Just "hi")
132+ (D.runBytes
133+ (D.oneOf
134+ [ D.cell (D.const D.cord "tape") D.tape
135+ , D.cell (D.const D.cord "cord") D.cord
136+ ]
137+ )
138+ (Bytes.fromByteValues [ 0x01, 0x7F, 0xEC, 0x4D, 0x8E, 0x0C, 0x1E, 0x2D, 0x0D ])
139+ )
140+ )
141+ ]
96142 ]
97143 ]
98144