Fork me on GitHub
Christian Barraza06:09:24

Hey everyone! Working through Brave and True so I can help with a particular project- came across a suggestion to try and re-implement the 'comp' function

Christian Barraza06:09:03

Anyways, here is my code, which works with two arguments, but no more. And I'm really struggling to understand why:

Christian Barraza06:09:51

(defn my-comp [f & g] (fn [& args] (f (if (= (count g) 1) (apply (first g) args) (apply my-comp g)))))


I'm trying to filter a Vector of Maps [{v} {q} {r}] by nested values. I'm having trouble smashing every-pred and get-in


[{:english "Go to the park at 3pm."
	:bunsetsu { :jar1 "3pm"  :lid1 "に" :sense1 "ni-time"
	:jar "the park" :lid2 "に" :sense2 "ni-destination"
        :verb "go" :verbsense "travel-verb"} ...more entries]


I'd like to grab all the maps from this vector where [:bunsetsu :lid1] and [:bunsetsu :lid2] are members of a known-list. i'm having trouble figuring out how to filter on nested elements.


Maybe I can store it more intelligently. I have 2 vital pieces of info in each map. Can I index map entries with 2 values o.O?


Using get-in, I need to provide a numerical index, like 1 or 2, but a lot of the examples i see using filter against a vector of maps doesn't have this


I do a for loop and I end up with lots of extra nils... easy way to get rid?


(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil 5 nil nil nil nil nil nil 7 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil)


For loop probably not the best solution but i'm not having much success trying to do filter with get-in


@sova Maybe something along these lines?

(let [v    [{:a 1 :b {:c "x"}}
            {:a 2 :b {:c "y"}} 
            {:a 3 :b {:c "z"}}]
      pred (fn [m] 
             (#{"x" "z"} (get-in m [:b :c])))]
  (filter pred v))


thank you! i was missing the map from the predicate. can you help me understand why the predicate needs a map?


can the predicate be multiple lines like that linked with (and) ?


(let [v    [{:a 1 :b {:c "x" :d "e"}}
            {:a 2 :b {:c "y" :d "f" }} 
            {:a 3 :b {:c "z" :d "g"}}]
      pred (fn [m] 
             (and (#{"x" "z"} (get-in m [:b :c]))
           (#{"e" "f" "g"} (get-in m [:b :d]))))]
  (filter pred v))


@sova filter looks at each item in the collection, applying the supplied pred to to each in order to determine whether to include it in the output. In this case, the items in the vector are maps. The implementation of pred can be anything: For this problem, it will be passed a single map, and it can look inside that map to decide whether it should be included in the output, and it can also apply arbitrary Boolean logic (using and, or, not etc.)


wonderful. thanks so much @mfikes you saved me time, energy. Amazing. I can filter this thing down so cleanly now. Much appreciated. I had seen this set pattern #{} a little bit on Stack Overflow and it turns out to be super duper useful!


it would be fun to write a solver that takes one data structure shape and attempts to come up with the most efficient transformations to get to the other, especially if it could use transducers.


even if it threw up it's hands a lot of the time.


I haven't even used it, but have heard descriptions of Specter that it has some of that inside, or at least some kind of internal run-time optimizations.


For complicated stuff, specter can be a lot faster than the core libs due to the internal optimizations it does. For the easy stuff, it almost never is. It’ll never make a faster get or assoc for example.