meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
Panel 3000 2021-03-18T02:53:15.001600Z

Is there a native way to avoid including a key in a map when the value would match nil ?

noprompt 2021-03-18T16:12:46.003800Z

Are you searching for a submap m such that for all k (string? (get m k))?

noprompt 2021-03-18T16:14:35.004Z

If so you can use

(m/rewrite {,,,}
  (m/submap-of !key (m/pred string? !value))
  (m/map-of !key !value))

Panel 3000 2021-03-18T22:37:31.004400Z

For my use case the killer feature is realy to keep the shape of the data because I need to rename the keys . What I am after is a way to not include the key value in the output if the value is nil.

(m/match
  {:a nil
   :b 2}
  {:a ?a
   :b ?b}
  {"a" ?a
   "b" ?b})
=> {"b" 2}

Panel 3000 2021-03-18T22:41:00.004800Z

The key "b" was omitted from the output because ?b match a nil.

noprompt 2021-03-18T22:43:50.005Z

(fn f [m]
  (m/find m
    {(m/keyword ?name) (m/some ?x) & ?rest}
    (merge {?name ?x} (f ?rest))

    {_ _ & ?rest}
    (f ?rest)

    _
    {}))

noprompt 2021-03-18T22:45:53.005300Z

Using rewrite :

(m/rewrite m
  {(m/keyword ?name) (m/some ?x) & (m/cata ?rest)}
  {?name ?x & ?rest}

  {_ _ & (m/cata ?rest)}
  ?rest

  _ {})

jimmy 2021-03-18T22:46:12.005500Z

I will just say you can also just do a post process step. (On my phone) Something like (into {} (filter second) my-data-after-meanderized))

Panel 3000 2021-03-19T10:57:37.005800Z

Thanks you both, I read thru the chat log of the past few months and the support you are providing is exceptional.

❤️ 2
jimmy 2021-03-18T03:54:05.001700Z

Not exactly sure what you mean. Here’s how you can make sure that a value is non-nil.

(m/match {:a 2}
  {:a (m/some ?a)}

  ?a)

;; => 2

(m/match {:b nil}
  {:a (m/some ?a)
   :b nil}

  ?a)

;; =>  non exhaustive pattern match

jimmy 2021-03-18T03:55:37.002Z

Did you want to do that or did you want to check for a key that is explicitly nil?

jimmy 2021-03-18T03:58:00.002200Z

If so, I think the best I can think of is this. Maybe there is a better way?

(m/match {:b nil}
  (m/and
   (m/pred #(contains? % :b))
   {})

 :yep)

jimmy 2021-03-18T03:58:33.002400Z

(If neither of these are right feel free to give some examples)

Panel 3000 2021-03-18T05:02:07.002800Z

Thanks for the answer (m/match{:a nil :b 2} {:a ?a :b ?b} {"a" ?a "b" ?b}) ;; => {"a" nil, "b" 2}

Panel 3000 2021-03-18T05:03:06.003Z

What I'm looking for is a way to instead return this : ;; => {"b" 2}

Panel 3000 2021-03-18T05:04:07.003200Z

I found this answer in the chat log: (m/search {:foo/a "..." :foo/b "..." :foo/optional nil } {:foo/a (m/pred string? ?value)} [:a ?value] {:foo/b (m/pred string? ?value)} [:b ?value] {:foo/optional (m/pred string? ?value)} [:optional ?value])

Panel 3000 2021-03-18T05:04:46.003400Z

But then we are losing the idea of keeping the shape of the output data.