on master help sign in
Add data structure called Data.NonEmptyList for a couple scenarios
83faa2eEvan Czaplicki
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