This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-20
Channels
- # announcements (1)
- # beginners (48)
- # cljdoc (6)
- # cljs-dev (1)
- # cljsjs (1)
- # clojure (13)
- # clojure-dev (6)
- # clojure-estonia (1)
- # clojure-europe (1)
- # clojure-finland (8)
- # clojure-gamedev (3)
- # clojure-spec (35)
- # clojure-uk (25)
- # clojurescript (9)
- # datascript (1)
- # datomic (18)
- # figwheel-main (2)
- # fulcro (5)
- # graphql (1)
- # jobs (16)
- # off-topic (76)
- # pathom (39)
- # re-frame (6)
- # reagent (7)
- # remote-jobs (6)
- # rum (3)
- # shadow-cljs (54)
- # spacemacs (8)
- # speculative (2)
- # yada (72)
not sure how to ask.Can i get stuff out of dotimes? simplistic example…
(defn something
[count]
(dotimes [n count]
; do something with a side effect
(+ total-ran 1)
("Total: " total-ran)
so that
(something 5)
-> "Total: 5"
@simon223 If you look at the dotimes
macro (https://github.com/clojure/clojure/blob/clojure-1.9.0/src/clj/clojure/core.clj#L3305), you can see the body of the loop
is a when
that always ends in a recur call, so it’ll always return nil when the iterations are done. dotimes
is primarily for side effects.
If you want to just return the number of iterations, you can do that from the count
var, but that’s not very useful, assuming not every iteration has a side effect.
What you might want is something like this:
(defn something
[count]
(loop [n count
total-ran 0]
(if (< n 0)
(str "Total: " total-ran) ; return final result
(if (even? n) ; some test to determine whether to do something
(do
(println "Did something with" n)
(recur (dec n) (inc total-ran)))
(recur (dec n) total-ran)))))
Happy to help. Feel free to reach out if you have any other questions
oh this is really clear to read https://www.freecodecamp.org/forum/t/clojure-loop-recur/18418
A binding gives a name to a value. Each time you recur
you give the same names to new values.
A top-level name, bound with def
(or defn
) is more like a pointer/reference since a Var
is created -- the name is bound to the Var
and the Var
"contains" the value. And you can alter what the Var
contains (`alter-var-root`, for example, or just calling def
again).
thanks @U04V70XH6 and @kingmob i got what i needed with something like:
(defn do-stuff [total]
(loop [results []]
(if (= (count results) total)
(results})
(do
(let [new-thing (do-something)]
(recur (conj results new-thing))))...
is there a type of prettier for clojurescript? where on save the file reformats automatically like this: https://github.com/prettier/prettier
How can I destructure extra_info keys from the map?
{
:categories ["A"]
:attributes {
:tags ["tag1"]
:unit_price "1"
:extra_info {
:neutrient_elements ["elem1"]
}
}
}
I tried something like
[{:keys [categories] :as product
{:keys [tags unit_price] :as attributes} :attributes
{:keys [nutrient_elements] :as extra_info} [:attributes :extra_info]}]
and it seems to work for attributes but does not destructure the extra_info 😞[{:keys [categories] :as product
{:keys [unit-price tags] :as attributes
{:keys [nutrient-elements] :as extra-info} :extra-info} :attributes}]
but at this level of nesting it's probably more readable to use get-in
than use destructuring
if i have [{:type "cat" :age 5}, {:type "dog" :b 6}]
how can i get a list like ("cat", "dog")
?
a little uncertain about my square/round brackets there, but my squigglies are definite 😅
Bit of feedback on the above, just for context:
You can express it either as a [vector]
or a '(list)
(note the quote so that it doesn’t try to interpret (list)
as a function). Either works equally well in this case; it seems to be more common to use vectors for literals. They have different performance characteristics, but those aren’t going to matter unless you’re dealing with a large number of elements.
One way to think about it is that they’re both sequences (ie fulfill the sequence interface), and a ton of functions in Clojure operate on sequences, without caring what the concrete type is.
@U077BEWNQ the “sequence interface” makes sense, I generally keep forgetting what’s what after i google it 😛
Thank you @qythium
@simon223 If you're just starting out you'll probably have a better time focusing on the classics like map, filter and friends before moving on to transducers
Definitely starting out… sooo. i feel bad i couldn’t work this out, because it feels simple… but i’ve got beginners amnesty? Trying to turn:
{:First "foo", :Second "bar"}
into
{:First
[:eq "foo"]
:Second
[:eq "bar"]}
My latest non-working example.
output ((fn [[k v]] [:eq v]) (into [] input))]
im just not quite getting how to process/transform items in a map 😕I PM'd you if you still need help btw 🙂
there are also utility libraries which implement map-vals
for mapping over values in a map, which clojure doesn’t have built in
ah i got similar.. yeah i did mostly understand why my examples weren’t working from the errors.
you can also use reduce
and be lazy: (reduce #(assoc %1 (first %2) [:eq (last %2)]) {} {:First "foo", :Second "bar"})
(you can do code formatting with backticks (`
), how to escape backticks is beyond me for now)
yes, to the right is a menu with three dots
choose edit
OR, when it is the last message you wrote, you can simply press arrow up