This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # arachne (4)
- # bangalore-clj (1)
- # beginners (38)
- # boot (182)
- # cider (21)
- # cljs-dev (9)
- # clojars (5)
- # clojure (229)
- # clojure-austin (1)
- # clojure-berlin (1)
- # clojure-czech (3)
- # clojure-dusseldorf (3)
- # clojure-ireland (5)
- # clojure-italy (4)
- # clojure-russia (33)
- # clojure-spec (73)
- # clojure-taiwan (6)
- # clojure-uk (22)
- # clojure-ukraine (1)
- # clojurescript (80)
- # core-async (26)
- # cursive (3)
- # datascript (20)
- # datomic (9)
- # defnpodcast (8)
- # editors (4)
- # emacs (7)
- # garden (41)
- # hoplon (2)
- # java (1)
- # lambdaisland (2)
- # lein-figwheel (1)
- # leiningen (5)
- # luminus (4)
- # lumo (36)
- # off-topic (4)
- # om (21)
- # onyx (1)
- # pedestal (33)
- # re-frame (33)
- # ring-swagger (70)
- # spacemacs (26)
- # specter (7)
- # sql (6)
- # timbre (2)
- # untangled (12)
- # vim (3)
- # yada (1)
hopefully quick question. i’m trying to make the value of the
:complete key true if it’s false and false if it’s true (triggered by a checkbox toggling on the front end). if i have this set to just
true without the conditional, it works. but with the conditional, it only sets to true (always defaults to false). i’ve also tried it with
(if (true? :complete) false true) but that didn’t work either.
(swap! todolist-atom (fn [todolist] (assoc-in todolist [id :complete] (if (true?) false true))))
true? expects a single argument but you don't supply one. Also, you probably want
update-in instead of
assoc-in to implement a toggle more easily.
here’s an updated version, but i get a 500 server error. it doesn’t like the boolean. i think i’m checking the wrong thing in the conditional.
(swap! todolist-atom (fn [todolist] (update-in todolist [id :complete] (if (true? :complete) false true))))
Now that you're using
update-in, it expects a function and arguments instead of a value.
Hello! I’m a beginner here. I’m really confused this code.
(reduce #(%2 %1) [1 2] '(reverse)) ;; nil (reduce #(%2 %1) [1 2] (list reverse)) ;; (2 1)
if you do
(type (first '(reverse))) you'll see that what you have isn't a list of the function "reverse" but a list of a symbol who's name is "reverse"
In Clojure, symbols are functions, and can look themselves up in associative collections. Vectors are associative collections (keys being integers). So your first example takes "reverse" and tries to look it up in the vector.
In the second case, list is a function that takes arguments. So the compiler looks up reverse, and calls
list passing in a reference to the function
reverse. Now you have the actual function and not a symbol inside the
reduce so it works as expected.
hi clojure.string/starts-with? looks perfect for a (filter) I have in mind, but I cannot apply starts-with? as a predicate to filter as it takes two params (s and substr) so this (filter (clojure.string/starts-with? "a") ["abc" "def"]) fails - is there something simple I'm missing or should I be looking at macros here?
You can’t use
partial here since the missing argument is the first, not the last. You can use an anonymous function:
(filter #(str/starts-with? % “a”) [”abc” “def”]) — assuming you
[clojure.string :as str] which is pretty standard in Clojure code.
^^ exactly what I was looking for - I didn't think I needed a macro to re-order the arguments!
Macros are usually best thought of as a last resort: do everything with functions and only use macros where a) you can’t get a function to do what you need or b) you want some syntactic sugar to remove boilerplate.
At work we have a
flip utility function (named for the Haskell version) that is like
partial but allows for the argument to be omitted at the front, so
((flip f :b :c :d) :a) will call
:a :b :c :d in that order. It’s useful in cases like yours:
(filter (flip str/starts-with? “a”) [”abc” “def”])
Sometimes I think it would be nice if we had
(filter-> (str/starts-with? “a”) […]) but then I love the threading macros and probably overuse them for your taste @alexmiller (or Rich’s taste) 🙂 We have
condp->> macros at work that thread the expression thru the predicate (as well as the expression) since that seemed to be a very common “need” in our code.
worldsingles.util has a bunch of stuff that we use heavily that is “like” core stuff but absolutely should not be “in” core!