Shorten Libraries.hs, and fix some types in some of the libraries.

This commit is contained in:
evancz 2013-04-22 02:36:11 -07:00
parent 29384b771a
commit 00d3ad2db6
4 changed files with 18 additions and 18 deletions

View file

@ -1,10 +1,11 @@
module Libraries (libraries, addPrelude) where
import Ast
import Control.Applicative ((<$>),(<*>))
import qualified Data.Map as Map
import Data.List (inits)
import Text.JSON
import LoadLibraries (docs)
import LoadLibraries as Libs
addPrelude :: Module -> Module
addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
@ -40,25 +41,20 @@ libraries =
getLibs :: Result (Map.Map String (Map.Map String String))
getLibs = do
obj <- decodeStrict docs :: Result (JSObject JSValue)
obj <- decodeStrict Libs.docs :: Result (JSObject JSValue)
modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
Map.fromList `fmap` mapM getValues modules
getName :: JSObject JSValue -> Result String
getName obj = valFromObj "name" obj
getType :: JSObject JSValue -> Result String
getType obj = valFromObj "type" obj
get :: String -> JSObject JSValue -> Result String
get = valFromObj
getValue :: JSObject JSValue -> Result (String,String)
getValue obj = do n <- getName obj
t <- getType obj
return (n,t)
getValue obj = (,) <$> get "name" obj <*> get "type" obj
getValues :: JSObject JSValue -> Result (String, Map.Map String String)
getValues obj = do
name <- getName obj
name <- get "name" obj
vs <- valFromObj "values" obj
vals <- mapM getValue vs
return (name, Map.fromList vals)

View file

@ -55,17 +55,16 @@ init' s f = Step (\x -> let (s',out) = f x s
count : Automaton a Int
count = init 0 (\_ c -> c + 1)
{-
type Queue t = ([t],[t])
empty = ([],[])
enqueue x (en,de) = (x:en, de)
enqueue x (en,de) = (x::en, de)
dequeue q = case q of
([],[]) -> Nothing
(en,[]) -> enqueue ([], reverse en)
(en,hd::tl) -> Just (hd, (en,tl))
-- Computes the running average of the last `n` inputs.
average : Int -> Automaton Float Float
average : Int -> Automaton (Number a) (Number a)
average k =
let step n (ns,len,sum) =
if len == k then stepFull n (ns,len,sum)
@ -76,7 +75,7 @@ average k =
Just (m,ns') -> let sum' = sum + n - m
in ((enqueue n ns', len, sum'), sum' / len)
in init' (empty,0,0) step
-}
{-- TODO(evancz): move this code to the Form library so people can find it.
data DragState = Listen | Ignore | DragFrom (Int,Int)

View file

@ -23,14 +23,14 @@ isRight : Either a b -> Bool
isRight e = case e of { Right _ -> True ; _ -> False }
-- Keep only the values held in `Left` values.
--lefts : [Either a b] -> [a]
lefts : [Either a b] -> [a]
lefts es = List.filter isLeft es
-- Keep only the values held in `Right` values.
--rights : [Either a b] -> [b]
rights : [Either a b] -> [b]
rights es = List.filter isRight es
-- Split into two lists, lefts on the left and rights on the right. So we
-- have the equivalence: `(partition es == (lefts es, rights es))`
-- partition : [Either a b] -> ([a],[b])
partition : [Either a b] -> ([a],[b])
partition es = List.partition isLeft es

View file

@ -72,15 +72,19 @@ concatMap : (a -> Appendable b) -> [a] -> Appendable b
concatMap f = L.concat . L.map f
-- Get the sum of the list elements.
sum : [Number a] -> Number a
sum = L.foldl (+) 0
-- Get the product of the list elements.
product : [Number a] -> Number a
product = L.foldl (*) 1
-- Find the highest number in a non-empty list.
maximum : [Comparable a] -> Comparable a
maximum = L.foldl1 max
-- Find the lowest number in a non-empty list.
minimum : [Comparable a] -> Comparable a
minimum = L.foldl1 min
-- Split a list based on the predicate.
@ -105,6 +109,7 @@ unzip pairs =
(x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
-- Split a list
--
-- split "," "hello,there,friend" == ["hello", "there", "friend"]
split : [a] -> [a] -> [[a]]