git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #373 from garyb/array-functions

Array function rearranging

commitf67256715f1e17b0e915e4c93dc7d04a7d1df2d6 authorPhil Freeman <paf31@cantab.net> dateFri 11 Apr 2014, 12:18 (13 years ago) parent9e9d1f51bfd354
4 files changed+49−19
prelude/prelude.purs+35−8 src/Language/PureScript/Constants.hs+6−3 src/Language/PureScript/Optimizer.hs+3−3 src/Language/PureScript/Optimizer/Inliner.hs+5−5
prelude/prelude.purs+35−8
@@ -39,6 +39,18 @@ module Prelude where
3939 (#) :: forall a b. a -> (a -> b) -> b
4040 (#) x f = f x
4141
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+
4254 class Show a where
4355 show :: a -> String
4456
@@ -61,6 +73,20 @@ module Prelude where
6173 instance showNumber :: Show Number where
6274 show = showNumberImpl
6375
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+
6490 infixl 4 <$>
6591
6692 class Functor f where
@@ -317,14 +343,6 @@ module Prelude where
317343 zshr = numZshr
318344 complement = numComplement
319345
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−
328346 infixr 2 ||
329347 infixr 3 &&
330348
@@ -385,6 +403,15 @@ module Data.Eq where
385403 (==) = liftRef refEq
386404 (/=) = liftRef refIneq
387405
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+
388415module Control.Monad.Eff where
389416
390417 foreign import data Eff :: # ! -> * -> *
src/Language/PureScript/Constants.hs+6−3
@@ -23,9 +23,6 @@ module Language.PureScript.Constants where
2323(#) :: String
2424(#) = "#"
2525
26−(!!) :: String
27−(!!) = "!!"
28−
2926(++) :: String
3027(++) = "++"
3128
@@ -80,6 +77,9 @@ bar = "|"
8077(||) :: String
8178(||) = "||"
8279
80+unsafeIndex :: String
81+unsafeIndex = "unsafeIndex"
82+
8383-- Prelude Operator Functions
8484
8585negate :: String
@@ -194,6 +194,9 @@ prim = "Prim"
194194prelude :: String
195195prelude = "Prelude"
196196
197+preludeUnsafe :: String
198+preludeUnsafe = "Prelude_Unsafe"
199+
197200eff :: String
198201eff = "Control_Monad_Eff"
199202
src/Language/PureScript/Optimizer.hs+3−3
@@ -63,9 +63,9 @@ optimize opts | optionsNoOptimizations opts = id
6363 , etaConvert
6464 , evaluateIifes
6565 , inlineVariables
66− , inlineOperator (C.$) $ \f x -> JSApp f [x]
67− , inlineOperator (C.#) $ \x f -> JSApp f [x]
68− , inlineOperator (C.!!) $ flip JSIndexer
66+ , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
67+ , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
68+ , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
6969 , inlineCommonOperators ])
7070
7171untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
src/Language/PureScript/Optimizer/Inliner.hs+5−5
@@ -75,15 +75,15 @@ inlineVariables = everywhere (mkT $ removeFromBlock go)
7575 go (replaceIdent var js sts)
7676 go (s:sts) = s : go sts
7777
78−inlineOperator :: String -> (JS -> JS -> JS) -> JS -> JS
79−inlineOperator op f = everywhere (mkT convert)
78+inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
79+inlineOperator (m, op) f = everywhere (mkT convert)
8080 where
8181 convert :: JS -> JS
8282 convert (JSApp (JSApp op' [x]) [y]) | isOp op' = f x y
8383 convert other = other
84− isOp (JSAccessor longForm (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
85− _ps == C._ps &&
86− longForm == identToJs (Op op) = True
84+ isOp (JSAccessor longForm (JSAccessor m' (JSVar _ps))) | m == m' &&
85+ _ps == C._ps &&
86+ longForm == identToJs (Op op) = True
8787 isOp (JSIndexer (JSStringLiteral op') (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
8888 _ps == C._ps &&
8989 op == op' = True