From 8a4fa1109b338c60ba08da65f4598dd9ab4c3dc5 Mon Sep 17 00:00:00 2001 From: Adam Bergmark Date: Sat, 29 Dec 2012 22:14:15 +0100 Subject: [PATCH] Examples: Remove declarations that are now in Prelude --- examples/CodeWorld.hs | 20 ----------- examples/Cont.hs | 10 ------ examples/canvaswater.hs | 4 --- examples/console.hs | 4 --- examples/data.hs | 3 -- examples/dom.hs | 3 -- examples/jquery.hs | 15 ++++---- examples/node.hs | 3 -- examples/nqueens.hs | 3 -- examples/oscillator.hs | 70 -------------------------------------- examples/pat.hs | 2 ++ examples/properties.hs | 4 --- examples/ref.hs | 3 -- examples/tailrecursive.hs | 18 ++++------ scripts/build-examples | 8 ++++- scripts/open-examples | 7 +++- scripts/typecheck-examples | 2 +- 17 files changed, 28 insertions(+), 151 deletions(-) diff --git a/examples/CodeWorld.hs b/examples/CodeWorld.hs index 1032c11..5de7a71 100644 --- a/examples/CodeWorld.hs +++ b/examples/CodeWorld.hs @@ -22,26 +22,6 @@ module CodeWorld where import Language.Fay.Prelude import Language.Fay.FFI -take 0 _ = [] -take n (x:xs) = x : take (n-1) xs - -_ ^ 0 = 1 -x ^ n = x * x ^ (n-1) - -pi :: Double -pi = ffi "Math.PI" - -sin :: Double -> Double -sin = ffi "Math.sin(%1)" - -cos :: Double -> Double -cos = ffi "Math.cos(%1)" - -abs :: Double -> Double -abs = ffi "Math.abs(%1)" - -floor :: Double -> Int -floor = ffi "Math.floor(%1)" data Element instance Foreign Element diff --git a/examples/Cont.hs b/examples/Cont.hs index 90c40be..21f36de 100644 --- a/examples/Cont.hs +++ b/examples/Cont.hs @@ -36,10 +36,6 @@ setTimeout = ffi "global.setTimeout(%2,%1)" readFile :: Foreign b => String -> (String -> Fay b) -> Fay b readFile = ffi "require('fs').readFile(%1,'utf-8',function(_,s){ %2(s); })" --- | Print using console.log. -print :: String -> Fay () -print = ffi "console.log(%1)" - sync :: (t -> (a -> Fay r) -> Fay r) -> t -> ContT r Fay a sync m a = ContT $ \c -> m a c @@ -66,9 +62,3 @@ contT = callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c lift m = ContT (\x -> m >>=* x) in CC return (>>=) (>>) callCC lift where (>>=*) = (>>=) - --------------------------------------------------------------------------------- --- Crap. - -take 0 _ = [] -take n (x:xs) = x : take (n-1) xs diff --git a/examples/canvaswater.hs b/examples/canvaswater.hs index 32d7ffb..17efb9d 100644 --- a/examples/canvaswater.hs +++ b/examples/canvaswater.hs @@ -145,10 +145,6 @@ print = ffi "console['log'](%1)" log :: String -> Fay () log = ffi "console['log'](%1)" --- | Alert using window.alert. -sin :: Double -> Double -sin = ffi "window.Math['sin'](%1)" - -- | Alert using window.alert. setInterval :: Fay () -> Double -> Fay () setInterval = ffi "window['setInterval'](%1,%2)" diff --git a/examples/console.hs b/examples/console.hs index 8576862..df352ec 100644 --- a/examples/console.hs +++ b/examples/console.hs @@ -6,7 +6,3 @@ import Language.Fay.FFI import Language.Fay.Prelude main = print "Hello, World!" - --- | Print using console.log. -print :: String -> Fay () -print = ffi "console.log(%1)" diff --git a/examples/data.hs b/examples/data.hs index d08eb15..7df91f5 100644 --- a/examples/data.hs +++ b/examples/data.hs @@ -16,6 +16,3 @@ data Foo = Foo { x :: Double, y :: String, z :: Foo } | Bar instance Foreign Foo main = print (show (Foo 123 "abc" Bar)) - -print :: String -> Fay () -print = ffi "console.log(%1)" diff --git a/examples/dom.hs b/examples/dom.hs index cb40bf9..a07101a 100644 --- a/examples/dom.hs +++ b/examples/dom.hs @@ -14,9 +14,6 @@ printBody = do result <- documentGetElements "body" print result -print :: Foreign a => [a] -> Fay () -print = ffi "console.log(%1)" - data Element instance Foreign Element instance Show (Element) diff --git a/examples/jquery.hs b/examples/jquery.hs index 8b42fda..a848282 100644 --- a/examples/jquery.hs +++ b/examples/jquery.hs @@ -9,16 +9,16 @@ import Language.Fay.Prelude main :: Fay () main = do ready $ do - print (showDouble 123) + putStrLn (showDouble 123) body <- select "body" printArg body - addClassWith (\i s -> do print ("i… " ++ showDouble i) - print ("s… " ++ showString s) + addClassWith (\i s -> do putStrLn ("i… " ++ showDouble i) + putStrLn ("s… " ++ showString s) return "abc") body - addClassWith (\i s -> do print ("i… " ++ showDouble i) - print ("s… " ++ showString s) - print (showString ("def: " ++ s)) + addClassWith (\i s -> do putStrLn ("i… " ++ showDouble i) + putStrLn ("s… " ++ showString s) + putStrLn (showString ("def: " ++ s)) return "foo") body printArg body @@ -31,9 +31,6 @@ instance Show JQuery data Element instance Foreign Element -print :: String -> Fay () -print = ffi "console.log(%1)" - printArg :: Foreign a => a -> Fay () printArg = ffi "console.log(\"%%o\",%1)" diff --git a/examples/node.hs b/examples/node.hs index cc763ce..2741ba3 100644 --- a/examples/node.hs +++ b/examples/node.hs @@ -26,6 +26,3 @@ require' = ffi "require(%1)" inspect :: Foreign a => Sys -> a -> Fay Details inspect = ffi "%1.inspect(%2)" - -print :: (Foreign a,Show a) => a -> Fay () -print = ffi "console.log(%1)" diff --git a/examples/nqueens.hs b/examples/nqueens.hs index af59364..d081a17 100644 --- a/examples/nqueens.hs +++ b/examples/nqueens.hs @@ -10,9 +10,6 @@ import Language.Fay.FFI main :: Fay () main = benchmark $ print (nsoln 11) -print :: Int -> Fay () -print = ffi "console.log(%1)" - listLength :: [a] -> Int -> Int listLength [] acc = acc listLength (_:l) acc = listLength l (1 + acc) diff --git a/examples/oscillator.hs b/examples/oscillator.hs index d900104..0e95bd3 100644 --- a/examples/oscillator.hs +++ b/examples/oscillator.hs @@ -416,64 +416,11 @@ renderGraph cg pref gdataref x rng = do -------------------------------------------------------------------------------- -- Utilities -pi :: Double -pi = ffi "Math.PI" - -sin :: Double -> Double -sin = ffi "Math.sin(%1)" - -cos :: Double -> Double -cos = ffi "Math.cos(%1)" - -replicate :: Int -> a -> [a] -replicate 0 _ = [] -replicate n x = x : (replicate (n-1) x) - -take :: Int -> [a] -> [a] -take n _ | n <= 0 = [] -take _ [] = [] -take n (x:xs) = x : take (n-1) xs - -drop :: Int -> [a] -> [a] -drop n xs | n <= 0 = xs -drop _ [] = [] -drop n (_:xs) = drop (n-1) xs - -splitAt :: Int -> [a] -> ([a],[a]) -splitAt n xs = (take n xs, drop n xs) - -takeWhile :: (a -> Bool) -> [a] -> [a] -takeWhile _ [] = [] -takeWhile p (x:xs) - | p x = x : takeWhile p xs - | otherwise = [] - -_ ^ 0 = 1 -x ^ n = x * x ^ (n-1) - -head :: [a] -> a -head (x:_) = x - -tail :: [a] -> [a] -tail (_:xs) = xs - -zip3 :: [a]->[b]->[c]->[(a,b,c)] -zip3 (a:as) (b:bs) (c:cs) = (a,b,c) : zip3 as bs cs -zip3 _ _ _ = [] - -zipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->[d] -zipWith3 z (a:as) (b:bs) (c:cs) = z a b c : zipWith3 z as bs cs -zipWith3 _ _ _ _ = [] - zipWith5 :: (a->b->c->d->e->f) -> [a]->[b]->[c]->[d]->[e]->[f] zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) = z a b c d e : zipWith5 z as bs cs ds es zipWith5 _ _ _ _ _ _ = [] -(!!) :: [a] -> Int -> a -(x:_) !! 0 = x -(_:xs) !! n = xs !! (n-1) - mapM :: (a -> Fay b) -> [a] -> Fay [b] mapM m (x:xs) = m x >>= (\mx -> mapM m xs >>= (\mxs -> return (mx:mxs))) mapM _ [] = return [] @@ -485,17 +432,6 @@ forM [] _ = return [] replicateM :: Int -> Fay a -> Fay [a] replicateM n x = sequence (replicate n x) -sum l = sum' l 0 - where - sum' [] a = a - sum' (x:xs) a = sum' xs (a+x) - -cycle :: [a] -> [a] -cycle xs = xs' where xs' = xs ++ xs' - -repeat :: a -> [a] -repeat x = xs where xs = x : xs - parseDouble :: String -> Double parseDouble = ffi "parseFloat(%1)" @@ -541,9 +477,6 @@ selectValue = ffi "%1[%1['selectedIndex']]['value']" setSelectIndex :: Element -> Int -> Fay () setSelectIndex = ffi "%1['selectedIndex']=%2" -floor :: Double -> Int -floor = ffi "Math.floor(%1)" - -------------------------------------------------------------------------------- -- Ref @@ -693,6 +626,3 @@ setArrayVal = ffi "%1[%2]=%3" arrayVal :: Array -> Int -> Fay Double arrayVal = ffi "%1[%2]" - -rem :: Int -> Int -> Int -rem = ffi "%1 %% %2" \ No newline at end of file diff --git a/examples/pat.hs b/examples/pat.hs index 2ef4ddf..0a9c26f 100644 --- a/examples/pat.hs +++ b/examples/pat.hs @@ -1,5 +1,7 @@ import Language.Fay.Prelude +import Language.Fay.FFI + main :: Fay () main = case [1,2] of diff --git a/examples/properties.hs b/examples/properties.hs index 82da9a0..038aea1 100644 --- a/examples/properties.hs +++ b/examples/properties.hs @@ -25,10 +25,6 @@ printList = ffi "console.log(%1)" print' :: String -> Fay () print' = ffi "console['log'](%1)" --- | Print using window.print. -print :: Foreign a => a -> Fay () -print = ffi "console['log'](%1)" - addEventListener :: String -> Fay () -> Bool -> Fay () addEventListener = ffi "window['addEventListener'](%1,%2,%3)" diff --git a/examples/ref.hs b/examples/ref.hs index 6d573df..75743cc 100644 --- a/examples/ref.hs +++ b/examples/ref.hs @@ -27,6 +27,3 @@ writeRef = ffi "Fay$$writeRef(%1,%2)" readRef :: Foreign a => Ref a -> Fay a readRef = ffi "Fay$$readRef(%1)" - -print :: String -> Fay () -print = ffi "console.log(%1)" diff --git a/examples/tailrecursive.hs b/examples/tailrecursive.hs index 6e970ad..5cd2c90 100644 --- a/examples/tailrecursive.hs +++ b/examples/tailrecursive.hs @@ -14,19 +14,13 @@ import Language.Fay.Prelude main = do benchmark $ printI (map (\x -> x+1) fibs !! 10) benchmark $ printI (fibs !! 80) - benchmark $ printD (sum 1000000 0) - benchmark $ printD (sum 1000000 0) - benchmark $ printD (sum 1000000 0) - benchmark $ printD (sum 1000000 0) + benchmark $ printD (sum' 1000000 0) + benchmark $ printD (sum' 1000000 0) + benchmark $ printD (sum' 1000000 0) + benchmark $ printD (sum' 1000000 0) fibs = 0 : 1 : zipWith (+) fibs (tail fibs) -tail (_:xs) = xs - -xs !! k = go 0 xs where - go n (x:xs) | n == k = x - | otherwise = go (n+1) xs - benchmark m = do start <- getSeconds m @@ -39,8 +33,8 @@ length' = go 0 where go acc [] = acc -- tail recursive -sum 0 acc = acc -sum n acc = sum (n - 1) (acc + n) +sum' 0 acc = acc +sum' n acc = sum' (n - 1) (acc + n) getSeconds :: Fay Double getSeconds = ffi "new Date()" diff --git a/scripts/build-examples b/scripts/build-examples index 0b41e7d..dc577aa 100755 --- a/scripts/build-examples +++ b/scripts/build-examples @@ -1,2 +1,8 @@ +#!/bin/sh + curl http://code.jquery.com/jquery-latest.min.js -o examples/jquery.min.js -for i in `ls examples/*.hs`; do echo $i;dist/build/fay/fay -p $i --no-ghc; done +for i in `ls examples/*.hs` +do + echo $i + dist/build/fay/fay --include examples -p $i --no-ghc +done diff --git a/scripts/open-examples b/scripts/open-examples index cb3324c..bd35769 100755 --- a/scripts/open-examples +++ b/scripts/open-examples @@ -1 +1,6 @@ -for i in `ls examples/*.html`; do gnome-open $i; done +#!/bin/sh + +for i in `ls examples/*.html` +do + gnome-open $i +done diff --git a/scripts/typecheck-examples b/scripts/typecheck-examples index c851545..a714de3 100755 --- a/scripts/typecheck-examples +++ b/scripts/typecheck-examples @@ -2,5 +2,5 @@ for i in `ls examples/*.hs`; do echo $i; - ghc -package fay -XNoImplicitPrelude -fno-code $i + ghc -package fay -XNoImplicitPrelude -fno-code -Iexamples -main-is Language.Fay.DummyMain $i done