Concept:FindAndAnyInCollection

Finding something that satisfies in a collection is a common operation, and many collection/container types provides such interface.

The find family

  • Returns an element that satisfies the predicate. If the collection is a mapping, returns key and value as two values.
  • Takes a predicate and a collection.
  • Some take a failure thunk as the third argument. If the operation doesn't take a failure thunk, #f is returned when no satisfying entry is found. Note: If it doesn't take failure thunk, it can't handle the case when #f as an element satisfies the predicate.
  • A predicate receives an element, if the collection is a simple collection of values; or it receives a key and an associated value, if the collection is a mapping.
    • Predicate receives key and value: hashmap-find?, mapping-find?.
  • Anomalies:
    • hash-table-find?(SRFI-125) - Returns what predicate returns, rather than the actual key & value. With this regard, it belongs to any family.
    • hashtable-find?(SRFI-126) - Arguments are reversed (a collection, a predicate). Returns three values ([key, value, found?])
    • imaping-find?(SRFI-224 draft) - predicate takes only a value.
  • If the collection is ordered, there can be a variant to find the last entry that satisfies the predicate.

The any family

  • Unresolved question: If mapping collection wants the multiple-collection interface, how should the arguments passed to the predicate?

The any? family

  • Anomalies:
    • imapping-any?? - predicate takes only a value.
    • In bit-set operations, the predicate is obvious so it only takes a collection, plus start/end arguments: bit-field-any??(SRFI-151), bitvector-field-any??(SRFI-178)

Maybe+find

  • Wrap the return value with Maybe, eliminating the failure thunk and allowing multiple values packed into a single one.
  • Predicate gets a key and a value: imapping-query/key?(SRFI-224 draft)
  • Predicate gets a value: imapping-query?(SRFI-224 draft)

Maybe+any

  • The predicate returns Maybe[]. If it's Just, it is returned immediately.
  • So far, we don't have an instance for this .