Fork me on GitHub

i thought it was the way middleware are layered like an onion (or rings in 2d)


Hey Folks, I need your help to understand how commute work on ref?


It's the same as the other api alter? but it's faster, there are less/no ordering guarentees

Alex Miller (Clojure team)13:09:01

"faster" sounds like latency, but the benefit is really greater concurrency (due to fewer constraints/conflicts), which will typically give you better transaction throughput


what's the best way to check equality on a specific list value (I'm filtering the result of partition-by)? I feel like #(= '(1) %) is not the way


(I'm actually filtering for not equals)

Alex Miller (Clojure team)15:09:43

sequential collections are equal regardless of coll type. so (= '(1) [1]) in case that makes anything simpler


good to know, I'll keep doing what I'm doing. Thanks Alex


hi all! some? is an interesting name for a function. Can someone explain why that's the name for that particular function instead of not-nil? or non-null?


using (def not-nil? (comp not nil?)) might read more like plain English ... but using a built-in that already exists seems like it should be the way to go ...

Alex Miller (Clojure team)17:09:41

It’s like math notation, “there exists some x, such that ...”

🤯 4

I understand that commute is faster than a`alter` but what I would like to know is how commute get to be fast? What does it do and how to make it faster? I read that it doesn't retry transaction and use intransaction-value. To be honest I don't know what that mean

Alex Miller (Clojure team)17:09:15

ok, again, it's not faster, it's greater concurrency

Alex Miller (Clojure team)17:09:30

transactions track all of the refs used in the transaction

Alex Miller (Clojure team)17:09:54

if two transactions happen at the same time and write the same refs, one fails and must be retried

Alex Miller (Clojure team)17:09:18

commute means you don't fail the transaction in this case, you go ahead and complete both (in some undefined order)

Alex Miller (Clojure team)17:09:32

the canonical example is that commute updates a stat counter

Alex Miller (Clojure team)17:09:57

you don't care in which order two transactions update it - they can both inc the counter and your stats are still correct, and no transactions fail


can this be written as a (def predicate instead of a (defn?

(defn is-sleeping? [state players]
   (and (= "night" (:time-of-day @state))
        (remove is-werewolf players)))


Yes, but it will terrible, also you should wrap seq around that call to remove


Seq functions may return the empty seq instead of nil


@hiredman: I was wondering how to do that, thanks!



(defn is-sleeping? [state players]
   (and (= "night" (:time-of-day @state))
        (into () (remove is-werewolf players))))
tried (into and (seq and still returns false when there's nil instead of ()


Not sure why you want to use into there?

user=> (if (and true (into () (remove even? [2 4 6]))) "truthy" "falsey")


user=> (remove even? [2 3 4])
user=> (remove even? [2 4])
user=> (seq (remove even? [2 3 4]))
user=> (seq (remove even? [2 4]))


With or without the into, that is-sleeping? will return a value that is treated as logical true in an if or cond condition.


it cannot return a value that is treated as logical false.


oh -- I think it's that and that is making it true/false instead of ()


user=> (if () "truthy" "falsey")


@hiredman: what is the terrible way to write the predicate as a (def?


In an if or cond conditional expression, if it evaluates to false or nil, it is logical false. Any other value returned by that expression, including (), is treated as logical true.


A combination of partial, comp, and juxt


But it is bad, don't do it


It is less readable, and breaks useful features like being able to redefine functions


Point free style is not good


and does not change values of its subexpressions. It uses the same rules that if and cond do to separate out the falsey from the truthy, and returns them unchanged, including ().


I guess we might be misinterpreting how you want to use your function is-sleeping?, because it is a (not enforced) convention that functions ending with ? return true or false. Perhaps your intent is different for that function?

Ewan Valentine19:09:24

Does anyone have any examples of routing PUT/PATCH requests to Compujure, using hiccup.forms?

(PATCH "/update/:id" [id completed] (update-task id completed)))
I have a handler defined like that, and my form has a hidden _method field automatically with PATCH. But it's showing up as POST which 404's. Much appreciated!


The _method thing is a work around some frameworks support

👍 4

It is not part of html/http or universally implemented

David Pham20:09:39

It is not really about a beginner Clojure question per se, but I had a weird experience today as a beginner clojure dev and I don’t really know where I could share, but desired to get some feedback. To put in context, I decided to take the risk of building our front-end tools in our team (60 persons) in my company using CLJS in January (I am mathematician/statistician working in a bank), and the feedback has been overwhelmingly positive (i.e. most don’t understand how I create the tools, but they want me to continue the good work). However, during lunch, I share my experience with another team which is technically much more experienced (typical Java, Scala, C++ skill set for AI) and I have been belittled because I was using Lisp at work. Usually, I don’t really care, but this time I really wondered if I were doing something wrong. I strongly believe Clojure and ClojureScript are amazing instruments to make effective stuff and I have tons of fun, but I got my first doubt today. I wondered if anyone had a similar experience?


user reports outweigh developer language arguments 100%


makers always win... enjoy the hate


By the time it’s compiled and run, Clojure is just JVM bytecodes same as any other JVM application, so prejudice against Clojure just because it’s a Lisp is definitely misplaced. Clojure is amazingly good at writing industrial strength enterprise applications.


hey all, if I had a vector of vectors like, [[1 2] [3 4] [5 6] [7 8] ... ] how can I go about combining them such that every two vectors are merged together resulting in [[1 2 3 4] [5 6 7 8] ... ] and so on?


Looks like (map flatten (partition 2 [[1 2] [3 4] [5 6] [7 8]])) gets it done. I think I was just overthinking it and assuming that clojure had a built in function to do that

Toby Clemson21:09:26

Or (partition 4 (flatten [[1 2] [3 4] [5 6] [7 8]])) I think


Never use flatten

❤️ 4

because flatten is indiscriminate (and flattens anything) and at some point you will have a list of lists of lists where you only want two levels collapsed


flatten is lazy, not in the evaluation sense, but on the part of the programmer, oh, I am not sure what the structure of this thing is, but I'll just bash it with this hammer and maybe I'll get what I want


you can think of tree traversal (nested lists are trees) as a sort of balanced equation, as you descend levels you remove nesting from one side and add context information to the other (context information may just be stack depth, etc). flatten is "meh, I don't need that information" which are famous last words


(I do use concat, just something to consider)


yeah, well, don't use concat like that


(mapv #(apply into %) (partition 2 [[1 2] [3 4] [5 6] [7 8] [9 10] [11 12]]))
[[1 2 3 4] [5 6 7 8] [9 10 11 12]]


or just plain old loop:

(def vecs [[1 2] [3 4] [5 6] [7 8] [9 10] [11 12]])
(loop [[fst snd & rst] vecs, res []]
  (if fst (recur rst (conj res (into fst snd)))
;;=> [[1 2 3 4] [5 6 7 8] [9 10 11 12]]


(into []
      (comp (partition-all 2)
            (map (fn [[a b]] (into a b))))
      [[1 2] [3 4] [5 6] [7 8] [9 10] [11 12]])


nice transducer solution 🙂


I feel like there is something that could be done with cat


user=> (map into (take-nth 2 vecs) (take-nth 2 (rest vecs)))
([1 2 3 4] [5 6 7 8] [9 10 11 12])


(I know, not the best solution complexity-wise)

Ewan Valentine22:09:14

Another noob question, I have a function, I'm passing in a hash map and a single integer value, I need to destructure the hashmap and add in the integer:

(defn update [id task]
  (sql/query spec ["update todos set title = ?, completed = ? where id = ?"
                   (:title :completed task)]))
So I need to fit id in there, but I can't figure it out


task is a Clojure map with keys :title and :completed, and perhaps others?


Because if so, evaluating (:title :completed task) will likely give an error, or at least not what you want.


Or else sql/query is a macro that I have never seen before, so do not know what it does with that expression.

Ewan Valentine22:09:49

Yes it's a map, that's correct


Wrapping the body of the function with (let [task-with-id (assoc task :id id)] (sql/query ...)) might be close.

Ewan Valentine22:09:16

I'll give that a go 🙂


then use task-with-id in the body where you currently have task


I am not sure about the sql/query part, but perhaps where you currently have (:title :completed task) you instead want (:title task-with-id) (:completed task-with-id) (:id task-with-id) ?

Ewan Valentine23:09:07

I'll give that a go, I am getting a weird Postgres error, that could be why


three ? require three parameters. you're only passing one and its the entire task object which won't work


(:keyword :access {:a :b}) #_>> {:a :b}


Yeah, it is a little bit unusual why this is the case, but (:title :completed task) returns task

Ewan Valentine23:09:36

I thought I was destructuring it, but it was a misunderstanding of how destructuring worked basically

Ewan Valentine23:09:24

I come from a Go background, so a lot of this is very, very... new to me haha 😅

😄 4

I believe this would be a correct use of Clojure destructuring on the map named task:

(defn update2 [id task]
  (let [{:keys [title completed]} task]
    (sql/query spec ["update todos set title = ?, completed = ? where id = ?"
                     title completed id])))


No need to create a different version of the map in that case.

Ewan Valentine23:09:07

Ahaaa that makes sense, that's pretty neat

Ewan Valentine23:09:16

Thanks both, much appreciated 🙂


Destructuring also works in function args BTW...

(defn update3 [id {:keys [title completed]}]
  (sql/query spec ["update todos set title = ?, completed = ? where id = ?"
                   title completed id]))

Ewan Valentine23:09:34

Even more succinct! Love it, great suggestion, that's really handy

👍 4

It is good to keep in mind though that in that example, I lost the name task when destructuring it which might have served as documentation. The function is called update and we implicitly understand that it updates a task because of the arg name, which is gone now. Because of this, it is many times better to just destructure in an inner let, or retain the name task like this:

(defn update3 [id {:keys [title completed] :as task}]
  (sql/query spec ["update todos set title = ?, completed = ? where id = ?"
                   title completed id]))

✔️ 12