Nil as "not found" marker and nil as value. Is it possible to somehow distinguish between the two cases?
char16t opened this issue · 6 comments
char16t commented
Hello.
I have two examples that return nil
. In the first case, the data structure I'm looking for contains nil
. In the second, I try to get in the same data structure value on a nonexistent path.
First:
(sp/select [:a :ab] {:a {:aa 1 :ab nil}}) ;; => [nil]
Second:
(sp/select [:a :ac] {:a {:aa 1 :ab nil}}) ;; => [nil]
Question: Is it possible to somehow distinguish between the two cases? How do I verify that the requested path exists in the data structure?
nathanmarz commented
You're looking for "must".
(sp/select (must :a :ac) {:a {:aa 1 :ab nil}}) ;; => []
On Sat, Sep 8, 2018 at 10:10 PM Valeriy ***@***.***> wrote:
Hello.
I have two examples that return nil. In the first case, the data
structure I'm looking for contains nil. In the second, I try to get in the
same data structure value on a nonexistent path.
First:
(sp/select [:a :ab] {:a {:aa 1 :ab nil}}) ;; => [nil]
Second:
(sp/select [:a :ac] {:a {:aa 1 :ab nil}}) ;; => [nil]
Is it possible to somehow distinguish between the two cases? How do I
verify that the requested path exists in the data structure?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#264>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIBWIk7eT1JqtTj52IbrkyJdD-xtFcdks5uZBYpgaJpZM4Wf_vb>
.
--
Twitter: @nathanmarz
http://nathanmarz.com
char16t commented
I tried this:
(let [p [:a :ab]
c {:a {:aa 1 :ab nil}}]
(sp/select
(sp/if-path (apply sp/must p) p sp/STOP)
c)) ;; => [nil]
(let [p [:a :ac]
c {:a {:aa 1 :ab nil}}]
(sp/select
(sp/if-path (apply sp/must p) p sp/STOP)
c)) ;; => []
But this solution returns []
when path is [sp/ALL]
for example.
With the suggested solution is the same:
(defn f [p c]
(sp/select
(apply sp/must p)
c))
;; OK
;; not selectable
(empty?
(f [:a :ac]
{:a {:aa 1 :ab nil}})) ;; => true
;; OK
;; selectable
(empty?
(f [:a :ab]
{:a {:aa 1 :ab nil}})) ;; => false
;; selectable
;; Failed
;; expected false because f should return [:ab]
(empty?
(f [:a sp/ALL sp/ALL #(= :ab %)]
{:a {:aa 1 :ab nil}})) ;; => true
nathanmarz commented
You'll have to be more specific of what you're changing to ALL that's
giving unexpected result.
On Sat, Sep 8, 2018 at 10:29 PM Valeriy ***@***.***> wrote:
I tried this:
(let [p [:a :ab]
c {:a {:aa 1 :ab nil}}]
(sp/select
(sp/if-path (apply sp/must p) p sp/STOP)
c)) ;; => [nil]
(let [p [:a :ac]
c {:a {:aa 1 :ab nil}}]
(sp/select
(sp/if-path (apply sp/must p) p sp/STOP)
c)) ;; => []
But this solution returns [] when path is [sp/ALL] for example
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#264 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIBWDYwj_VrTYZ7IEq3bjZhVQv3MSsKks5uZBqzgaJpZM4Wf_vb>
.
--
Twitter: @nathanmarz
http://nathanmarz.com
char16t commented
I mean that
(sp/select
[:a sp/ALL sp/ALL #(= :ab %)]
{:a {:aa 1 :ab nil}}) ;; => [:ab]
and
(sp/select
(sp/must :a sp/ALL sp/ALL #(= :ab %))
{:a {:aa 1 :ab nil}}) ;; => []
Not any specter path will be correctly verified by such a solution.
nathanmarz commented
Look at the documentation for must, it is the same as keypath except with
an existence check. It's not a wrapper for arbitrary paths.
If you're doing an ALL on a map, by definition you only navigate to
existing key/value pairs.
On Sun, Sep 9, 2018 at 5:25 PM Valeriy ***@***.***> wrote:
I mean that
(sp/select
[:a sp/ALL sp/ALL #(= :ab %)]
{:a {:aa 1 :ab nil}}) ;; => [:ab]
and
(sp/select
(sp/must :a sp/ALL sp/ALL #(= :ab %))
{:a {:aa 1 :ab nil}}) ;; => []
Not any specter path will be correctly verified by such a solution.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#264 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIBWLrjrqHy4kh0LVUhPz7jOpUOVJnvks5uZSTMgaJpZM4Wf_vb>
.
--
Twitter: @nathanmarz
http://nathanmarz.com
char16t commented
Thanks