Skip to content

Commit

Permalink
v0.4.23
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jan 27, 2023
1 parent ce8e15e commit 4c35500
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ $ echo '{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}' | jet -t '(jet/paths)'
### `when-pred`
Given a predicate and arguments, run predicate on arguments and return first
argument when truthy. When exception happens, `when-pred` return `nil`, so you
can safely use functions like `odd?` on anything without checking for numbers.
Given a predicate, return predicate that returns the given argument when
predicate was truthy. In case of an exception during the predicte call, catches
and returns `nil`.
The following returns all paths for which the leafs are odd numbers:
``` clojure
$ echo '{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}' | jet -t '(jet/paths) (filter #(jet/when-pred odd? (:val %))) (mapv :path)'
$ echo '{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}' | jet -t '(jet/paths) (filter (comp (jet/when-pred odd?) :val)) (mapv :path)'
[[:a :b 0] [:a :b 2] [:a :c :d]]
```
Expand Down
2 changes: 1 addition & 1 deletion resources/JET_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.22
0.4.23
9 changes: 5 additions & 4 deletions src/jet/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@
paths)]
paths))

(defn when-pred [pred x & args]
(try (when (apply pred x args)
x)
(catch Exception _ nil)))
(defn when-pred [pred]
(fn [x]
(try (when (pred x)
x)
(catch Exception _ nil))))

(def ctx
(-> (sci/init {:namespaces {'camel-snake-kebab.core
Expand Down
2 changes: 1 addition & 1 deletion test/jet/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@

(deftest paths-test
(is (= [[:a :b 0] [:a :b 2] [:a :c :d]]
(edn/read-string (jet "{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}" "-t" "(jet/paths) (filter #(jet/when-pred odd? (:val %))) (mapv :path)")))))
(edn/read-string (jet "{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}" "-t" "(jet/paths) (filter (comp (jet/when-pred odd?) :val)) (mapv :path)")))))

0 comments on commit 4c35500

Please sign in to comment.