Skip to content

Commit

Permalink
Merge pull request #259 from sogaiu/swap-keep-examples
Browse files Browse the repository at this point in the history
Swap an example for keep
  • Loading branch information
sogaiu authored Jan 17, 2025
2 parents b4809da + 400f2e4 commit 9e44e8d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/keep.janet
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# returns an array of truthy results of predicate
(keep identity [false :x nil true]) # -> @[:x true]

# keep values > 1, equivalent to filter
(keep (fn [x] (when (> x 1) x)) @[0 1 2 3]) # -> @[2 3]

# for all members > 2, keep the square
(keep (fn [x] (when (> x 2) (* x x))) [0 1 3 4 5]) # -> @[9 16 25]

# operates on values in table
(keep (fn [x] (when (> x 1) (* x x))) @{:foo 1 :bar 2 :baz 3}) # -> @[4 9]

0 comments on commit 9e44e8d

Please sign in to comment.