This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-20
Channels
- # 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))))
@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))))
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.
replacing the if with not
will actually do a toggle
(update-in todolist [id :complete] not)
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)
@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"
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?
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”])
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.
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!
the cool thing about functions is we can always make more :)
Always Be Composing 🙂