git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #373 from garyb/array-functions
f672567Phil Freeman
prelude/prelude.purs+35−8
@@ -39,6 +39,18 @@ module Prelude where
39 (#) :: forall a b. a -> (a -> b) -> b
39 (#) :: forall a b. a -> (a -> b) -> b
40 (#) x f = f x
40 (#) x f = f x
41
41
42+ infixr 6 :
43+
44+ (:) :: forall a. a -> [a] -> [a]
45+ (:) = cons
46+
47+ foreign import cons
48+ "function cons(e) {\
49+ \ return function (l) {\
50+ \ return [e].concat(l);\
51+ \ };\
52+ \}" :: forall a. a -> [a] -> [a]
53+
42 class Show a where
54 class Show a where
43 show :: a -> String
55 show :: a -> String
44
56
@@ -61,6 +73,20 @@ module Prelude where
61 instance showNumber :: Show Number where
73 instance showNumber :: Show Number where
62 show = showNumberImpl
74 show = showNumberImpl
63
75
76+ foreign import showArrayImpl
77+ "function showArrayImpl (f) {\
78+ \ return function (xs) {\
79+ \ var ss = [];\
80+ \ for (var i = 0, l = xs.length; i < l; i++) {\
81+ \ ss[i] = f(xs[i]);\
82+ \ }\
83+ \ return '[' + ss.join(',') + ']';\
84+ \ };\
85+ \}" :: forall a. (a -> String) -> [a] -> String
86+
87+ instance showArray :: (Show a) => Show [a] where
88+ show = showArrayImpl show
89+
64 infixl 4 <$>
90 infixl 4 <$>
65
91
66 class Functor f where
92 class Functor f where
@@ -317,14 +343,6 @@ module Prelude where
317 zshr = numZshr
343 zshr = numZshr
318 complement = numComplement
344 complement = numComplement
319
345
320− infixl 8 !!
321−
322− foreign import (!!) "function $bang$bang(xs) {\
323− \ return function(n) {\
324− \ return xs[n];\
325− \ };\
326− \}" :: forall a. [a] -> Number -> a
327−
328 infixr 2 ||
346 infixr 2 ||
329 infixr 3 &&
347 infixr 3 &&
330
348
@@ -385,6 +403,15 @@ module Data.Eq where
385 (==) = liftRef refEq
403 (==) = liftRef refEq
386 (/=) = liftRef refIneq
404 (/=) = liftRef refIneq
387
405
406+module Prelude.Unsafe where
407+
408+ foreign import unsafeIndex
409+ "function unsafeIndex(xs) {\
410+ \ return function(n) {\
411+ \ return xs[n];\
412+ \ };\
413+ \}" :: forall a. [a] -> Number -> a
414+
388module Control.Monad.Eff where
415module Control.Monad.Eff where
389
416
390 foreign import data Eff :: # ! -> * -> *
417 foreign import data Eff :: # ! -> * -> *
src/Language/PureScript/Constants.hs+6−3
@@ -23,9 +23,6 @@ module Language.PureScript.Constants where
23(#) :: String
23(#) :: String
24(#) = "#"
24(#) = "#"
25
25
26−(!!) :: String
27−(!!) = "!!"
28−
29(++) :: String
26(++) :: String
30(++) = "++"
27(++) = "++"
31
28
@@ -80,6 +77,9 @@ bar = "|"
80(||) :: String
77(||) :: String
81(||) = "||"
78(||) = "||"
82
79
80+unsafeIndex :: String
81+unsafeIndex = "unsafeIndex"
82+
83-- Prelude Operator Functions
83-- Prelude Operator Functions
84
84
85negate :: String
85negate :: String
@@ -194,6 +194,9 @@ prim = "Prim"
194prelude :: String
194prelude :: String
195prelude = "Prelude"
195prelude = "Prelude"
196
196
197+preludeUnsafe :: String
198+preludeUnsafe = "Prelude_Unsafe"
199+
197eff :: String
200eff :: String
198eff = "Control_Monad_Eff"
201eff = "Control_Monad_Eff"
199
202
src/Language/PureScript/Optimizer.hs+3−3
@@ -63,9 +63,9 @@ optimize opts | optionsNoOptimizations opts = id
63 , etaConvert
63 , etaConvert
64 , evaluateIifes
64 , evaluateIifes
65 , inlineVariables
65 , inlineVariables
66− , inlineOperator (C.$) $ \f x -> JSApp f [x]
66+ , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
67− , inlineOperator (C.#) $ \x f -> JSApp f [x]
67+ , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
68− , inlineOperator (C.!!) $ flip JSIndexer
68+ , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
69 , inlineCommonOperators ])
69 , inlineCommonOperators ])
70
70
71untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
71untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
src/Language/PureScript/Optimizer/Inliner.hs+5−5
@@ -75,15 +75,15 @@ inlineVariables = everywhere (mkT $ removeFromBlock go)
75 go (replaceIdent var js sts)
75 go (replaceIdent var js sts)
76 go (s:sts) = s : go sts
76 go (s:sts) = s : go sts
77
77
78−inlineOperator :: String -> (JS -> JS -> JS) -> JS -> JS
78+inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
79−inlineOperator op f = everywhere (mkT convert)
79+inlineOperator (m, op) f = everywhere (mkT convert)
80 where
80 where
81 convert :: JS -> JS
81 convert :: JS -> JS
82 convert (JSApp (JSApp op' [x]) [y]) | isOp op' = f x y
82 convert (JSApp (JSApp op' [x]) [y]) | isOp op' = f x y
83 convert other = other
83 convert other = other
84− isOp (JSAccessor longForm (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
84+ isOp (JSAccessor longForm (JSAccessor m' (JSVar _ps))) | m == m' &&
85− _ps == C._ps &&
85+ _ps == C._ps &&
86− longForm == identToJs (Op op) = True
86+ longForm == identToJs (Op op) = True
87 isOp (JSIndexer (JSStringLiteral op') (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
87 isOp (JSIndexer (JSStringLiteral op') (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
88 _ps == C._ps &&
88 _ps == C._ps &&
89 op == op' = True
89 op == op' = True