on f4401ac help sign in
elk/compiler/src/Data/OneOrMore.hs
99 lines 1.4 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Data.OneOrMore
3 ( OneOrMore(..)
4 , one
5 , more
6 , map
7 , destruct
8 , getFirstTwo
9 )
10 where
13import Prelude hiding (map)
17-- ONE OR MORE
20data OneOrMore a
21 = One a
22 | More (OneOrMore a) (OneOrMore a)
25one :: a -> OneOrMore a
26one =
27 One
30more :: OneOrMore a -> OneOrMore a -> OneOrMore a
31more =
32 More
36-- MAP
39map :: (a -> b) -> OneOrMore a -> OneOrMore b
40map func oneOrMore =
41 case oneOrMore of
42 One value ->
43 One (func value)
45 More left right ->
46 More (map func left) (map func right)
50-- DESTRUCT
53destruct :: (a -> [a] -> b) -> OneOrMore a -> b
54destruct func oneOrMore =
55 destructLeft func oneOrMore []
58destructLeft :: (a -> [a] -> b) -> OneOrMore a -> [a] -> b
59destructLeft func oneOrMore xs =
60 case oneOrMore of
61 One x ->
62 func x xs
64 More a b ->
65 destructLeft func a (destructRight b xs)
68destructRight :: OneOrMore a -> [a] -> [a]
69destructRight oneOrMore xs =
70 case oneOrMore of
71 One x ->
72 x : xs
74 More a b ->
75 destructRight a (destructRight b xs)
79-- GET FIRST TWO
82getFirstTwo :: OneOrMore a -> OneOrMore a -> (a,a)
83getFirstTwo left right =
84 case left of
85 One x ->
86 (x, getFirstOne right)
88 More lleft lright ->
89 getFirstTwo lleft lright
92getFirstOne :: OneOrMore a -> a
93getFirstOne oneOrMore =
94 case oneOrMore of
95 One x ->
96 x
98 More left _ ->
99 getFirstOne left