Node: Modifying Sequences, Next: , Previous: Simple Sequence Functions, Up: Sequences



Modifying Sequences

remove x sequence Function

remove x sequence test Function

remove returns a copy of sequence sequence with any element eql to x removed. If the optional third argument test is given it must be an Lpp Function of two arguments and it used in place of eql for the test. For example:

     let list1 = list(L(1), L(2), L(3));
     let list2 = remove(L(2), list1);
     list1 => (1 2 3)
     list2 => (1 3)
     list1 = list(L("1"), L("2"), L("3"));
     list2 = remove(L("2"), list1);
     list1 => ("1" "2" "3")
     list2 => ("1" "2" "3")
     list2 = remove(L("2"), list1, L(stringEqual));
     list1 => ("1" "2" "3")
     list2 => ("1" "3")
     

nremove x sequence Function

nremove x sequence test Function

nremove x sequence test gc Function

nremove returns the sequence sequence with any element eql to x removed. Unlike remove, nremove is a destructive operation on sequence. If the optional third argument test is given it must be an Lpp Function of two arguments and it used in place of eql for the test. If test is given but nil then again eql is assumed.

The fourth argument gc tells nremove how the removed elements should be garbage collected. If sequence is a string then gc is ignored. If gc is nil then they are not collected. If gc is t then they are fully collected. If gc is an Lpp Function of one argument then it is called on the removed element.

If sequence is a list then gc is applied to the removed cons and a t value is equivalent to passing in the gcConsElement function. If you wanted to collect just the cons element but not its contents you would pass in the gc function as the gc argument. Here are some examples for lists

     let list1 = list(L(1), L(2), L(3));
     let list2 = nremove(L(2), list1);
     list1 => (1 3)
     list2 => (1 3)
     let listKeep = list(L(1), L(2), L(3));
     let list1 = list(L(1), listKeep, L(3));
     list1 => (1 (1 2 3) 3)
     let list2 = nremove(listKeep, list1, 0);
     list1 => (1 3)
     list2 => (1 3)
     listKeep => (1 2 3)
     list1 = list(L("1"), L("2"), L("3"));
     list2 = nremove(L("2"), list1, Nil, True);  // Strings are not eql!
     list1 => ("1" "2" "3")
     list2 => ("1" "2" "3")
     list2 = nremove(L("2"), list1, L(stringEqual), True);
     list1 => ("1" "3")
     list2 => ("1" "3")
     

Here are some examples of remove and nremove for strings

     let string1 = L("Hello World");
     remove(codeChar('o'), string1) => "Hell Wrld"
     string1 => "Hello World"
     nremove(codeChar('o'), string1) = "Hell Wrld"
     string1 => "Hell Wrld"