From daa29ee819aee2940818b3bc276f5cfb99064b85 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 30 Jan 2023 11:18:37 +0100 Subject: [PATCH] Add library procedures: map, foreach, first, rest. --- aocla.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aocla.c b/aocla.c index d52d38a..f96701f 100644 --- a/aocla.c +++ b/aocla.c @@ -1045,7 +1045,18 @@ void loadLibrary(aoclactx *ctx) { addProcString(ctx,"dup","[(x) $x $x]"); addProcString(ctx,"swap","[(x y) $y $x]"); addProcString(ctx,"drop","[(_)]"); + + /* [1 2 3] [dup *] map => [1 4 9] */ addProcString(ctx,"map", "[(l f) $l len (e) 0 (j) [] [$j $e <] [ $l $j get@ $f upeval swap -> $j 1 + (j)] while]"); + + /* [1 2 3] [printnl] foreach */ + addProcString(ctx,"foreach"," [(l f) $l len (e) 0 (j) [$j $e <] [$l $j get@ $f upeval $j 1 + (j)] while]"); + + /* [1 2 3] first => 1 */ + addProcString(ctx,"first","[0 get@]"); + + /* [1 2 3] rest => [2 3] */ + addProcString(ctx,"rest","[#t (f) [] (n) [[$f] [#f (f) drop] [$n -> (n)] ifelse] foreach $n]"); } /* ================================ CLI ===================================== */