| all(), any()
|
s1 = "2026"
s2 = "2026-05-09"
print(s1.isdigit()) # True
print(s2.isdigit()) # False; contains '-' sign
a = True
b = True
c = False
print(all([a, b, c])) # False
print(any([a, b, c])) # True
|
import std/strutils # isDigit
import std/sequtils # toSeq(iter), map, filter
import std/sugar # =>
let
s1 = "2026"
s2 = "2026-05-09"
echo s1.all(isDigit) # true
echo s2.all(isDigit) # false; contains '-' sign
let
a = true
b = true
c = false
echo [a, b, c].all(x => x == true) # false
echo @[a, b, c].any(x => x) # true
|
In Python, isdigit() is a string method. It checks if every character is a digit.
In Nim, all() and any() are defined in std/sequtils:
proc all[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool {.effectsOf: pred.}
proc any[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool {.effectsOf: pred.}
all(): Iterates through a container and checks if every item fulfills the predicate.
any(): Iterates through a container and checks if at least one item fulfills the predicate.