`all(isdigit, empty_string)`

Is the first result expected?

``````all(isdigit, "")     # true
``````

given that:

``````any(isdigit, "")     # false
``````

Thanks.

Yes

3 Likes

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.

3 Likes

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)
sqrt.(A)
end

# 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)
``````
2 Likes

ex falso quodlibet

1 Like

Likewise,

``````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, "")`
false

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

julia> myany(isdigit, "")
false
``````
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?

2 Likes

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
``````

writing:

``````atleast(thresh, pred, collection)
``````

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

2 Likes
``````julia> contains("x","")
true

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.

2 Likes

An empty set that prints is prettier

``````julia> findfirst("", "x")
1:0
``````
1 Like