@@ -7,11 +7,12 @@ import Control.Monad.Writer (runWriter, tell)
7
7
import Data.AddressBook (PhoneType (..), address , phoneNumber )
8
8
import Data.Array ((..))
9
9
import Data.Either (Either (..))
10
+ import Data.Foldable (foldl , foldr , foldMap )
10
11
import Data.Int (fromNumber )
11
12
import Data.List (List (..), (:))
12
13
import Data.Maybe (Maybe (..))
13
14
import Data.String.Regex as R
14
- import Data.Traversable (traverse )
15
+ import Data.Traversable (sequence , traverse )
15
16
import Data.Tuple (snd )
16
17
import Data.Validation.Semigroup (invalid )
17
18
import Effect (Effect )
@@ -154,16 +155,39 @@ Note to reader: Delete this line to expand comment block -}
154
155
let
155
156
leaf :: forall a . a -> Tree a
156
157
leaf x = Branch Leaf x Leaf
158
+ intTree :: Tree Int
159
+ intTree = Branch (Branch (leaf 1 ) 2 (leaf 3 )) 4 (Branch (leaf 5 ) 6 (leaf 7 ))
157
160
suite " Exercise - traverse" do
161
+ suite " Functor Tree" do
162
+ test " Functor - map" do
163
+ Assert .equal
164
+ (Branch (Branch (leaf " 1" ) " 2" (leaf " 3" )) " 4" (Branch (leaf " 5" ) " 6" (leaf " 7" )))
165
+ $ map show intTree
166
+ suite " Foldable Tree" do
167
+ test " Foldable - foldr" do
168
+ Assert .equal " 1234567"
169
+ $ foldr (\x acc -> show x <> acc) " " intTree
170
+ test " Foldable - foldl" do
171
+ Assert .equal " 7654321"
172
+ $ foldl (\acc x -> show x <> acc) " " intTree
173
+ test " Foldable - foldMap" do
174
+ Assert .equal " 1234567"
175
+ $ foldMap (\x -> show x) intTree
158
176
suite " Maybe side-effect" do
159
- test " Just" do
177
+ test " Just - traverse " do
160
178
Assert .equal (Just $ Branch (leaf 1 ) 2 (leaf 3 ))
161
179
$ traverse fromNumber
162
180
$ Branch (leaf 1.0 ) 2.0 (leaf 3.0 )
163
- test " Nothing" do
181
+ test " Just - sequence" do
182
+ Assert .equal (Just $ Branch (leaf 1 ) 2 (leaf 3 ))
183
+ $ sequence $ Branch (leaf $ Just 1 ) (Just 2 ) (leaf $ Just 3 )
184
+ test " Nothing - traverse" do
164
185
Assert .equal Nothing
165
186
$ traverse fromNumber
166
187
$ Branch (leaf 1.0 ) 2.0 (leaf 3.7 )
188
+ test " Nothing - sequence" do
189
+ Assert .equal Nothing
190
+ $ sequence $ Branch (leaf $ Nothing ) (Just 2 ) (leaf $ Just 3 )
167
191
test " Array side-effect - check traversal order" do
168
192
Assert .equal (1 .. 7 )
169
193
$ snd
0 commit comments