on master help sign in
Shorten Libraries.hs, and fix some types in some of the libraries.
00d3ad2evancz
compiler/Model/Libraries.hs+7−11
@@ -1,10 +1,11 @@
1module Libraries (libraries, addPrelude) where
1module Libraries (libraries, addPrelude) where
2
2
3import Ast
3import Ast
4+import Control.Applicative ((<$>),(<*>))
4import qualified Data.Map as Map
5import qualified Data.Map as Map
5import Data.List (inits)
6import Data.List (inits)
6import Text.JSON
7import Text.JSON
7−import LoadLibraries (docs)
8+import LoadLibraries as Libs
8
9
9addPrelude :: Module -> Module
10addPrelude :: Module -> Module
10addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
11addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
@@ -40,25 +41,20 @@ libraries =
40
41
41getLibs :: Result (Map.Map String (Map.Map String String))
42getLibs :: Result (Map.Map String (Map.Map String String))
42getLibs = do
43getLibs = do
43− obj <- decodeStrict docs :: Result (JSObject JSValue)
44+ obj <- decodeStrict Libs.docs :: Result (JSObject JSValue)
44 modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
45 modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
45 Map.fromList `fmap` mapM getValues modules
46 Map.fromList `fmap` mapM getValues modules
46
47
47
48
48−getName :: JSObject JSValue -> Result String
49−getName obj = valFromObj "name" obj
50−
51−getType :: JSObject JSValue -> Result String
49+get :: String -> JSObject JSValue -> Result String
52−getType obj = valFromObj "type" obj
50+get = valFromObj
53
51
54getValue :: JSObject JSValue -> Result (String,String)
52getValue :: JSObject JSValue -> Result (String,String)
55−getValue obj = do n <- getName obj
53+getValue obj = (,) <$> get "name" obj <*> get "type" obj
56− t <- getType obj
57− return (n,t)
58
54
59getValues :: JSObject JSValue -> Result (String, Map.Map String String)
55getValues :: JSObject JSValue -> Result (String, Map.Map String String)
60getValues obj = do
56getValues obj = do
61− name <- getName obj
57+ name <- get "name" obj
62 vs <- valFromObj "values" obj
58 vs <- valFromObj "values" obj
63 vals <- mapM getValue vs
59 vals <- mapM getValue vs
64 return (name, Map.fromList vals)
60 return (name, Map.fromList vals)
libraries/Automaton.elm+3−4
@@ -55,17 +55,16 @@ init' s f = Step (\x -> let (s',out) = f x s
55count : Automaton a Int
55count : Automaton a Int
56count = init 0 (\_ c -> c + 1)
56count = init 0 (\_ c -> c + 1)
57
57
58−{-
59type Queue t = ([t],[t])
58type Queue t = ([t],[t])
60empty = ([],[])
59empty = ([],[])
61−enqueue x (en,de) = (x:en, de)
60+enqueue x (en,de) = (x::en, de)
62dequeue q = case q of
61dequeue q = case q of
63 ([],[]) -> Nothing
62 ([],[]) -> Nothing
64 (en,[]) -> enqueue ([], reverse en)
63 (en,[]) -> enqueue ([], reverse en)
65 (en,hd::tl) -> Just (hd, (en,tl))
64 (en,hd::tl) -> Just (hd, (en,tl))
66
65
67-- Computes the running average of the last `n` inputs.
66-- Computes the running average of the last `n` inputs.
68−average : Int -> Automaton Float Float
67+average : Int -> Automaton (Number a) (Number a)
69average k =
68average k =
70 let step n (ns,len,sum) =
69 let step n (ns,len,sum) =
71 if len == k then stepFull n (ns,len,sum)
70 if len == k then stepFull n (ns,len,sum)
@@ -76,7 +75,7 @@ average k =
76 Just (m,ns') -> let sum' = sum + n - m
75 Just (m,ns') -> let sum' = sum + n - m
77 in ((enqueue n ns', len, sum'), sum' / len)
76 in ((enqueue n ns', len, sum'), sum' / len)
78 in init' (empty,0,0) step
77 in init' (empty,0,0) step
79−-}
78+
80{-- TODO(evancz): move this code to the Form library so people can find it.
79{-- TODO(evancz): move this code to the Form library so people can find it.
81
80
82data DragState = Listen | Ignore | DragFrom (Int,Int)
81data DragState = Listen | Ignore | DragFrom (Int,Int)
libraries/Either.elm+3−3
@@ -23,14 +23,14 @@ isRight : Either a b -> Bool
23isRight e = case e of { Right _ -> True ; _ -> False }
23isRight e = case e of { Right _ -> True ; _ -> False }
24
24
25-- Keep only the values held in `Left` values.
25-- Keep only the values held in `Left` values.
26−--lefts : [Either a b] -> [a]
26+lefts : [Either a b] -> [a]
27lefts es = List.filter isLeft es
27lefts es = List.filter isLeft es
28
28
29-- Keep only the values held in `Right` values.
29-- Keep only the values held in `Right` values.
30−--rights : [Either a b] -> [b]
30+rights : [Either a b] -> [b]
31rights es = List.filter isRight es
31rights es = List.filter isRight es
32
32
33-- Split into two lists, lefts on the left and rights on the right. So we
33-- Split into two lists, lefts on the left and rights on the right. So we
34-- have the equivalence: `(partition es == (lefts es, rights es))`
34-- have the equivalence: `(partition es == (lefts es, rights es))`
35−-- partition : [Either a b] -> ([a],[b])
35+partition : [Either a b] -> ([a],[b])
36partition es = List.partition isLeft es
36partition es = List.partition isLeft es
libraries/List.elm+5
@@ -72,15 +72,19 @@ concatMap : (a -> Appendable b) -> [a] -> Appendable b
72concatMap f = L.concat . L.map f
72concatMap f = L.concat . L.map f
73
73
74-- Get the sum of the list elements.
74-- Get the sum of the list elements.
75+sum : [Number a] -> Number a
75sum = L.foldl (+) 0
76sum = L.foldl (+) 0
76
77
77-- Get the product of the list elements.
78-- Get the product of the list elements.
79+product : [Number a] -> Number a
78product = L.foldl (*) 1
80product = L.foldl (*) 1
79
81
80-- Find the highest number in a non-empty list.
82-- Find the highest number in a non-empty list.
83+maximum : [Comparable a] -> Comparable a
81maximum = L.foldl1 max
84maximum = L.foldl1 max
82
85
83-- Find the lowest number in a non-empty list.
86-- Find the lowest number in a non-empty list.
87+minimum : [Comparable a] -> Comparable a
84minimum = L.foldl1 min
88minimum = L.foldl1 min
85
89
86-- Split a list based on the predicate.
90-- Split a list based on the predicate.
@@ -105,6 +109,7 @@ unzip pairs =
105 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
109 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
106
110
107-- Split a list
111-- Split a list
112+--
108-- split "," "hello,there,friend" == ["hello", "there", "friend"]
113-- split "," "hello,there,friend" == ["hello", "there", "friend"]
109split : [a] -> [a] -> [[a]]
114split : [a] -> [a] -> [[a]]
110
115