mirror of
https://github.com/carp-lang/Carp.git
synced 2024-10-12 04:58:18 +03:00
Filter-function.
This commit is contained in:
parent
48ea80d3d0
commit
5d0c549d49
@ -40,6 +40,7 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
|
|||||||
, templateRepeat
|
, templateRepeat
|
||||||
, templateMap
|
, templateMap
|
||||||
, templateEMap
|
, templateEMap
|
||||||
|
, templateFilter
|
||||||
, templateRaw
|
, templateRaw
|
||||||
, templateAset
|
, templateAset
|
||||||
, templateAsetBang
|
, templateAsetBang
|
||||||
|
@ -10,7 +10,10 @@
|
|||||||
(defn square [x]
|
(defn square [x]
|
||||||
(Int.* x x))
|
(Int.* x x))
|
||||||
|
|
||||||
|
(defn even? [x]
|
||||||
|
(= (Int.mod x 2) 0))
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(let [stuff [1 2 3 4 5]
|
(let [stuff [1 2 3 4 5 6 7 8 9 10]
|
||||||
after (emap square stuff)]
|
after (emap square (filter even? stuff))]
|
||||||
(println (refstr &stuff))))
|
(println (refstr &after))))
|
||||||
|
@ -86,7 +86,7 @@ toTemplate text = case Parsec.runParser templateSyntax 0 "(template)" text of
|
|||||||
parseTokC = do s <- Parsec.many1 validInSymbol
|
parseTokC = do s <- Parsec.many1 validInSymbol
|
||||||
return (TokC s)
|
return (TokC s)
|
||||||
where validInSymbol = Parsec.choice [Parsec.letter, Parsec.digit, Parsec.oneOf validCharactersInTemplate]
|
where validInSymbol = Parsec.choice [Parsec.letter, Parsec.digit, Parsec.oneOf validCharactersInTemplate]
|
||||||
validCharactersInTemplate = " ><{}()[]|;.,_-+*#/'^!?€%&=@\"\n\t"
|
validCharactersInTemplate = " ><{}()[]|;:.,_-+*#/'^!?€%&=@\"\n\t"
|
||||||
|
|
||||||
parseTokTy :: Parsec.Parsec String Int Token
|
parseTokTy :: Parsec.Parsec String Int Token
|
||||||
parseTokTy = do _ <- Parsec.char '$'
|
parseTokTy = do _ <- Parsec.char '$'
|
||||||
@ -174,6 +174,38 @@ templateEMap =
|
|||||||
])
|
])
|
||||||
(\(FuncTy [t, arrayType] _) -> [defineFunctionTypeAlias t, defineArrayTypeAlias arrayType])
|
(\(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 :: (String, Binder)
|
||||||
templatePushBack =
|
templatePushBack =
|
||||||
let aTy = StructTy "Array" [VarTy "a"]
|
let aTy = StructTy "Array" [VarTy "a"]
|
||||||
|
Loading…
Reference in New Issue
Block a user