JanetDocsPlaygroundI'm feeling luckyGitHub sign in

keep



    function
    boot.janet on line 1004, column 1

    (keep pred ind)

    Given a predicate `pred`, return a new array containing the truthy 
    results of applying `pred` to each element in the indexed 
    collection `ind`. This is different from `filter` which returns an 
    array of the original elements where the predicate is truthy.


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