Clojure相当于Python的"any"和"all"函数?

Tor*_*ler 28 python clojure

Clojure中是否内置了与Python anyall函数类似的函数?

例如,在Python中,它就是all([True, 1, 'non-empty string']) == True.

小智 42

(every? f data) [ docs ]是一样的all(f(x) for x in data).

(some f data) [ docs ]就像any(f(x) for x in data)它返回的值f(x)(必须是真实的),而不仅仅是true.

如果你想要与Python完全相同的行为,你可以使用identity函数,它只返回它的参数(相当于(fn [x] x)).

user=> (every? identity [1, true, "non-empty string"])
true
user=> (some identity [1, true "non-empty string"])
1
user=> (some true? [1, true "non-empty string"])
true
Run Code Online (Sandbox Code Playgroud)

  • 我认为使用身份功能而不是新的匿名功能会更好.例如(每个?identity [1,true,"非空字符串"]),(某些标识[1,true,"非空字符串"])... (3认同)