JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

count

core-api


    function
    boot.janet on line 1264, column 1

    (count pred x & xs)

    Count the number of values in `x` for which applying `pred` yields 
    a truthy value.

    Also functions variadically, i.e. takes additional arguments, with 
    suitable changes to the arity of `pred`. `pred` is called with both 
    values from `x` and additional arguments, and only as many times as 
    the length of the shortest of `x` and each element of `xs`. If `x` 
    or any element of `xs` is empty, returns 0.

    `x` and elements of `xs` can be values of type bytes, indexed, 
    dictionary, fiber, or abstract with suitable `get` and `next` 
    methods.


1 exampleSign in to add an example
Loading...
(count even? [1 2 3 4 5])  # => 2

(count  (fn [x] (> x 3))  [1 2 3 4 5])  # => 2
(count         |(> $ 3)   [1 2 3 4 5])  # => 2

(count  (fn [x] (truthy? x))  [nil false true 42 :a "foo"])  # => 4
(count         |(truthy? $)   [nil false true 42 :a "foo"])  # => 4

(var f even?)
(count f [1 2 3 4 5])  # => 2
(set f odd?)
(count f [1 2 3 4 5])  # => 3

(map  (fn [f] (count f [1 2 3 4 5]))  [even? odd?])  # => @[2 3]
(map         |(count $ [1 2 3 4 5])   [even? odd?])  # => @[2 3]
cellularmitosisPlayground