• remove-elements-not-satisfying-the-predicate

    From B. Pym@21:1/5 to Barry Margolin on Wed Jul 3 00:42:32 2024
    Barry Margolin wrote:

    In article ... (Christopher Dollin) writes:
    So suppose someone says "please implement 'remove-if-not'. I would like >your solution to generate no garbage if possible and to be reasonably >efficient".

    Here's a version I threw together quickly (note that it doesn't take
    any options as the CL version does, and it only accepts a list, not
    any sequence):

    (defun simple-remove-if-not (test list)
    (let ((result nil))
    (dolist (item list)
    (unless (funcall test item)
    (push item result)))
    (nreverse result)))

    What an egregiously bad name: "remove-if-not"
    Scheme uses the sensible "filter".

    * (remove-if-not 'oddp '(0 2 3 5))

    (3 5)

    * (simple-remove-if-not 'oddp '(0 2 3 5))

    (0 2)

    See? The high and mighty CL (COBOL-like) guru got it wrong.
    Of course, he probably didn't test it. He has said that
    he doesn't have CL (COBOL-like) installed on his computer.
    It's not worth installing.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Wed Jul 3 01:02:16 2024
    On 2024-07-03, B. Pym <No_spamming@noWhere_7073.org> wrote:
    Barry Margolin wrote:

    In article ... (Christopher Dollin) writes:
    So suppose someone says "please implement 'remove-if-not'. I would like
    your solution to generate no garbage if possible and to be reasonably
    efficient".

    Here's a version I threw together quickly (note that it doesn't take
    any options as the CL version does, and it only accepts a list, not
    any sequence):

    (defun simple-remove-if-not (test list)
    (let ((result nil))
    (dolist (item list)
    (unless (funcall test item)
    (push item result)))
    (nreverse result)))

    What an egregiously bad name: "remove-if-not"
    Scheme uses the sensible "filter".

    Although it is a verbose name, which contains a double negative
    (removing is negative, as is "not") Scheme's name is completely idiotic.

    Filtering is an activity in which we separate a mixture into
    material that is wanted and that is unwanted.

    The name filter doesn't tell me what is returned: the wanted
    material or the unwanted material?

    Sometimes what you want is captured in a filter (e.g. cleaned
    vegetables in a strainer). Sometimes the unwanted material is captured
    in a filter; what passes through is what you want: e.g. cleaner air.

    So even if we specify that the result of filtering something is that
    which is trapped in the filter, that doesn't inform us whether that
    material is what we want to keep.

    If "filter" has to be in the name, you want names like filter-for and filter-out. Filter for this condition (what we want to keep). Filter
    out on this condition (what we want to reject). GNU make has
    $(filter-out ...). Unfortunately, it pairs that with $(filter ...).

    A much better name is keep-if, which is what I used in TXR Lisp.
    It complements remove-if. A user contributed a function called
    separate which returns the keep-if and remove-if results together.

    separate is a synonym for filter; it would be justifiable for separate
    to be called filter.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Paul Rubin on Wed Jul 3 02:54:39 2024
    On 2024-07-03, Paul Rubin <no.email@nospam.invalid> wrote:
    Kaz Kylheku <643-408-1753@kylheku.com> writes:
    The name filter doesn't tell me what is returned: the wanted
    material or the unwanted material?

    The term has been around forever, like in electronics: high pass
    filters, low pass filters, etc. It's almost always the wanted material
    that comes out of the filter.

    Electronics filters (at least simple, passive ones like one-pole RC)
    have only one output. There is no tap for the "filtered out" signal.

    The "high pass" term alone tells you what passes onto that one and only
    output.

    "pass" would be an okay name for the function:

    (pass (lambda (x) (> x 3)) list)

    (reject (lambda (x) (> x 3)) list)

    It's a "high pass filter" and not just a "high filter" because
    the latter makes it unclear whether highs are rejected or passed!

    It could be interpreted similarly to "UV filter" or "dust filter".


    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Kaz Kylheku on Tue Jul 2 19:20:45 2024
    Kaz Kylheku <643-408-1753@kylheku.com> writes:
    The name filter doesn't tell me what is returned: the wanted
    material or the unwanted material?

    The term has been around forever, like in electronics: high pass
    filters, low pass filters, etc. It's almost always the wanted material
    that comes out of the filter.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Kaz Kylheku on Wed Jul 3 13:27:46 2024
    Kaz Kylheku <643-408-1753@kylheku.com> writes:
    Electronics filters (at least simple, passive ones like one-pole RC)
    have only one output. There is no tap for the "filtered out" signal.

    A filter that lets you collect the rejected material is called a sieve.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)