on master help sign in
Add data structure called Data.NonEmptyList for a couple scenarios

Rather just do it from scratch than bring in weird operators. Also gives more control over what can and cannot happen with this data structure in the overall codebase.

commit83faa2e1e6f20911add80d3d6426e876e95ea183 authorEvan Czaplicki <evancz@users.noreply.github.com> dateSun 24 Mar 2019, 15:06 (8 years ago) parentd371510
1 file changed+36
compiler/src/Data/NonEmptyList.hs+36
compiler/src/Data/NonEmptyList.hsnew file+36
@@ -0,0 +1,36 @@
1+module Data.NonEmptyList
2+ ( List(..)
3+ , toList
4+ )
5+ where
6+
7+
8+
9+-- LIST
10+
11+
12+data List a =
13+ List a [a]
14+
15+
16+toList :: List a -> [a]
17+toList (List x xs) =
18+ x:xs
19+
20+
21+
22+-- INSTANCES
23+
24+
25+instance Functor List where
26+ fmap func (List x xs) = List (func x) (map func xs)
27+
28+
29+instance Traversable List where
30+ traverse func (List x xs) = List <$> func x <*> traverse func xs
31+
32+
33+instance Foldable List where
34+ foldr step state (List x xs) = step x (foldr step state xs)
35+ foldl step state (List x xs) = foldl step (step state x) xs
36+ foldl1 step (List x xs) = foldl step x xs