Navigation Menu

Skip to content

Commit

Permalink
Examples: Remove declarations that are now in Prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmark committed Dec 29, 2012
1 parent c33d10c commit 8a4fa11
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 151 deletions.
20 changes: 0 additions & 20 deletions examples/CodeWorld.hs
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions examples/Cont.hs
Expand Up @@ -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

Expand All @@ -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
4 changes: 0 additions & 4 deletions examples/canvaswater.hs
Expand Up @@ -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)"
4 changes: 0 additions & 4 deletions examples/console.hs
Expand Up @@ -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)"
3 changes: 0 additions & 3 deletions examples/data.hs
Expand Up @@ -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)"
3 changes: 0 additions & 3 deletions examples/dom.hs
Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions examples/jquery.hs
Expand Up @@ -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
Expand All @@ -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)"

Expand Down
3 changes: 0 additions & 3 deletions examples/node.hs
Expand Up @@ -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)"
3 changes: 0 additions & 3 deletions examples/nqueens.hs
Expand Up @@ -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)
Expand Down
70 changes: 0 additions & 70 deletions examples/oscillator.hs
Expand Up @@ -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 []
Expand All @@ -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)"

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
2 changes: 2 additions & 0 deletions examples/pat.hs
@@ -1,5 +1,7 @@
import Language.Fay.Prelude

import Language.Fay.FFI

main :: Fay ()
main =
case [1,2] of
Expand Down
4 changes: 0 additions & 4 deletions examples/properties.hs
Expand Up @@ -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)"

Expand Down
3 changes: 0 additions & 3 deletions examples/ref.hs
Expand Up @@ -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)"
18 changes: 6 additions & 12 deletions examples/tailrecursive.hs
Expand Up @@ -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
Expand All @@ -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()"
Expand Down
8 changes: 7 additions & 1 deletion 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
7 changes: 6 additions & 1 deletion 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
2 changes: 1 addition & 1 deletion scripts/typecheck-examples
Expand Up @@ -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

0 comments on commit 8a4fa11

Please sign in to comment.