git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Better syntax for single function application
commitca13b3a9063badb8b3959727eabf7e1d7a342307 authorPhil Freeman <paf31@cantab.net> dateTue 22 Oct 2013, 16:09 (13 years ago) parent0ddd210
11 files changed+34−30
examples/Console.ps+2−2 examples/List.ps+4−4 examples/Maybe.ps+3−3 examples/Objects.ps+2−2 examples/Operators.ps+2−2 examples/ReadShow.ps+1−1 examples/TypeDecl.ps+3−3 examples/TypeSynonyms.ps+4−4 src/PureScript/CodeGen/JS.hs+1−1 src/PureScript/Parser/Common.hs+3−2 src/PureScript/Parser/Values.hs+9−6
examples/Console.ps+2−2
@@ -10,10 +10,10 @@ extern putStrLn :: String -> IO {}
1010
1111replicateM_ :: forall a. Number -> IO a -> IO {}
1212replicateM_ = \n -> \x -> do
13− var io = ret({})
13+ var io = ret {}
1414 for i <- 0 until n:
1515 io = x *> io
1616 return io
1717
18−main = replicateM_(10)(putStrLn("Hello World!"))
18+main = replicateM_ 10 (putStrLn "Hello World!")
1919
examples/List.ps+4−4
@@ -11,12 +11,12 @@ isNull = \l -> case l of
1111
1212concat = \l1 -> \l2 -> case l1 of
1313 Nil -> l2
14− Cons x -> Cons(
14+ Cons x -> Cons
1515 { head: x.head
16− , tail: concat(x.tail)(l2)
17− })
16+ , tail: concat x.tail l2
17+ }
1818
1919flatten = \xss -> case xss of
2020 Nil -> Nil
21− Cons cons -> concat(cons.head)(flatten(cons.tail))
21+ Cons cons -> concat cons.head (flatten cons.tail)
2222
examples/Maybe.ps+3−3
@@ -3,8 +3,8 @@ data Maybe a = Nothing | Just a
33maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
44maybe = \b -> \f -> \m -> case m of
55 Nothing -> b
6− Just a -> f(a)
6+ Just a -> f a
77
8−fmap = \m -> \f -> maybe(Nothing)(\a -> Just(f(a)))(m)
8+fmap = \m -> \f -> maybe Nothing (\a -> Just (f a)) m
99
10−bind = \m -> \f -> maybe(Nothing)(f)(m)
10+bind = \m -> \f -> maybe Nothing f m
examples/Objects.ps+2−2
@@ -4,6 +4,6 @@ append = \o -> { foo: o.foo, bar: 1 }
44
55apTest = append({foo : "Foo", baz: "Baz"})
66
7−f = (\a -> a.b.c)({ b: { c: 1, d: "Hello" }, e: "World" })
7+f = (\a -> a.b.c) { b: { c: 1, d: "Hello" }, e: "World" }
88
9−g = (\a -> a.f({ x: 1, y: "y" }))({ f: \o -> o.x + 1 })
9+g = (\a -> a.f { x: 1, y: "y" }) { f: \o -> o.x + 1 }
examples/Operators.ps+2−2
@@ -1,6 +1,6 @@
11test1 = \x, y, z -> x * y + z(x)(y)
22
3−test2 = (\x -> x.foo(false))({ foo : \_ -> 1 })
3+test2 = (\x -> x.foo false) { foo : \_ -> 1 }
44
55test3 = (\x, y -> x)(1 + 2 * (1 + 2), true && (false || false))
66
@@ -12,4 +12,4 @@ test4 = 1 `k` 2
1212
1313test5 = 1 %% 2 %% 3
1414
15−test6 = ((\x -> x) `k` 2) (3)
15+test6 = ((\x -> x) `k` 2) 3
examples/ReadShow.ps+1−1
@@ -2,4 +2,4 @@ extern read :: forall a. String -> a
22
33extern show :: forall a. a -> String
44
5−test = \x -> show(read(x))
5+test = \x -> show (read x)
examples/TypeDecl.ps+3−3
@@ -1,12 +1,12 @@
11id :: forall a. (a) -> a
22id = \x -> x
33
4−k :: (String) -> (Number) -> String
4+k :: String -> Number -> String
55k = \x -> \y -> x
66
7−iterate :: forall a. (Number) -> ((a) -> a) -> (a) -> a
7+iterate :: forall a. Number -> (a -> a) -> a -> a
88iterate = \n -> \f -> \a -> do
99 var result = a
1010 for i <- 0 until n:
11− result = f(result)
11+ result = f result
1212 return result
examples/TypeSynonyms.ps+4−4
@@ -5,9 +5,9 @@ type Lens a b =
55
66composeLenses :: forall a b c. Lens a b -> Lens b c -> Lens a c
77composeLenses = \l1 -> \l2 ->
8− { get: \a -> l2.get(l1.get(a))
8+ { get: \a -> l2.get (l1.get a)
99 , set: \a -> \c -> do
10− var b = l1.get(a)
11− var b1 = l2.set(b)(c)
12− return l1.set(a)(b1)
10+ var b = l1.get a
11+ var b1 = l2.set b c
12+ return l1.set a b1
1313 }
src/PureScript/CodeGen/JS.hs+1−1
@@ -170,7 +170,7 @@ binderToJs :: String -> String -> Binder -> Gen String
170170binderToJs varName done (StringBinder str) =
171171 return $ "if (" ++ varName ++ " === \"" ++ str ++ "\") {" ++ done ++ " }"
172172binderToJs varName done (NumberBinder num) =
173− return $ "if (" ++ varName ++ " === \"" ++ either show show num ++ "\") {" ++ done ++ " }"
173+ return $ "if (" ++ varName ++ " === " ++ either show show num ++ ") {" ++ done ++ " }"
174174binderToJs varName done (BooleanBinder True) =
175175 return $ "if (" ++ varName ++ ") {" ++ done ++ " }"
176176binderToJs varName done (BooleanBinder False) =
src/PureScript/Parser/Common.hs+3−2
@@ -28,6 +28,7 @@ import PureScript.Names
2828
2929reservedNames :: [String]
3030reservedNames = [ "case"
31+ , "of"
3132 , "data"
3233 , "type"
3334 , "var"
@@ -69,7 +70,7 @@ reservedNames = [ "case"
6970
7071reservedOpNames :: [String]
7172reservedOpNames = [ "!", "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
72− , "==", "!=", "&", "^", "|", "&&", "||" ]
73+ , "==", "!=", "&", "^", "|", "&&", "||", "->" ]
7374
7475identStart :: P.Parsec String u Char
7576identStart = P.lower <|> P.oneOf "_$"
@@ -129,7 +130,7 @@ properName :: P.Parsec String u String
129130properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
130131
131132integerOrFloat :: P.Parsec String u (Either Integer Double)
132−integerOrFloat = Left <$> P.try (PT.integer tokenParser) <|>
133+integerOrFloat = Left <$> P.try (PT.natural tokenParser) <|>
133134 Right <$> P.try (PT.float tokenParser)
134135
135136augment :: P.Stream s m t => P.ParsecT s u m a -> P.ParsecT s u m b -> (a -> b -> a) -> P.ParsecT s u m a
src/PureScript/Parser/Values.hs+9−6
@@ -95,15 +95,18 @@ parseValueAtom = C.indented *> P.choice (map P.try
9595 , C.parens parseValue ])
9696
9797parseValue :: P.Parsec String P.Column Value
98−parseValue = buildExpressionParser operators . C.buildPostfixParser postfixTable $ parseValueAtom
98+parseValue = buildExpressionParser operators . C.buildPostfixParser postfixTable2 $ indexersAndAccessors
9999 where
100− postfixTable = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
101− , Indexer <$> (C.indented *> C.squares parseValue)
102− , flip App <$> (C.indented *> C.parens (parseValue `P.sepBy` (C.indented *> C.comma)))
103− , flip TypedValue <$> (P.try $ C.lexeme (C.indented *> P.string "::") *> parsePolyType) ]
100+ indexersAndAccessors = C.buildPostfixParser postfixTable1 $ parseValueAtom
101+ postfixTable1 = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
102+ , Indexer <$> (C.indented *> C.squares parseValue) ]
103+ postfixTable2 = [ C.indented *> indexersAndAccessors >>= \t2 -> return (\t1 -> App t1 [t2])
104+ , flip App <$> (C.indented *> C.parens (parseValue `P.sepBy` (C.indented *> C.comma)))
105+ , flip TypedValue <$> (P.try $ C.lexeme (C.indented *> P.string "::") *> parsePolyType) ]
104106 operators = [ [ Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "!") >> return (Unary Not)
105107 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "~") >> return (Unary BitwiseNot)
106− , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate) ]
108+ , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate)
109+ , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "+") >> return id ]
107110 , [ Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix) >>= \ident -> return $ \t1 t2 -> App (App (Var ident) [t1]) [t2])) AssocRight ]
108111 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<=") >> return (Binary LessThanOrEqualTo)) AssocRight
109112 , Infix (C.lexeme (P.try $ C.indented *> C.reservedOp ">=") >> return (Binary GreaterThanOrEqualTo)) AssocRight ]