From 909cf1ba9001f52afbbb6f7cfb6c919869c068ac Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 2 Jan 2023 18:58:35 +0100 Subject: add inc!, dec! --- mew.scm | 18 ++++++++++++++++-- mew.svnwiki | 5 +++++ tests/test.mew | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mew.scm b/mew.scm index 7707851..845a16e 100644 --- a/mew.scm +++ b/mew.scm @@ -3,13 +3,13 @@ act accumulate andloc app at boolean comp cross-product - dec def del-at div dup + dec dec! def del-at div dup empty? eof esc fail fin final for for/into fun* gconcatenate gen generator-xfold generic-for-each genumerate get gfix giterate gmatch gpick group-by-accumulator gslice-when gsplit gsplit-on gwindow - imp inc inject inject-accumulator into + imp inc inc! inject inject-accumulator into juxt keys len lines loc @@ -1063,6 +1063,20 @@ ((_ location . fs) (set location (=> location . fs))))) + (define-syntax inc! + (syntax-rules () + ((_ location) + (inc! location 1)) + ((_ location n) + (set location (+ location n))))) + + (define-syntax dec! + (syntax-rules () + ((_ location) + (dec! location 1)) + ((_ location n) + (set location (- location n))))) + (define (and=> x . fs) (and x (if (null? fs) diff --git a/mew.svnwiki b/mew.svnwiki index 00b4724..16dd90d 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -144,6 +144,11 @@ with a {{'message}} of {{}} passed through {{format}} with Increment or decrement the argument by 1. +(inc! []) +(dec! []) + +Increment or decrement the location {{}} by {{}} (default: 1). + (div ) Alias for {{floor-quotient}}. diff --git a/tests/test.mew b/tests/test.mew index b7e5da7..44fdb95 100644 --- a/tests/test.mew +++ b/tests/test.mew @@ -97,6 +97,11 @@ (test 3/2 (inc 1/2)) (test-error (inc "foo"))) +(test-group "inc!" + (test 7 (loc (x 5) (inc! x) (inc! x) x)) + (test 6 (loc (x 5) (inc! x))) + (test 7 (loc (x 5) (inc! x 2)))) + (test-group "dec" (test 4 (dec 5)) (test -2 (dec -1)) @@ -104,6 +109,11 @@ (test -1/2 (dec 1/2)) (test-error (dec "foo"))) +(test-group "dec!" + (test 5 (loc (x 7) (dec! x) (dec! x) x)) + (test 5 (loc (x 6) (dec! x))) + (test 5 (loc (x 7) (dec! x 2)))) + (test-group "boolean" (test #f (boolean #f)) (test #t (boolean #t)) -- cgit 1.4.1