This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-15
Channels
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
Anyways, here is my code, which works with two arguments, but no more. And I'm really struggling to understand why:
(defn my-comp [f & g] (fn [& args] (f (if (= (count g) 1) (apply (first g) args) (apply my-comp g)))))
I keep getting "Unhandled java.long.ClassCastException clojure_noob.core$my_comp$fn__5806 cannot be cast to java.lang.Number" when I try to call ((my-comp inc inc *) 2 3)
@UN67GMG81 you're trying to apply pass the result of (apply my-comp g)
as a first arg to f which is inc
in your case; inc
expects a number but the result (apply my-comp g)
is a function => you need to wrap it in extra parenthesis and pass args
as well - In your particular case something like this:
(defn my-comp
[f & g]
(fn [& args]
(f (if (= (count g) 1)
(apply (first g) args)
(apply (apply my-comp g) args)))))
((my-comp inc inc *) 2 3)
;; => 8
@jumar Thank you very much.
@jumar out of curiosity then, am I missing the order in which things happen? For some reason I thought the (apply my-comp g) would resolve to its fullest first, passing (inc ) to my-comp, and then you'd end up with (inc (inc (apply args))). Is it resolving f(apply my-comp g) before it fully recurs my-comp?
@UN67GMG81 The problem is that the result of my-comp
is a function which means that (apply my-comp g)
is going to return a function -- and then you're trying to call f
on that. Since f
is inc
from that first call, it doesn't really matter what (apply my-comp g)
evaluates to beyond it being (fn [& args] ,,,)
-- you can't increment that function.
Also, (my-comp inc)
will fail whereas (comp inc)
works, so that's another case to think about.
Similarly, (my-comp)
fails but (comp)
returns the identity function.
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))
@mfikes testing... 🙂
it works!
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.
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.
@jumar out of curiosity then, am I missing the order in which things happen? For some reason I thought the (apply my-comp g) would resolve to its fullest first, passing (inc ) to my-comp, and then you'd end up with (inc (inc (apply args))). Is it resolving f(apply my-comp g) before it fully recurs my-comp?