Fork me on GitHub
#specter
<
2016-06-28
>
thomasdeutsch07:06:18

is there a way in specter to delete nil keys from a map? {nil 1, :b 2} -> {:b 2}

Chris O’Donnell11:06:25

(transform ALL (fn [[k v]] (if (nil? k) nil [k v])) data) I think. Can't check as I'm on my phone.

Chris O’Donnell13:06:26

But why not just do (dissoc m nil)?

vikeri13:06:51

Hi, I want to select from a vector using srange. It works well until the index is out of bounds. Can I somehow prevent getting an error and instead return only the elements found? Or is this bad coding practice of some reason? As an example, if I have a vector with three elements and does (srange 0 4) it will throw an error, but instead I would like to get the only three elements found.

nathanmarz14:06:10

@vikeri: you can use srange-dynamic for that

nathanmarz14:06:56

@vikeri

(defnavconstructor bounded-srange
  [p srange-dynamic]
  [s e]
  (p (fn [aseq]
      (if (< s 0) 0 s))
     (fn [aseq]
      (let [c (count aseq)]
        (if (>= e c) c e)
        ))))

vikeri14:06:40

@nathanmarz: Great! Looks a little like black magic to me since I started looking at specter today. But it worked so I’m happy 🙂

vikeri14:06:15

Had to change it to this to fully accomodate my needs:

(defnavconstructor bounded-srange
                   [p srange-dynamic]
                   [s e]
                   (p (fn [aseq]
                        (let [c (count aseq)]
                          (cond
                            (< s 0) 0
                            (>= s c) c
                            :else s)))
                      (fn [aseq]
                        (let [c (count aseq)]
                          (if (>= e c) c e)
                          ))))

nathanmarz15:06:18

@vikeri: defnavconstructor works like a normal function but also integrates with Specter's inline caching

vikeri16:06:16

Alright, I'm sure it makes more sense if I read the docs 😛 . But it seems to be a nifty library, quite useful with a powerful query language for nested data structures.