`all(isdigit, empty_string)`

Is the first result expected?

all(isdigit, "")     # true

given that:

any(isdigit, "")     # false




Functionally, you can think of all and any as doing this (although more efficiently):

all(f, itr) = mapreduce(f, &, itr; init=true)
any(f, itr) = mapreduce(f, |, itr; init=false)

when itr is an empty collection, all defaults to true and any defaults to false. As @jar1 points out above, this is also consistent with how mathematical \forall (all) and \exists (any) work.


So it is a “vacuous statement” that doesn’t really say anything meaningful. But I am glad that mathematicians can make sense of such truths.

It’s the natural answer not just in the abstract mathematical sense.
Some examples to make clear that all(empty collection) must be true:

# check if the string can be typed into a textfield that only supports digits
# empty string can be typed just fine
all(isdigit, s)

# check if all numbers are positive, so that sqrt.(a) doesn't throw
# if array is empty - sqrt won't have any issues
if all(>=(0), A)

# should I go outside and gather children from the playground and forest, or not?
# if no children at all - that's fine
all(is_at_home, children)

ex falso quodlibet

1 Like


any(is_outside, children)

any could also have been called atleast1 to make things clearer (and to open the door for other atleast, atmost functions). Also note the following synthetic any definition:

julia> !all(!isdigit, "")  # like `any(isdigit, "")`

julia> myany = !∘((p,c)->all(!p,c))
#97 (generic function with 1 method)

julia> myany(isdigit, "")
1 Like

*DeMorgan nods approvingly*

Note that the composition operator in your definition of myany is redundant, as ! composes already.

I rather like the name of any and wouldn’t want that to change. But the idea of atleast and atmost is intriguing. perhaps?:

any(f, itr) == atleast(1, f, itr)

Is there any mainstream programming language that provides such predicates in their standard library? i.e. is there evidence that this is a common need?


The use of these phrases in common language is suggestive, but I’ll defer to @Dan, as I haven’t found myself in need; it’s just a short-circuiting optimization over what can be done with count.

The atmost-k and atleast-k are common in SAT formulations. In that domain there is a known and important problem of how to encode such a constraint with SAT clauses which are essentially atleast-1 clauses.

As an aside, my gut tells me, formulation of code into SAT/SMT verifiers is a good thing (especially in a future where boundaries between hardware and software become fuzzier).

And there is also the short-circuit issue, instead of

count(pred, collection) >= thresh   # common condition


atleast(thresh, pred, collection)

can be short-circuiting (and possibly parallelizing) and just as clear.

julia> contains("x","")

julia> findfirst(==(""),"x")

A sort of Gödel’s incompleteness theorems: there will always be statements about any string that are true, but that are unfindable within the system. :smile:


An empty set that prints is prettier :wink:

julia> findfirst("", "x")
1 Like