git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Remove foreach
commitb10f11be19c1b6b8ada1b398f0eec19abcc300b2 authorPhil Freeman <paf31@cantab.net> dateTue 7 Jan 2014, 15:22 (13 years ago) parent7e7cc99
9 files changed+16−84
examples/passing/Foreach.purs−9 libraries/prelude/prelude.js−23 libraries/prelude/prelude.purs+15−32 purescript.cabal+1−1 src/Language/PureScript/CodeGen/JS.hs−1 src/Language/PureScript/Parser/Values.hs−6 src/Language/PureScript/Pretty/Values.hs−3 src/Language/PureScript/TypeChecker/Types.hs−8 src/Language/PureScript/Values.hs−1
examples/passing/Foreach.pursdeleted−9
@@ -1,9 +0,0 @@
1−module Foreach where
2−
3− sum = \ns -> {
4− var total = 0;
5− foreach (n in ns) {
6− total = total + n;
7− }
8− return total;
9− }
libraries/prelude/prelude.js−23
@@ -40,26 +40,3 @@ function numberToString(n) { return n.toString(); }
4040
4141var math = Math;
4242
43−function map(f) {
44− return function(xs) {
45− var ys = [];
46− for (var i = 0; i < xs.length; i++) {
47− ys[i] = f(xs[i]);
48− }
49− return ys;
50− };
51−}
52−
53−function filter(xs) {
54− return function (p) {
55− var ys = [];
56− var j = 0;
57− for (var i = 0; i < xs.length; i++) {
58− var x = xs[i];
59− if (p(x)) {
60− ys[j++] = x;
61− }
62− }
63− return ys;
64− };
65−}
libraries/prelude/prelude.purs+15−32
@@ -67,20 +67,17 @@ module Arrays where
6767 tailSafe (_ : xs) = Just xs
6868 tailSafe _ = Nothing
6969
70− foreign import map :: forall a b. (a -> b) -> [a] -> [b]
70+ map :: forall a b. (a -> b) -> [a] -> [b]
71+ map _ [] = []
72+ map f (x:xs) = f x : map f xs
7173
7274 foldr :: forall a b. (a -> b -> a) -> a -> [b] -> a
7375 foldr f a (b : bs) = f (foldr f a bs) b
7476 foldr _ a [] = a
7577
7678 foldl :: forall a b. (a -> b -> b) -> b -> [a] -> b
77− foldl f b as = {
78− var result = b;
79− foreach (a in as) {
80− result = f a result;
81− }
82− return result;
83− }
79+ foldl _ b [] = b
80+ foldl f b (a:as) = foldl f (f a b) as
8481
8582 foreign import member "length" length :: forall a. [a] -> Number
8683
@@ -112,15 +109,13 @@ module Arrays where
112109 (:) a = concat [a]
113110
114111 concatMap :: forall a b. [a] -> (a -> [b]) -> [b]
115− concatMap as f = {
116− var result = [];
117− foreach (a in as) {
118− result = result `concat` f a;
119− }
120− return result;
121− }
112+ concatMap [] f = []
113+ concatMap (a:as) f = f a `concat` concatMap as f
122114
123− foreign import filter :: forall a. [a] -> (a -> Boolean) -> [a]
115+ filter :: forall a. (a -> Boolean) -> [a] -> [a]
116+ filter _ [] = []
117+ filter p (x:xs) | p x = x : filter p xs
118+ filter p (_:xs) = filter p xs
124119
125120 empty :: forall a. [a] -> Boolean
126121 empty [] = true
@@ -140,24 +135,12 @@ module Arrays where
140135 zipWith _ _ _ = []
141136
142137 any :: forall a. (a -> Boolean) -> [a] -> Boolean
143− any p as = {
144− for (i <- 0 until length as) {
145− if (p (as !! i)) {
146− return true;
147− }
148− }
149− return false;
150− }
138+ any _ [] = false
139+ any p (a:as) = p a || any p as
151140
152141 all :: forall a. (a -> Boolean) -> [a] -> Boolean
153− all p as = {
154− for (i <- 0 until length as) {
155− if (!(p (as !! i))) {
156− return false;
157− }
158− }
159− return true;
160− }
142+ all _ [] = true
143+ all p (a:as) = p a && all p as
161144
162145module Tuple where
163146
purescript.cabal+1−1
@@ -1,5 +1,5 @@
11name: purescript
2−version: 0.2.4
2+version: 0.2.5
33cabal-version: >=1.8
44build-type: Simple
55license: MIT
src/Language/PureScript/CodeGen/JS.hs−1
@@ -180,7 +180,6 @@ statementToJs m e (VariableIntroduction ident value) = JSVariableIntroduction id
180180statementToJs m e (Assignment target value) = JSAssignment (JSAssignVariable target) (valueToJs m e value)
181181statementToJs m e (While cond sts) = JSWhile (valueToJs m e cond) (JSBlock (map (statementToJs m e) sts))
182182statementToJs m e (For ident start end sts) = JSFor ident (valueToJs m e start) (valueToJs m e end) (JSBlock (map (statementToJs m e) sts))
183−statementToJs m e (ForEach ident arr sts) = JSApp (JSAccessor "forEach" (valueToJs m e arr)) [JSFunction Nothing [ident] (JSBlock (map (statementToJs m e) sts))]
184183statementToJs m e (If ifst) = ifToJs ifst
185184 where
186185 ifToJs :: IfStatement -> JS
src/Language/PureScript/Parser/Values.hs−6
@@ -174,11 +174,6 @@ parseFor = For <$> (C.reserved "for" *> C.indented *> C.lexeme (P.char '(') *> C
174174 <*> (C.indented *> C.reserved "until" *> parseValue <* C.indented <* C.lexeme (P.char ')'))
175175 <*> parseManyStatements
176176
177−parseForEach :: P.Parsec String ParseState Statement
178−parseForEach = ForEach <$> (C.reserved "foreach" *> C.indented *> C.lexeme (P.char '(') *> C.indented *> C.parseIdent)
179− <*> (C.indented *> C.reserved "in" *> parseValue <* C.lexeme (P.char ')'))
180− <*> parseManyStatements
181−
182177parseIf :: P.Parsec String ParseState Statement
183178parseIf = If <$> parseIfStatement
184179
@@ -204,7 +199,6 @@ parseStatement = P.choice
204199 , parseAssignment
205200 , parseWhile
206201 , parseFor
207− , parseForEach
208202 , parseIf
209203 , parseValueStatement
210204 , parseReturn ] P.<?> "statement"
src/Language/PureScript/Pretty/Values.hs−3
@@ -191,9 +191,6 @@ prettyPrintStatement (For ident start end sts) = "for " ++ show ident
191191 ++ " <- " ++ prettyPrintValue start
192192 ++ " until " ++ prettyPrintValue end ++ ": {"
193193 ++ intercalate "; " (map prettyPrintStatement sts) ++ " }"
194−prettyPrintStatement (ForEach ident arr sts) = "foreach " ++ show ident
195− ++ " in " ++ prettyPrintValue arr ++ ": {"
196− ++ intercalate "; " (map prettyPrintStatement sts) ++ " }"
197194prettyPrintStatement (If ifst) = prettyPrintIfStatement ifst
198195prettyPrintStatement (ValueStatement val) = prettyPrintValue val
199196prettyPrintStatement (Return value) = "return " ++ prettyPrintValue value
src/Language/PureScript/TypeChecker/Types.hs−8
@@ -540,14 +540,6 @@ checkStatement mass ret (For ident start end inner) = do
540540 check end Number
541541 (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, Number)] $ checkBlock mass ret inner
542542 return (allCodePathsReturn, mass)
543−checkStatement mass ret (ForEach ident vals inner) = do
544− moduleName <- substCurrentModule <$> ask
545− assignVariable ident
546− val <- fresh
547− check vals (Array val)
548− (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, val)] $ checkBlock mass ret inner
549− guardWith "Cannot return from within a foreach block" $ not allCodePathsReturn
550− return (False, mass)
551543checkStatement mass _ (ValueStatement val) = do
552544 check val unit
553545 return (False, mass)
src/Language/PureScript/Values.hs−1
@@ -77,7 +77,6 @@ data Statement
7777 | Assignment Ident Value
7878 | While Value [Statement]
7979 | For Ident Value Value [Statement]
80− | ForEach Ident Value [Statement]
8180 | If IfStatement
8281 | ValueStatement Value
8382 | Return Value deriving (Show, Data, Typeable)