JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

keep

core-api


    function
    boot.janet on line 1141, column 1

    (keep pred ind & inds)

    Given a predicate `pred`, return a new array containing the truthy 
    results of applying `pred` to each value in the data structure 
    `ind`, but only if no `inds` are provided. Multiple data structures 
    can be handled if each `inds` is a data structure and `pred` is a 
    function of arity one more than the number of `inds`. The resulting 
    array has a length that is no longer than the shortest of `ind` and 
    each of `inds`.


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