diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-06-09 23:35:38 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-06-09 23:35:38 -0700 |
commit | 864d1c6fe182661a7bd7d4eda928f8a19318b651 (patch) | |
tree | a26ede9a8bf91e7b42aef355e19a37980233c065 /lib.c | |
parent | e57c4d0857fcf8b4e4369bde72c7453d2ae98125 (diff) | |
download | txr-864d1c6fe182661a7bd7d4eda928f8a19318b651.tar.gz txr-864d1c6fe182661a7bd7d4eda928f8a19318b651.tar.bz2 txr-864d1c6fe182661a7bd7d4eda928f8a19318b651.zip |
reduce-left: rewrite using seq_iter.
* lib.c (reduce_left): Use sequence iteration instead of list
operations.
* txr.1: Add a note to the documentation.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -7902,22 +7902,26 @@ val funcall4(val fun, val arg1, val arg2, val arg3, val arg4) wrongargs(fun); } -val reduce_left(val fun, val list, val init, val key) +val reduce_left(val fun, val seq, val init, val key) { + val self = lit("reduce-left"); + seq_iter_t item_iter; + val item; + if (null_or_missing_p(key)) key = identity_f; - list = nullify(list); + seq_iter_init(self, &item_iter, seq); if (missingp(init)) { - if (list) - init = funcall1(key, pop(&list)); + if (seq_get(&item_iter, &item)) + init = funcall1(key, item); else return funcall(fun); } - for (; list; list = cdr(list)) - init = funcall2(fun, init, funcall1(key, car(list))); + while (seq_get(&item_iter, &item)) + init = funcall2(fun, init, funcall1(key, item)); return init; } |