Embedded programming language for Zig
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix bug in `lambda`, and add `map`

IamPyu 0637f7e5 f0f0f0b2

+20 -4
+15 -4
src/std/core.zig
··· 39 39 try scope.insert("clone", try Value.initNativeFunc(scope, clone)); 40 40 try scope.insert("list", try Value.initNativeFunc(scope, list)); 41 41 try scope.insert("append", try Value.initNativeFunc(scope, append)); 42 + try scope.insert("map", try Value.initNativeFunc(scope, map)); 42 43 } 43 44 44 45 // -- MACROS -- ··· 131 132 var iter = Value.Iter.init(arglist); 132 133 133 134 while (iter.next()) |current| { 134 - const sym = current.getCar() orelse break; 135 + const sym = current; 135 136 if (sym.getType() != .sym) { 136 137 continue; 137 138 } ··· 409 410 410 411 pub fn map(scope: *Scope, args: []const *Value) RuntimeError!*Value { 411 412 if (args.len != 2) { 412 - return Value.initNil(scope); 413 + return RuntimeError.Exception; 413 414 } 414 415 415 - // TODO: mapcar 416 - return Value.initNil(scope); 416 + var func = args[0]; 417 + 418 + const iterable = try args[1].clone(scope); 419 + var iter = Value.Iter.init(iterable); 420 + 421 + while (iter.next()) |v| { 422 + scope.getCallStack().append(func) catch return RuntimeError.External; 423 + const out = try func.call(scope, &.{v}); 424 + v.* = out.*; 425 + } 426 + 427 + return iterable; 417 428 }
+5
src/test/map.zexa
··· 1 + (define 'my-list (list 1 2 3 4)) 2 + (define 'mapped-list (map (lambda (x) (mul x 2)) my-list)) 3 + 4 + (print my-list) 5 + (print mapped-list)