Fork me on GitHub
#beginners
<
2018-07-27
>
andy.fingerhut00:07:29

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.

andy.fingerhut00:07:05

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.

pablore02:07:44

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

mfikes02:07:19

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

mfikes02:07:33

(source defn-) is illuminating

tkrauthoff08:07:19

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 😕

tkrauthoff08:07:28

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

ben11:07:28

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: https://github.com/mikera/core.matrix/blob/2300819e4fdc406273e99ea9565548e750d14af4/src/main/clojure/clojure/core/matrix/protocols.cljc#L1245 Does this mean I need to use a different matrix library to create my-matrix (i.e. one of [these](https://github.com/mikera/core.matrix/blob/develop/README.md#status))? Thanks

ben11:07:21

Currently:

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

Alex_Bakic17:07:45

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

Alex_Bakic17:07:49

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

mg17:07:27

@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 {}

Alex_Bakic17:07:51

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

manutter5117:07:42

@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

bringe17:07:37

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. http://www.4clojure.com/problem/22

manutter5117:07:35

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.

bringe17:07:37

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

Alex_Bakic17:07:50

@manutter51 @michael.gaare thank you for the help !

Chase23:07:04

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.

noisesmith23:07:22

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

noisesmith23:07:47

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

Chase23:07:03

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

Chase23:07:12

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:

Chase23:07:18

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?

noisesmith23:07:04

I think you can trivially prove both qualities of that function

noisesmith23:07:21

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

noisesmith23:07:41

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

noisesmith23:07:32

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

user=> (set (map inc nil))
#{}

noisesmith23:07:31

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

noisesmith23:07:06

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

Chase23:07:07

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?

Chase23:07:05

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.

Chase23:07:31

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

sundarj01:07:15

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

Chase01:07:11

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

noisesmith23:07:45

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)

Chase23:07:05

ah yeah, that is true too.

noisesmith23:07:07

coll is a good name there