# \`all(isdigit, empty\_string)\`

**URL:** <https://discourse.julialang.org/t/all-isdigit-empty-string/96038>\
**Category:** General Usage\
**Created:** [March 14, 2023, 8:14am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038 "2023-03-14T08:14:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 14, 2023, 8:14am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/1 "2023-03-14T08:14:40Z")

</div>

Is the first result expected?

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

```

given that:

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

```

Thanks.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 14, 2023, 8:17am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/2 "2023-03-14T08:17:13Z")

</div>

Yes

> **[Vacuous truth](https://en.wikipedia.org/wiki/Vacuous_truth)**
>
> In mathematics and logic, a vacuous truth is a conditional or universal statement (a universal statement that can be converted to a conditional statement) that is true because the antecedent cannot be satisfied. For example, the statement "she does not own a cell phone" will imply that the statement "all of her cell phones are turned off" will be assigned a truth value. Additionally, the statement "all of her cell phones are turned on" would also be vacuously true, as would the conjunction of Mo...

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 14, 2023, 8:24am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/3 "2023-03-14T08:24:49Z")

</div>

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

```julia
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](https://en.m.wikipedia.org/wiki/Universal_quantification)) and \exists ([any](https://en.m.wikipedia.org/wiki/Existential_quantification)) work.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 14, 2023, 8:55am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/4 "2023-03-14T08:55:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 14, 2023, 11:01am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/5 "2023-03-14T11:01:57Z")

</div>

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

```julia
# 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)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 14, 2023, 2:48pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/6 "2023-03-14T14:48:05Z")

</div>

ex falso quodlibet

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 14, 2023, 8:14pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/7 "2023-03-14T20:14:10Z")

</div>

> [@aplavin](#):
>
> ```julia-auto
> # 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)
> 
> ```

Likewise,

```julia
any(is_outside, children)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 14, 2023, 8:42pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/8 "2023-03-14T20:42:23Z")

</div>

`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
julia> !all(!isdigit, "") # like `any(isdigit, "")`
false

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

julia> myany(isdigit, "")
false

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 14, 2023, 9:10pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/9 "2023-03-14T21:10:07Z")

</div>

\*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?:

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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 14, 2023, 9:17pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/10 "2023-03-14T21:17:15Z")

</div>

> [@uniment](#):
>
> But the idea of `atleast` and `atmost` is intriguing. perhaps?:

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?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 14, 2023, 9:30pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/11 "2023-03-14T21:30:56Z")

</div>

> [@stevengj](#):
>
> 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`.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 15, 2023, 12:00am UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/12 "2023-03-15T00:00:13Z")

</div>

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

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

```

writing:

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

```

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

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 16, 2023, 3:24pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/13 "2023-03-16T15:24:24Z")

</div>

```julia
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._ 😄

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 16, 2023, 4:16pm UTC](https://discourse.julialang.org/t/all-isdigit-empty-string/96038/14 "2023-03-16T16:16:00Z")

</div>

An empty set that prints is prettier 😉

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

```
