Fork me on GitHub

There exist a few alternate implementations of sets and maps that introduce a few different ideas of order, e.g. sorted by some comparator on the set elements/maps keys, e.g. integers sorted by <. There are also ones that remember the order that elements/keys were most recently added. Those are not very commonly used, I don't think, and in particular operations like clojure.set/union and others don't have any guarantees on which specific type of set they will return, if you pass it more than one different type of set.


In general, you will be surprised less often if you use sets and maps for things where you really do not care about the order, and lists or vectors for cases where you do.


So what is the main difference between using defn- and :^private annotation?


@pablore They are essentially the same: They both result in :private true meta on the fn var being defined.


(source defn-) is illuminating


good morning. is there anyone very familiar with yada? i want to call one method on every request, but don't know how to inject this. tried several things, but either the function wont be called or swagger-doc is fucked up, cause i played with subrecoures...or the interceptor chain will not accept any prepending of functions 😕


(and the documentation is not 100% correct :-D)


I’ve come up against an error using mikera/core.matrix:

=> (require '[clojure.core.matrix.linear :as linear])
=> (linear/eigen my-matrix)

clojure.lang.ExceptionInfo: TODO: Not yet implemented: (mp/eigen m options) for class mikera.matrixx.Matrix
After poking around a little bit, it seems like the implementation of eigen is up to the array implementation: Does this mean I need to use a different matrix library to create my-matrix (i.e. one of [these]( Thanks



=> my-matrix
#vectorz/matrix [[...] [...] etc]


Hey there so I was working through the map defaults problem on 4clojure : , and with my solution I'm only returning an empty hashmap ... what am I doing wrong 😕


(fn map-default [init-value init-keys] (let [default-map {}] (dotimes [n (count init-keys)] (assoc default-map (get init-keys n) init-value)) default-map))


@alexbakic you're thinking in terms of mutable data structures, and trying to update your default-map in place. assoc doesn't change default-map, it returns a new map with a new key/val. Your dotimes is repeatedly associng into default-map and then throwing away the return value, and then you return default-map which is unchanged from when you initialized it as {}


I see , how would I go about changing it so I return the changes ?


@alexbakic You could use loop/`recur` for that, although reduce would be more idiomatic. Since this is the #beginners channel though I think loop/recur is a bit easier to follow, let me see if I can make a quick example


Hate to interrupt the convo, but I happen to also be having an issue with a 4clojure problem, except my solution seems to pass the tests with the exact same solution/test code in my editor, but the site is claiming it's failing the first test when I hit run.


The assoc function returns a new map with the added value, and then recur passes the updated map back through the loop, so each time through, you get a map with one more key/value pair. When you hit 10, you just return the updated map instead of calling recur, so you fall out of the loop, and the result is your map with 10 values.


Oops it says I can't use the function count and I named a binding count 😅


if i create an anon function in the repl using fn and associate it with a name do I have to do something special to let the repl use that function? I'm getting an unable to resolve symbol error on my fn associated name.


that name isn't a var, it's just a name that is usable in self-calls and shows up in the name / stack-trace


to make a var, use defn (same syntax as you used for fn there)


got it! i was mixing up two examples i saw from this book into one wrong mish mash of stuff. haha. thanks!


so in another example problem i was told to create a function, mapset, that works like map but returns a set. Using the repl (clojure and the repl are so cool!) I was able to create this function:


now that does what the example question says it should do but how do i actually really know its doing what its supposed to? does that makes sense. like how do I know if that really takes anything you can do with map and does the same but returns a set. is this what stuff like Spec helps you confirm?


I think you can trivially prove both qualities of that function


(map f v) - takes all f and v that map would take by definition


(set l) - for any lazy-seq l (what map returns) - always returns a set


this might have been a corner case if I didn't know the behavior, I guess

user=> (set (map inc nil))


there are other ways to write that which eg. might perform better, but it's pretty straightforward to determine that does what we want


as far as I know, there are no specs defined yet for the map or set functions


hmmm. ok. and would you change anything to my function? i guess i should call it coll or collection instead of vector huh. the example used a vector but i guess it works for lists too. is this what they mean when they say naming things is hard in programming?


either way, the fact i was able to create this function in five minutes having just read the opening chapters of the Brave book is just awesome to me. after fiddling with the beginning tutorials of a few other languages i really think i found the right fit for me with clojure.


i might need to table the spec stuff for a future date. maybe i should be asking about testing functions in general


generative (or property-based) testing is something people do in Clojure. this is when you specify some properties of a function that you expect to hold true generally, e.g. (= (+ x 0) x) or (set? (mapset f xs)), and then test.check (the generative testing library) will generate many random inputs and test the function on them for you. if any fail, it will find the smallest possible failing input it can and return that. with clojure.spec, when you write a spec, in many cases you can get one of these random generators for free! so if you define a spec for something that is an int?, you get a generator for ints automagically, and can both sample it, and do generative testing with it


very cool. i will look into this test.check thing.


yeah, the "vector" name is also less than ideal because it shadows a function name from clojure.core (this is allowed but makes code harder to read)


ah yeah, that is true too.


coll is a good name there