Fork me on GitHub

whats the purpose in wrapping the server and db in a handler? In the PurelyFunctional vids it has this:

(defn wrap-server [hdlr]
  (fn [req]
    (assoc-in (hdlr req) [:headers "Server"] "Listronica Server")))

(defn wrap-db [hdlr]
  (fn [req]
    (hdlr (assoc req :webdev/db db))))

(def app
Is wrapping the db in a handler used in other languages as well? I don't recall having to do anything like this in Node.js.... and I don't think I've ever seen the server handler like this... curious as to the purpose?

dpsutton02:08:32 here's an issue about this very pattern in express


but the idea is to just embed the database connection into the request so the handler doesn't need any specific way to create its own connection it just has one available to it


So just curious... does embedding the database connection like this create any security issues?


@ryan.russell011 to be pedantic, the wrap- functions are wrapping the handler and replacing it with a new one that has added context, so the db isn't wrapped in the handler, the handler is wrapped by the db to create a new handler


@noisesmith oh right... yeah.. thank you for that clarification... I had it backwards in my head there.... @dpsutton So from what I got from the github issue is that this pattern isn't necessary and could cause potential issues. I'm curious why PurelyFunctional decided to present it this way. When I get to my own project, I'll look around more for how this could be handled differently.


I have looked for a function to remove duplicates from a seq with a test which define an equality. That’s like the remove-duplicates with :test on Common Lisp. However, I couldn’t find it in clojure core functions, so I had accordingly written the function like this ( But I would like to know why the function is not implemented (or I missed it), and a way to write in better approach (e.g. how I can construct it on a combination of core functions). Does anyone know this?


@U3L6TFEJF Thank you response. I found it but I think it cannot receive an equality function (it only check =). I would like to remove duplicates, by an equality function as belows:

(remove-duplicate-with-test [1 2 3 4 3 2 1] =)
;=> [1 2 3 4]

(remove-duplicate-with-test [{:foo 1 :bar 2} {:foo 1 :bar 3} {:foo 2 :bar 1} {:foo 2 :bar 4}]
                            #(= (:foo %1) (:foo %2)))
;=> [{:foo 1, :bar 2} {:foo 2, :bar 1}]
In the first case, it is same behavior as distinct, but the second case only check :foo’s values regardless of :bar’s values for to decide the equality. Can the distinct deal with that case? If so, I’m sorry and I will read the page once.


ahh, sorry, I did not read the question carefully enough! there is no such function in core, but it has been implemented a number of times, here is one example:


Thank you very much. The library is so helpful for me.


glad to help 🙂


the last expression returns 2 whereas I expect 1! is this a bug or am I missing something?


I am not a manifold expert but my first hunch is that map isn't fully lazy - eg. if it has a special case for empty input it needs to consume at least one item (common problem with otherwise lazy code)


does the return value surprise you?


it doesn't surprise me, because it's common for a nominally lazy process to steal a single value


but I'm not 100% sure that's what happened, I just think that's likely


you'd need to consume from z to really know


if consuming y gives 2, then consuming z gives "1", that's what happened


consuming z gives "1"


how do you think of the relationship between y and z?


what's the concept?


y is a mutable source of data, z consumes from it, so reading y depends on the state ofz


in reliable async code, I wouldn't expect to see anything sharing an input with z (that is, I wouldn't expect anything else to read from y), unless you are OK with z not seeing those items, and also OK with z stealing items the other consumers of y would otherwise see

✔️ 4

this is from the manifold library (aleph)


Following , the tests fail with AssertionError Assert failed: (= "Hello, World!" (greeting)) user/eval6094 (NO_SOURCE_FILE:1). What am I doing wrong?


I have defined the function:

(defn greeting
 ([] (greeting "World"))
 ([x] (println "Hello," (str x "!")))
 ([x y] (println (str x ", " y "!"))))


And (greeting) returns "Hello, World!" as expected.


So far I interpret the error message to represent the lack of a file where the function is defined, but so far in the tutorial it doesn't assume more than a REPL environment, which is what I'm using.


It's not returning anything. println returns nil and prints as a side effect


Hmm, makes sense Michael. The tutorial hasn't mentioned returning values yet though, which is why it got me confused.


Also there's an extra comma in there


You're adding a comma and an exclamation mark in the one-arity, which calls the two arity and it adds a comma and exclamation mark as well


Are you working in a repl?


@anantpaatra println returns nill


The two arity is independent in this case, no?


Yes, I'm working in a repl.


@anantpaatra The problem is you have written a function that prints the result as a side-effect. That’s different from returning the result. Solution: just get rid of the println


Yes, that is the solution @manutter51


apart from removing the println, multiple-arity is not very readable some times


It was just a bit confusing, where can I contribute to this tutorial?


Thanks everyone


There’s a guide to how to contribute to clojure


IIRC that includes changes to the docs


@joelsanchez I guess the tutorial is just trying to make a point by using multiple arity in this case, but that looks nice.


@manutter51 it's a bit more involved than I anticipated but I guess I would have to fill that someday anyway.


@anantpaatra @manutter51 actually, small changes to the docs only require a GitHub issue/PR here:

👍 4

Ah, cool, didn’t know that.


I have a performance question. I've got a map with about 150k entries. Each entry is a key with a string vector as a value (each vector is about ~5-10 strings long) I want to group the map by the length of the string vectors, but (group-by #(count (second %)) mymap)) seems to take forever.


are you sure they are vectors and not lazy-seqs and that doing the count isn't doing the work of forcing all the lazy seqs?


oh yep it's a lazy sequence. How do I avoid this problem?


it isn't really something to avoid, you can either do the work up front, or delay it until you do the group-by, but either way it will get done


question to group: What is your preferred way/library to interact with a Postgres database? (and why?)


so i've been able to wrap my head around basic recursion thanks to you folks. now i'm trying to figure out how recur and reduce fit in this picture. I understand now how this example works:

(defn sum
     (sum vals 0))
  ([vals accumulating-total]
     (if (empty? vals)
       (sum (rest vals) (+ (first vals) accumulating-total)))))
And now, I can just sub out that last sum for recur and it works just the same:
(defn sum
     (sum vals 0))
  ([vals accumulating-total]
     (if (empty? vals)
       (recur (rest vals) (+ (first vals) accumulating-total)))))
But I read the doc on recur and I don't really understand what it is saying. I'm having difficulty formulating my question, but what is recur actually doing and why is it used for performance reasons as this tutorial is telling me?


and then what does reduce add to this equation? I'm thinking these are just various approaches to recursion right but not sure when you would use each.


the processing described by that sum function has a shape that is shared by lots of other processes, that shape is called a fold, and folds are tree shaped processes (a list being a degenerate form of a tree). reduce is a kind of fold


as for recur, that is kind of a whole other thing, but calling it a performance optimization is kind of weird


is that shape/tree meaning where you are going to take the first item in a collection and do something, and then call the function with the rest of the collection and keep doing it until the collection is empy.


the tutorial (brave book) says to generally use recur for performance because clojure doesn't provide tail call optimization


it isn't really a performance thing, it is a correctness thing. without recur your sum will blow the stack on a large enough input


I dunno if you saw the sicp's discussion of recursive vs. iterative processes, but recur makes a recursive process iterative


in a language with tco, your sum's recursive call would be made iterative by tco


i saved the link but i haven't watched them yet. So are you saying recur is just a basic tool we would use for simple recursion functions that let us reason about it in a more iterative manner? i'm not too familiar with iterative loops either unfortunately.


not sure if you remember but you basically gave me a step by step breakdown of simple recursion. with recur what would i do differently when writing out the steps. is it the same as if the function itself was being used in it's place?


@hiredman fixed my problem now, was unrelated to the laziness. The function to generate the string vectors was the bottleneck


a recursive process builds a tree, it may actually build a tree datastructure and return it, but it also builds a tree of runtime state. at the very least a sort of linear list of stack frames to be popped as the process completes


so it accumulates runtime state, that runtime state has to be stored somewhere (on the jvm it is stored in a callstack)


an iterative process does the same thing over and over again without building up some intermediate state (like a loop that overwrites the same set of variables)


when you have a recursive process that is in the shape of a list (not a general tree, but there are tricks for that too) it is kind of trivial to convert it in to an iterative process, and some languages offer the ability to do that automatically as tco


because of technically limitations of the jvm, and wanting to interop with the jvm well, clojure can't do tco in the general case


recur is like a manual tco annotation, with the extra benefit that the compiler will complain if recur is used in a situation where it cannot create an iterative process. in languages that do automatic tco you can sometimes mean to write an iterative process and accidentally write a recursive one


good stuff!


i don't quite get all of it but it's interesting to learn some of the fundamental things going on in the background

hiredman19:08:23 has example substitution model executions of a recursive fib function and an iterative fib function


i'll read through it. and i'm already starting to understand more of what you wrote above as i read over it a couple times. i think this "reward of deep thought" that i get when I really try and grasp this stuff is what draws me to programming so much. it's cool stuff. it's also why when i feel i'm too tired to progress i just allow myself to walk away for a bit cuz it can't be forced with tired, surface thinking.


Question about specs. Lets say I have a function like this:

(defn insert! 
   [{:keys [db id]}]
   ;; implementation here]
If I were to spec this, I would need to do something like this:
(s/fdef insert! 
  :args (s/cat :args (s/keys :req-un [::my-ns/db ::my-ns/id])))
However, what if I want the names in the argument declerations to be different from the specs? For example, I want to keep the argument declarations the same as defined in insert! but I want maybe the spec names are different
(s/fdef insert! 
  :args (s/cat :args (s/keys :req-un [::my-ns/global-db ::my-ns/global-id])))
Notice how db is now global-db vs. db. Would this be possible to do this inline the :req-un? or would I have to write a spec like (s/def ::db ::my-ns/db) to get this to work?


I'm not sure how this could be done but I do want to point out that this is working against the underlying philosophy of spec. There's a strong emphasis in the spec "system of thinking" on having the same namespaced keyword for the same conceptual unit of information all throughout the program.


Indeed. I agree with this tenant. I suppose an issue with this is if a code base defined the specs, but poor names/conventions were used. Instead of changing things everywhere, I was hoping to change things in place 😞 Obviously, this is not a problem with spec.


On the bright side, if you're using literal namespaced keywords throughout, the find-and-replace operation should be fairly easy to do?