From b8b1c891f17d26cf3b13e77c1657cd855ae6543b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 11 Jan 2015 09:34:29 -0800 Subject: * eval.c (merge_wrap): Make generic over sequences. * txr.1: Update documentation for merge function. --- ChangeLog | 6 ++++++ eval.c | 23 ++++++++++++++++++----- txr.1 | 21 +++++++++++---------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0747d675..52c8a16f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2015-01-11 Kaz Kylheku + + * eval.c (merge_wrap): Make generic over sequences. + + * txr.1: Update documentation for merge function. + 2015-01-05 Kaz Kylheku * match.c (h_trailer): Bugfix: not returning new variable diff --git a/eval.c b/eval.c index 71f2470e..b02e4d0b 100644 --- a/eval.c +++ b/eval.c @@ -3399,12 +3399,25 @@ static val pprinl(val obj, val stream) return ret; } -static val merge_wrap(val list1, val list2, val lessfun, val keyfun) -{ - keyfun = default_arg(keyfun, identity_f); - lessfun = default_arg(lessfun, less_f); +static val merge_wrap(val seq1, val seq2, val lessfun, val keyfun) +{ + if (!nullify(seq1)) { + if (type(seq1) == type(seq2)) + return seq2; + return make_like(tolist(seq2), seq1); + } else if (!nullify(seq2)) { + if (type(seq1) == type(seq2)) + return seq1; + return make_like(tolist(seq1), seq1); + } else { + val list1 = tolist(seq1); + val list2 = tolist(seq2); - return merge(list1, list2, lessfun, keyfun); + keyfun = default_arg(keyfun, identity_f); + lessfun = default_arg(lessfun, less_f); + + return make_like(merge(list1, list2, lessfun, keyfun), seq1); + } } void eval_init(void) diff --git a/txr.1 b/txr.1 index 5f886a8d..8b329d7b 100644 --- a/txr.1 +++ b/txr.1 @@ -14786,26 +14786,27 @@ value. .SS* List Sorting .coNP Function @ merge .synb -.mets (merge < list1 < list2 >> [ lessfun <> [ keyfun ]]) +.mets (merge < seq1 < seq2 >> [ lessfun <> [ keyfun ]]) .syne .desc The .code merge -function merges two sorted lists -.meta list1 +function merges two sorted sequences +.meta seq1 and -.meta list2 +.meta seq2 into a single -sorted list. The semantics and defaulting behavior of the +sorted sequence. The semantics and defaulting behavior of the .meta lessfun and .meta keyfun -arguments are the same as those of the sort function. The input lists -are assumed to be sorted according to these functions. +arguments are the same as those of the sort function. + +The sequence which is returned is of the same kind as +.metn seq1 . -This function is destructive. The application should not retain references to -the input lists, since the output list is formed out of the structure of the -input lists. +This function is destructive of any inputs that are lists. If the output +is a list, it is formed out of the structure of the input lists. .coNP Function @ multi-sort .synb -- cgit v1.2.3