From 2434b4c54ba42c153a51ba96ee24573be297c4ee Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 27 Jun 2014 21:14:43 -0700 Subject: * lib.c (where, sel): Extend into hashes. * txr.1: document hash support for select and where. --- lib.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index e84b098f..8a6f9ed2 100644 --- a/lib.c +++ b/lib.c @@ -5537,13 +5537,23 @@ val search(val seq, val key, val testfun, val keyfun) val where(val seq_in, val func) { list_collect_decl (out, ptail); - val seq = nullify(seq_in); - val idx = zero; - for (; seq; seq = cdr(seq), idx = plus(idx, one)) { - val elt = car(seq); - if (funcall1(func, elt)) - ptail = list_collect(ptail, idx); + if (hashp(seq_in)) { + val hiter = hash_begin(seq_in); + val cell; + + while ((cell = hash_next(hiter))) + if (funcall1(func, cdr(cell))) + ptail = list_collect(ptail, car(cell)); + } else { + val seq = nullify(seq_in); + val idx = zero; + + for (; seq; seq = cdr(seq), idx = plus(idx, one)) { + val elt = car(seq); + if (funcall1(func, elt)) + ptail = list_collect(ptail, idx); + } } return out; @@ -5576,6 +5586,24 @@ val sel(val seq_in, val where_in) } } break; + case COBJ: + if (!hashp(seq)) + type_mismatch(lit("select: ~s is not a sequence or hash"), seq, nao); + { + val newhash = make_similar_hash(seq); + + for (; where; where = cdr(where)) { + val found; + loc pfound = mkcloc(found); + val key = car(where); + val value = gethash_f(seq, car(where), pfound); + + if (found) + sethash(newhash, key, value); + } + + return newhash; + } default: { val len = length(seq); -- cgit v1.2.3