This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-11
Channels
- # announcements (1)
- # aws (2)
- # beginners (43)
- # calva (7)
- # cider (5)
- # cljs-dev (1)
- # cljsrn (2)
- # clojure (68)
- # clojure-android (10)
- # clojure-conj (2)
- # clojure-italy (10)
- # clojure-nl (3)
- # clojure-spec (3)
- # clojure-uk (128)
- # clojurescript (10)
- # core-async (47)
- # cursive (32)
- # datomic (35)
- # events (1)
- # fulcro (24)
- # funcool (2)
- # graphql (2)
- # jobs (3)
- # juxt (2)
- # leiningen (3)
- # luminus (1)
- # off-topic (7)
- # om (1)
- # onyx (2)
- # pedestal (32)
- # perun (2)
- # portkey (2)
- # re-frame (4)
- # ring-swagger (2)
- # rum (3)
- # shadow-cljs (137)
- # spacemacs (4)
- # testing (3)
- # tools-deps (101)
- # uncomplicate (2)
- # unrepl (5)
- # vim (2)
Hi all, I'm constructing a vector using a map like so
(mapv #(let [ {:keys [a b c]} %]
[b c "foo" a]
) data)
I need to add a counter, so that the resulting vector looks like this:
[
[1 b c "foo" a]
[2 b c "foo" a]
[3 b c "foo" a]
...
]
Is there an elegant way to achieve this without using loop/recur/var?user=> (mapv (fn [m i] [(:a m) (inc i)]) [{:a "a"} {:a "b"}] (range))
[["a" 1] ["b" 2]]
@rakhim you can also use map-indexed
too
(map-indexed (fn [i m] [(:a m) (inc i)]) [{:a "a"} {:a "b"}])
produces the same effect
Wait... but why?..
(range)
returns a lazy seq, and then... (inc i)
, I don't quite understand how this works
you are passing 2 args to map
: [{:a "a"} {:a "b"}]
and (range)
. so map runs till one of them finishes. in this case its the first param
@rahul080327 thanks, this is even better, although requires an into
or something to convert it to a vector since there's no mapv-indexed
yeah thats a catch
How can it be that this tests comparing two numbers fails?
expected: 3 to be an instance of java.lang.Long
was: 3 is an instance of java.lang.Integer
[praha bRno]
(testing "case insensitive"
(let [xs (contacts/get-all {:city ["praha" "bRno"] :lang_id "cs"})]
(is (= 2 (count xs)))
Is there a way how to make clojure test ignore types or do I really need cast the int to long?@carr0t it's a list of maps
boot.user=> (require '[clojure.test :refer :all])
nil
boot.user=> (is (= 2 (count '({:a "a"} {:b "b"}))))
true
boot.user=> (is (= 2 (count [{:a "a"} {:b "b"}])))
true
boot.user=> (is (= (Long. 2) (count '({:a "a"} {:b "b"}))))
true
boot.user=> (is (= (Integer. 2) (count '({:a "a"} {:b "b"}))))
true
No idea what's happening with your one... Are you using clojure.spec
or is the newer version of clojure.test
? Might be that the specfile somewhere is saying that the compared value must be a Long
@cpfiro are you asking how to parse json?
@noisesmith yes but using two keys to pull out two values
usually with jvm clojure I would parse first (using the cheshire library, or clojure.data.json), and then pull the keys out of the resulting map
unless you are talking about cljs, where I would use the built in JSON class for interop
right, so you can use select-keys
once you have a map
but that's not json any more, it's just clojure data
user=> (select-keys {:a 0 :b 1 :c "more"} [:a :b])
{:a 0, :b 1}
often your http lib will convert the json for you