Filter-function.

This commit is contained in:
Erik Svedäng 2017-06-30 10:36:37 +02:00
parent 48ea80d3d0
commit 5d0c549d49
3 changed files with 40 additions and 4 deletions

View File

@ -40,6 +40,7 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
, templateRepeat
, templateMap
, templateEMap
, templateFilter
, templateRaw
, templateAset
, templateAsetBang

View File

@ -10,7 +10,10 @@
(defn square [x]
(Int.* x x))
(defn even? [x]
(= (Int.mod x 2) 0))
(defn main []
(let [stuff [1 2 3 4 5]
after (emap square stuff)]
(println (refstr &stuff))))
(let [stuff [1 2 3 4 5 6 7 8 9 10]
after (emap square (filter even? stuff))]
(println (refstr &after))))

View File

@ -86,7 +86,7 @@ toTemplate text = case Parsec.runParser templateSyntax 0 "(template)" text of
parseTokC = do s <- Parsec.many1 validInSymbol
return (TokC s)
where validInSymbol = Parsec.choice [Parsec.letter, Parsec.digit, Parsec.oneOf validCharactersInTemplate]
validCharactersInTemplate = " ><{}()[]|;.,_-+*#/'^!?€%&=@\"\n\t"
validCharactersInTemplate = " ><{}()[]|;:.,_-+*#/'^!?€%&=@\"\n\t"
parseTokTy :: Parsec.Parsec String Int Token
parseTokTy = do _ <- Parsec.char '$'
@ -174,6 +174,38 @@ templateEMap =
])
(\(FuncTy [t, arrayType] _) -> [defineFunctionTypeAlias t, defineArrayTypeAlias arrayType])
templateFilter :: (String, Binder)
templateFilter = defineTypeParameterizedTemplate templateCreator path t
where
fTy = FuncTy [VarTy "a"] BoolTy
aTy = StructTy "Array" [VarTy "a"]
path = SymPath ["Array"] "filter"
t = (FuncTy [fTy, aTy] aTy)
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "Array $NAME($(Fn [a] Bool) predicate, Array a)"))
(\(FuncTy [(FuncTy [insideTy] BoolTy), aTy] _) ->
(toTemplate $ unlines $
let deleter = insideArrayDeletion env insideTy
in ["$DECL { "
, " int insertIndex = 0;"
, " for(int i = 0; i < a.len; ++i) {"
, " if(predicate((($a*)a.data)[i])) {"
, " ((($a*)a.data)[insertIndex++]) = (($a*)a.data)[i];"
, " } else {"
, " " ++ deleter
, " }"
, " }"
, " a.len = insertIndex;"
, " // NOTE: the array isn't resized for now, it probably should be?"
, " return a;"
, "}"
]))
(\(FuncTy [t@(FuncTy [insideType] BoolTy), arrayType] _) ->
[defineFunctionTypeAlias t, defineArrayTypeAlias arrayType] ++ insideArrayDeleteDeps typeEnv env insideType)
templatePushBack :: (String, Binder)
templatePushBack =
let aTy = StructTy "Array" [VarTy "a"]