This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-12
Channels
- # beginners (85)
- # boot (7)
- # cider (7)
- # clara (1)
- # cljsrn (49)
- # clojure (85)
- # clojure-dev (19)
- # clojure-dusseldorf (15)
- # clojure-greece (1)
- # clojure-italy (12)
- # clojure-nl (4)
- # clojure-russia (21)
- # clojure-sanfrancisco (1)
- # clojure-spec (25)
- # clojure-uk (59)
- # clojurescript (29)
- # core-async (5)
- # core-matrix (1)
- # css (1)
- # datomic (1)
- # docker (10)
- # fulcro (12)
- # graphql (21)
- # hoplon (7)
- # jobs-rus (6)
- # jobs_rus (1)
- # lein-figwheel (1)
- # mount (5)
- # off-topic (6)
- # om (4)
- # onyx (8)
- # perun (1)
- # portkey (25)
- # protorepl (7)
- # re-frame (78)
- # reagent (24)
- # ring-swagger (32)
- # rum (3)
- # schema (1)
- # shadow-cljs (122)
- # spacemacs (3)
- # specter (2)
- # sql (1)
- # testing (1)
- # uncomplicate (2)
- # yada (14)
hey, I got a list that looks like this '(true true false true)
and another one like this: (:a :b :c :d)
I need to keep only the elements from the second if the element from the first as the same index is true
is there a function in clojure.core
that could help me? Or do I have to invent the wheel here?
check keep-indexed
- https://clojuredocs.org/clojure.core/keep-indexed
(keep-indexed #(if (nth '(true true false true) %1) %2) '(:a :b :c :d))
something like this
another suggestion:
user=> (def a '(true true false true))
#'user/a
user=> (def b '(:a :b :c :d))
#'user/b
user=> (remove nil? (map #(when %1 %2) a b))
(:a :b :d)
I ended up doing this:
cljs.user=> (filter (fn [[keep _]] (true? keep)) (map vector '(true false true) '(:a :b :c)))
([true :a] [true :c])
@U3L6TFEJF fyi (remove nil? (map ...))
can be replaced by (keep ...)
oh, never mind, keep doesn’t take more args like map does, that’s too bad
I am new to clojure and functional programming. I was doing some hackerrank example in clojure to build up my knowledge I was wondering could I get a some feedback. The test is to add up the amount of positive, negatives and zeros in a vector
(def positives 0) (def negatives 0) (def zeroes 0) (doseq [item arr] (if (= item 0) (do (def zeroes (inc zeroes))) (do (if (neg? item) (do (def negatives (inc negatives))) (do (def positives (inc positives))) ) ) ) ) (println (format "%.5f" (float (fractional n positives))) ) (println (float (fractional n negatives))) (println (float (fractional n zeroes)))
(def zeroes (count (filter #{0} arr))) (def positives (count (filter (fn [x] (pos? x)) arr))) (def negatives (count (filter (fn [x] (neg? x)) arr))) (println (format "%.5f" (float (fractional n positives)))) (println (float (fractional n negatives))) (println (float (fractional n zeroes)))
Interest to understand where should I tihnk about to be able to filter a list list this but iterate once
When I want to do a thing over a seq but I’m not sure what the best fns to use are, I go to my old friend reduce
^ same thing came to mind
(reduce
(fn [[zeroes positives negatives] val]
(prn zeroes positives negatives val)
[(if (zero? val) (inc zeroes) zeroes)
(if (pos? val) (inc positives) positives)
(if (neg? val) (inc negatives) negatives)])
(repeat 3 0)
[0 0 -1 2 2 -3 0])
@diarmuidmoloneyAnother thing you might try is to separate the two things that are going on: simplifying the values from the domain of all numbers to the domain of #{:pos :neg: zero}
, then counting the frequencies of those values
This might be something that gets you in the right direction:
(defn countNumTypes
[acc num]
(let [[pos neg zero] acc]
(cond
(> num 0) [(inc pos) neg zero]
(< num 0) [pos (inc neg) zero]
(= num 0) [pos neg (inc zero)])))
(reduce countNumTypes [0 0 0] [0 1 -1])
returns: [1 1 1]
@diarmuidmoloney 1. Extract the function which produces 0, 1, -1, call it sign
or signum
. 2. Call (frequencies (map signum your-number))
Hi 👋 Hoping someone can help me out with this. If I had a collection like this:
(def example [{:name "Foo" :someCount 18} {:name "Bar" :someCount 24}])
What would be the "best" way of using (reduce + [insert logic here])
to get a sum of the values in :someCount
?hey, there's a function called comp
that takes multiple functions and returns a function that calls each of them in reverse order (reduce (comp + :someCount) example)
What about doing something along the lines of (reduce (fn [result coll] (+ result some-selector-here)) 0 example)
?
I think I'm missing an understanding of how to get the :someCount
selector within coll
yogidevbear, when reducing over a collection, the reducing function gets called with the accumulator as the first param and each element of the collection as the second element
so our signature is (fn [count-so-far this-map] (+ count-so-far (:someCount this-map)))
in the alternative, you can turn your collection into numbers (map :someCount collection)
and then (reduce + this-new-collection)
or (apply + this-new-collection)
Thanks @dpsutton 👍 It was the (:key-name coll)
that I wasn't quite grokking
Doing (reduce (fn [result coll] (+ result (:someCount coll))) 0 example)
should do the trick
Yup, works like a treat. So my next question is, is there a "better" way? If so, what and why is it considered better?
an alternate that might perform better is (transduce (map :someCount) + example)
how about (transduce (map :someCount) + 0 [{:name "Foo", :someCount 18} {:name "Bar", :someCount 24}])
@sundarj the 0 is redundant, as (+)
returns 0
also (+ n) returns n
user=> (transduce (map :someCount) + [{:someCount 1} {:someCount 2}])
3
the transduce version is also much clearer and cleaner (to my eye)
Wow! That is pretty sweet
I'm not onto transducers yet, but it's good to see that in action
once again... I look at a new rich hickey talk, look back at my code... and ask what the hell I was doing
Another example related to filtering based on a value existing within a vector in a hashmap.
Given (def example [ { :name "Foo" :someVector ["String A" "String B" "String C"] } { :name "Bar" :someVector [] } ] )
How would I retrieve the hashmaps where :someVector
constains a string (e.g. "String B"
to return Foo hashmap)?
I'm leaning towards a solution like (filter #(find "String B" (:someVector %)) example)
, but that gives me a ClassCastException
find expects a hash-map
you can use #(contains? #{"String B"} (:someVector %))
or as a shorthand, (comp #{"String B"} :someVector)
oh wait, you want to know if “String B” is anywhere in there, so that won’t work
(contains? (set (:someVector %)) "String B")
or, use .indexOf
Hello, all, I'm working on my first cljs project, and I've added [cheshire "5.8.0"]
to project.clj :dependencies
, but the figwheel output doesn't seem to include it (after restarting)
I did get output from leiningen apparently downloading the new dependencies. Figwheel reads the config as valid, but then says "Failed to compile ... No such namespace: cheshire.core"
why would cljs be able to use cheshire?
I guess you could use it in a macro but I can’t imagine that would ever be useful
@noisesmith yeeeah, that would be it.
to use json from cljs I think the preferred thing is using interop since javascript can do JSON natively
Did that at the start. I was googling for JSON-to-native data structures when I found cheshire
oh- you want the opposite direction - the better option is to use goog.object I think
either just using goog.object on the thing, or using the functions there to construct the clojure data you need
Okay. I'd definitely rather not rely on these functions throughout, so I might do the second
Maybe I'm just missing the simple trick though? -- https://cljs.github.io/api/cljs.core/js-GTclj
I bet there are helpful libraries but I’ve heard people who know more about cljs than I do talk about the limitations / drawbacks of using eg. js->clj
@noisesmith, (filter #(contains? (set (:someVector %)) "String B") example)
worked perfectly, thank you. I tried playing around with filter
and .indexOf
, but couldn't quite get it to work. #(contains? (set ...
is quite nicely readable though 🙂
transit does seem pretty reasonable
cljs.user=> (t/read (t/reader :json) "{ \"foo\": 1, \"bar\": \"bash\", \"baz\": [{\"string\": \"key\"}]}")
{"foo" 1, "bar" "bash", "baz" [{"string" "key"}]}
cljs.user=>
js->clj also seems to work but its documentation indicates you're better off using transit
cljs.user=> (js->clj (JSON/parse "{ \"foo\": 1, \"bar\": \"bash\", \"baz\": [{\"string\": \"key\"}]}"))
⬆
WARNING: No such namespace: JSON, could not locate JSON.cljs, JSON.cljc, or JavaScript source providing "JSON" at line 1
⬆
WARNING: Use of undeclared Var JSON/parse at line 1
{"foo" 1, "bar" "bash", "baz" [{"string" "key"}]}
@bfabry I think that nees to be js/JSON
I'd love to figure out why my core.cljs is locked every time I have a compile error and I have to restart figwheel to be able to save the file again.
I'm on Windows, and this seemed promising https://github.com/bhauman/lein-figwheel/issues/604, except it looks like it was only on the Linux emulation layer for that user