on master help sign in
Shorten Libraries.hs, and fix some types in some of the libraries.
commit00d3ad2db60523e8a14b3881674f37dec66c0a73 authorevancz <info@elm-lang.org> dateMon 22 Apr 2013, 02:36 (14 years ago) parent29384b7
4 files changed+18−18
compiler/Model/Libraries.hs+7−11 libraries/Automaton.elm+3−4 libraries/Either.elm+3−3 libraries/List.elm+5
compiler/Model/Libraries.hs+7−11
@@ -1,10 +1,11 @@
11module Libraries (libraries, addPrelude) where
22
33import Ast
4+import Control.Applicative ((<$>),(<*>))
45import qualified Data.Map as Map
56import Data.List (inits)
67import Text.JSON
7−import LoadLibraries (docs)
8+import LoadLibraries as Libs
89
910addPrelude :: Module -> Module
1011addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
@@ -40,25 +41,20 @@ libraries =
4041
4142getLibs :: Result (Map.Map String (Map.Map String String))
4243getLibs = do
43− obj <- decodeStrict docs :: Result (JSObject JSValue)
44+ obj <- decodeStrict Libs.docs :: Result (JSObject JSValue)
4445 modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
4546 Map.fromList `fmap` mapM getValues modules
4647
4748
48−getName :: JSObject JSValue -> Result String
49−getName obj = valFromObj "name" obj
50−
51−getType :: JSObject JSValue -> Result String
52−getType obj = valFromObj "type" obj
49+get :: String -> JSObject JSValue -> Result String
50+get = valFromObj
5351
5452getValue :: JSObject JSValue -> Result (String,String)
55−getValue obj = do n <- getName obj
56− t <- getType obj
57− return (n,t)
53+getValue obj = (,) <$> get "name" obj <*> get "type" obj
5854
5955getValues :: JSObject JSValue -> Result (String, Map.Map String String)
6056getValues obj = do
61− name <- getName obj
57+ name <- get "name" obj
6258 vs <- valFromObj "values" obj
6359 vals <- mapM getValue vs
6460 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
5555count : Automaton a Int
5656count = init 0 (\_ c -> c + 1)
5757
58−{-
5958type Queue t = ([t],[t])
6059empty = ([],[])
61−enqueue x (en,de) = (x:en, de)
60+enqueue x (en,de) = (x::en, de)
6261dequeue q = case q of
6362 ([],[]) -> Nothing
6463 (en,[]) -> enqueue ([], reverse en)
6564 (en,hd::tl) -> Just (hd, (en,tl))
6665
6766-- Computes the running average of the last `n` inputs.
68−average : Int -> Automaton Float Float
67+average : Int -> Automaton (Number a) (Number a)
6968average k =
7069 let step n (ns,len,sum) =
7170 if len == k then stepFull n (ns,len,sum)
@@ -76,7 +75,7 @@ average k =
7675 Just (m,ns') -> let sum' = sum + n - m
7776 in ((enqueue n ns', len, sum'), sum' / len)
7877 in init' (empty,0,0) step
79−-}
78+
8079{-- TODO(evancz): move this code to the Form library so people can find it.
8180
8281data DragState = Listen | Ignore | DragFrom (Int,Int)
libraries/Either.elm+3−3
@@ -23,14 +23,14 @@ isRight : Either a b -> Bool
2323isRight e = case e of { Right _ -> True ; _ -> False }
2424
2525-- Keep only the values held in `Left` values.
26−--lefts : [Either a b] -> [a]
26+lefts : [Either a b] -> [a]
2727lefts es = List.filter isLeft es
2828
2929-- Keep only the values held in `Right` values.
30−--rights : [Either a b] -> [b]
30+rights : [Either a b] -> [b]
3131rights es = List.filter isRight es
3232
3333-- Split into two lists, lefts on the left and rights on the right. So we
3434-- have the equivalence: `(partition es == (lefts es, rights es))`
35−-- partition : [Either a b] -> ([a],[b])
35+partition : [Either a b] -> ([a],[b])
3636partition es = List.partition isLeft es
libraries/List.elm+5
@@ -72,15 +72,19 @@ concatMap : (a -> Appendable b) -> [a] -> Appendable b
7272concatMap f = L.concat . L.map f
7373
7474-- Get the sum of the list elements.
75+sum : [Number a] -> Number a
7576sum = L.foldl (+) 0
7677
7778-- Get the product of the list elements.
79+product : [Number a] -> Number a
7880product = L.foldl (*) 1
7981
8082-- Find the highest number in a non-empty list.
83+maximum : [Comparable a] -> Comparable a
8184maximum = L.foldl1 max
8285
8386-- Find the lowest number in a non-empty list.
87+minimum : [Comparable a] -> Comparable a
8488minimum = L.foldl1 min
8589
8690-- Split a list based on the predicate.
@@ -105,6 +109,7 @@ unzip pairs =
105109 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
106110
107111-- Split a list
112+--
108113-- split "," "hello,there,friend" == ["hello", "there", "friend"]
109114split : [a] -> [a] -> [[a]]
110115