Fork me on GitHub

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))))


@jennifer 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))))


i’m going to go look at the update-in docs, too.


that if isn't a valid arg to update-in


update-in needs a function (optionally followed by more args to the function)


Now that you're using update-in, it expects a function and arguments instead of a value.


yeah. i’m seeing that. 🙂


replacing the if with not will actually do a toggle


(update-in todolist [id :complete] not)


yup! that did the trick! thanks!!!


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)


Can anyone help me explain why it’s not the same ?


@ice5050 it really bizzare, so let's walk through it a bit simply:


'(reverse) expands to (quote reverse)


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"


Oh, I see.


I think I can use ’() shorthand for list of functions.


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.


Of course the vector doesn't have a key named "reverse" so it returns nil.


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.


@tbaldridge Thank you very much, you helped me a lot!


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?


@foamdino u can create a partial function and use that in filter


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 require [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 f with :a :b :c :d in that order. It’s useful in cases like yours: (filter (flip str/starts-with? “a”) [”abc” “def”])

Alex Miller (Clojure team)19:03:33

Rich considers anon functions to be the Clojure approach to this


Yeah, I know, but I find (flip f x y z) to be nicer to read than #(f % x y z) 🙂


(or (fn [a] (f a x y z)))


Sometimes I think it would be nice if we had filter-> etc: (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-> and 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.

Alex Miller (Clojure team)19:03:57

sounds like a good thing to have outside of core :)


Heh, yeah, worldsingles.util has a bunch of stuff that we use heavily that is “like” core stuff but absolutely should not be “in” core!

Alex Miller (Clojure team)19:03:47

the cool thing about functions is we can always make more :)


Always Be Composing 🙂