mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
Reduce!
This commit is contained in:
parent
414c772f9a
commit
25f9892e94
@ -41,6 +41,7 @@ arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName =
|
||||
, templateMap
|
||||
, templateEMap
|
||||
, templateFilter
|
||||
, templateReduce
|
||||
, templateRaw
|
||||
, templateAset
|
||||
, templateAsetBang
|
||||
|
@ -36,13 +36,13 @@ bool not(bool b) {
|
||||
return !b;
|
||||
}
|
||||
|
||||
#define Int__PLUS_(x, y) ((x) + (y))
|
||||
#define Int__MINUS_(x, y) ((x) - (y))
|
||||
#define Int__MUL_(x, y) ((x) * (y))
|
||||
#define Int__DIV_(x, y) ((x) / (y))
|
||||
#define Int__EQ_(x, y) ((x) == (y))
|
||||
#define Int__LT_(x, y) ((x) < (y))
|
||||
#define Int__GT_(x, y) ((x) > (y))
|
||||
int Int__PLUS_(x, y) { return x + y; }
|
||||
int Int__MINUS_(x, y) { return x - y; }
|
||||
int Int__MUL_(x, y) { return x * y; }
|
||||
int Int__DIV_(x, y) { return x / y; }
|
||||
int Int__EQ_(x, y) { return x == y; }
|
||||
int Int__LT_(x, y) { return x < y; }
|
||||
int Int__GT_(x, y) { return x > y; }
|
||||
|
||||
int Int_inc(int x) { return x + 1; }
|
||||
int Int_dec(int x) { return x - 1; }
|
||||
|
@ -15,6 +15,6 @@
|
||||
|
||||
(defn main []
|
||||
(while true
|
||||
(let [stuff [1 2 3 4 5 6 7 8 9 10]
|
||||
after (emap square (filter even? stuff))]
|
||||
(println (refstr &doom)))))
|
||||
(let [stuff [3 5 8 9 10]
|
||||
after (reduce Int.+ 0 (emap square (filter even? stuff)))]
|
||||
(println (ref (Int.str after))))))
|
||||
|
@ -206,6 +206,34 @@ templateFilter = defineTypeParameterizedTemplate templateCreator path t
|
||||
(\(FuncTy [t@(FuncTy [insideType] BoolTy), arrayType] _) ->
|
||||
[defineFunctionTypeAlias t, defineArrayTypeAlias arrayType] ++ insideArrayDeleteDeps typeEnv env insideType)
|
||||
|
||||
templateReduce :: (String, Binder)
|
||||
templateReduce = defineTypeParameterizedTemplate templateCreator path t
|
||||
where
|
||||
fTy = FuncTy [bTy, aTy] bTy
|
||||
arrTy = StructTy "Array" [aTy]
|
||||
aTy = VarTy "a"
|
||||
bTy = VarTy "b"
|
||||
path = SymPath ["Array"] "reduce"
|
||||
t = (FuncTy [fTy, bTy, arrTy] bTy)
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "$b $NAME($(Fn [b a] a) f, $b initial_value, Array a)"))
|
||||
(\(FuncTy [(FuncTy [_, _] _), _, _] _) ->
|
||||
(toTemplate $ unlines $
|
||||
[ "$DECL { "
|
||||
, " $b b = initial_value;"
|
||||
, " for(int i = 0; i < a.len; ++i) {"
|
||||
, " b = f(b, (($a*)a.data)[i]);"
|
||||
, " }"
|
||||
, " CARP_FREE(a.data); // Can't call Array_delete since it will destroy the items that have been handed off to f()."
|
||||
, " return b;"
|
||||
, "}"
|
||||
]))
|
||||
(\(FuncTy [ft@(FuncTy [_, _] _), _, arrayType] _) ->
|
||||
[defineFunctionTypeAlias ft, defineArrayTypeAlias arrayType])
|
||||
|
||||
templatePushBack :: (String, Binder)
|
||||
templatePushBack =
|
||||
let aTy = StructTy "Array" [VarTy "a"]
|
||||
|
Loading…
Reference in New Issue
Block a user