Is the first result expected?
all(isdigit, "") # true
given that:
any(isdigit, "") # false
Thanks.
Is the first result expected?
all(isdigit, "") # true
given that:
any(isdigit, "") # false
Thanks.
Yes
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)
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)
ex falso quodlibet
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
*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
writing:
atleast(thresh, pred, collection)
can be short-circuiting (and possibly parallelizing) and just as clear.
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.
An empty set that prints is prettier
julia> findfirst("", "x")
1:0