@@ -27,7 +27,7 @@ import Data.Maybe (catMaybes, fromJust)
2727import Data.Function (on)
2828import Data.List (nub, (\\), delete, sortBy)
2929
30−import Control.Monad (replicateM, forM)
30+import Control.Monad (foldM, replicateM, forM)
3131import Control.Applicative
3232
3333import Language.PureScript.Names
@@ -392,18 +392,27 @@ binderToJs m e varName done (ArrayBinder bs) = do
392392 done'' <- go done' (index + 1) bs'
393393 js <- binderToJs m e elVar done'' binder
394394 return (JSVariableIntroduction elVar (Just (JSIndexer (JSNumericLiteral (Left index)) (JSVar varName))) : js)
395−binderToJs m e varName done (ConsBinder headBinder tailBinder) = do
396− headVar <- freshName
395+binderToJs m e varName done binder@(ConsBinder _ _) = do
396+ let (headBinders, tailBinder) = uncons [] binder
397+ numberOfHeadBinders = fromIntegral $ length headBinders
398+ js1 <- foldM (\done' (headBinder, index) -> do
399+ headVar <- freshName
400+ jss <- binderToJs m e headVar done' headBinder
401+ return (JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left index)) (JSVar varName))) : jss)) done (zip headBinders [0..])
397402 tailVar <- freshName
398− js1 <- binderToJs m e headVar done headBinder
399403 js2 <- binderToJs m e tailVar js1 tailBinder
400− return [JSIfElse (JSBinary GreaterThan (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left 0))) (JSBlock
401− ( JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left 0)) (JSVar varName))) :
402− JSVariableIntroduction tailVar (Just (JSApp (JSAccessor "slice" (JSVar varName)) [JSNumericLiteral (Left 1)])) :
404+ return [JSIfElse (JSBinary GreaterThanOrEqualTo (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left numberOfHeadBinders))) (JSBlock
405+ ( JSVariableIntroduction tailVar (Just (JSApp (JSAccessor "slice" (JSVar varName)) [JSNumericLiteral (Left numberOfHeadBinders)])) :
403406 js2
404407 )) Nothing]
408+ where
409+ uncons :: [Binder] -> Binder -> ([Binder], Binder)
410+ uncons acc (ConsBinder h t) = uncons (h : acc) t
411+ uncons acc (PositionedBinder _ b) = uncons acc b
412+ uncons acc tailBinder = (reverse acc, tailBinder)
405413binderToJs m e varName done (NamedBinder ident binder) = do
406414 js <- binderToJs m e varName done binder
407415 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
408416binderToJs m e varName done (PositionedBinder _ binder) =
409417 binderToJs m e varName done binder
418+