any? and find Shiro Kawai (27 May 2021 03:23 UTC)
Re: any? and find Wolfgang Corcoran-Mathe (27 May 2021 18:35 UTC)

Re: any? and find Wolfgang Corcoran-Mathe 27 May 2021 18:35 UTC

Shiro,

Thanks very much for your feedback, and for the lists of
`find'-procedure prior art.

On 2021-05-26 17:23 -1000, Shiro Kawai wrote:
> Finding an item satisfying a given predicate is an operation that can be
> found in most collection types.   Traditionally, we have some flavors:
>
> 1. Returns an item that satisfies the predicate  (srfi-1 find)
> 2. Returns the first true value the predicate returns (srfi-1 any, srfi-125
> hash-table-find)
> 3. Returns true if any item satisfies the predicate (imapping-any?,
> srfi-113 set-any?)
>
> And we may now also be able to have it combined with Maybe.
> 1M. Predicate is item -> generalized boolean.  Returns Just item if
> predicate returns true on any item, or Nothing.
> 2M. Predicate is item -> Maybe.  Returns Just if any of the result is Just,
> or Nothing.

I agree that SRFI 224 should provide ways to handle all of these
cases.  At the moment, I'm thinking that covering 1 and 2M is
sufficient; case 2 is obtained from 1 through simple composition, and
cases 3 and 2M are just 1 and 1M, respectively, without witnesses.

I'm thinking of adding procedures

    (imapping-find pred imap failure) -> [exact-integer, *] or ...

    (imapping-query pred imap) -> maybe[exact-integer, *]

where `imapping-find' is analogous to SRFI 146's `mapping-find' and
where (imapping-query pred imap) returns Just k v if (k, v) is the
least association of <imap> satisfying <pred>, and Nothing otherwise.
Case 2, as mentioned above, can be obtained by simply processing the
values/Maybe returned by the above functions.

(Why do we differentiate case 2, then?  I'd suggest that
hash-table-find from SRFI 125 returns the result of applying its
<proc> argument mainly as a way to deal with multiple values; the
value-or-#f protocol isn't appropriate in that context.)

A further question: why not make `any' witness-generating?  A common
Scheme convention is that procedures which can return a useful truthy
value do so.  Boolean-only `any?' forms (e.g. those from SRFI 113)
make sense to me when any witness value would be in some sense
arbitrary; it's not clear that such a value would be more useful than
#t.  Since the elements of integer mappings have an ordering, however,
it might be useful for imapping-any to return the least-keyed
association, rather than just #t.  (Dual remarks apply to `every'.)

Would the forms proposed above be acceptable, and provide sufficient
coverage?

Apologies if these thoughts seem disjointed.  Best regards,

--
Wolfgang Corcoran-Mathe  <xxxxxx@sigwinch.xyz>

"The dojo is the place where courage is fostered and superior
human nature is bred through the ecstacy of sweating in hard work.
It is the sacred place where the human spirit is polished."
--Shoshin Nagamine, The Essence Of Okinawan Karate-Do