Page 1 of 1

Example for removeAllBy

Posted: 10 Mar 2021 16:16
by daveplummermd
Folks, could anyone provide an example of how to use "removeAllBy". There is no such example in the programs provided under examples and I don't understand the arguments that need to be included.

Thanks in advance
Dave Plummer MD

Re: Example for removeAllBy

Posted: 10 Mar 2021 20:18
by Gukalov

Code: Select all

implement main     open core   clauses     run() :-         X = "abc",         List = [ "ABC", "aBc", "cba", "abc", "AbC", "abC" ],         L = list::removeAllBy(string::compareIgnoreCase, List, X), %        L = list::removeAllEq({(A, B) :- equal = string::compareIgnoreCase(A,B)}, List, X), %        L = list::filter(List, {(A) :- equal <> string::compareIgnoreCase(A,X)}),         console::write(L),         _ = console::readLine().   end implement main   goal     console::runUtf8(main::run).

Re: Example for removeAllBy

Posted: 10 Mar 2021 22:46
by daveplummermd
Thank you for the example.
There is some stuff here I don't understand but I'll work through it using debug to learn.
Again, thanks for your reply.

Re: Example for removeAllBy

Posted: 12 Mar 2021 12:14
by daveplummermd
Gukalov

is it possible to do this same thing with different domains?
For example if x=1 and List=[1,2,3] in your example? What is the comparator to be used?

Code: Select all

X=1, List=[1,2,3], L = list::removeAllBy(somecomparitor, List, X),
dave

Re: Example for removeAllBy

Posted: 12 Mar 2021 21:17
by Gukalov

Code: Select all

class predicates     somecomparItor : core::comparator{PofigChyo}. % define your own domain here instead of "PofigChyo" clauses     somecomparItor(A, B) = less :- A < B, !. % define your own "less" here instead of "A < B,".     somecomparItor(A, B) = greater :- A > B, !. % define your own "greater" here instead of "A > B,".     somecomparItor(_, _) = equal.

I think that using list::removeAllBy is not the best way to clean the lists.

Re: Example for removeAllBy

Posted: 14 Mar 2021 21:07
by Thomas Linder Puls
The predicate removeAllBy is used when you want to supply your own comparison routine.

For strings it could for example (as above) be a comparison that consider uppercase and lowercase letters the same.

But for numbers it would (almost always) be natural to use the normal number comparison.

Code: Select all

clauses     run() :-         L1 = [1, 2, 3, 4, 5],         L2 = list::removeAllBy(compare, L1, 4),         stdio::writef("L2 = %\n", L2).
However, that is exactly what the predicate removeAll does:

Code: Select all

clauses     run() :-         L1 = [1, 2, 3, 4, 5],         L2 = list::removeAll(L1, 4),         stdio::writef("L2 = %\n", L2).