JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

keep

core-api


    function
    boot.janet on line 1284, column 1

    (keep pred x & xs)

    Given a predicate `pred`, return a new array containing the truthy 
    results of applying `pred` to each value in `x`.

    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 an empty array.

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


See also:filter2 examplesSign in to add an example
Loading...
# absolute value of negatives, ignore positives
(keep |(if (< $ 0) (- $) nil) [-1 2 -3 -4 -5 6]) # -> @[1 3 4 5]

# halves each even number, ignores odds
(keep |(when (even? $) (/ $ 2)) [1 2 8 7 -2])    # -> @[1 4 -1]
TechcablePlayground
(def person-ids {"Alice" 42 "Bob" 23})

(keep person-ids ["Carl" "Bob" "Alice"])   # -> @[23 42]

(filter person-ids ["Carl" "Bob" "Alice"]) # -> @["Bob" "Alice"]
(map person-ids ["Carl" "Bob" "Alice"])    # -> @[nil 23 42]
felixrPlayground