Fork me on GitHub
#beginners
<
2017-12-10
>
hiredman01:12:23

am not sure why you get different behavior there, but embedding a function object in code is not something you should ever do, even though sometimes you can get it to work

hiredman01:12:45

your macro should return a form that when evaluated returns a function object

noisesmith01:12:06

yes, that's what I said before sharing that example

noisesmith01:12:14

I was just making a smaller reproduction of his issue

drewverlee01:12:46

If your working with a server, like compojure, it seems like you need to call your project functions from a namespace outside the ... project one. For example, i was having trouble evaling my compjure app inside emacs while jacked into my cider repl.

(-> {:uri "/spec/plus"
     :request-method :post
     :body-params {:x 1 :y 2}}
    app
    :body
    slurp
    (cheshire.core/parse-string true))
Results in an error. But lein repl
user=> (use 'example.handler)
user=> (-> {:uri "/spec/plus" :request-method :post :body-params {:x 1 :y 2}} app :body slurp)
"{\"total\":3}"
is a success. So two questions. 1. Why does the later work? 2. And how do i get back to using my editor? as typing in the terminal is a pain. I fee like this is something stuart has talked about before with his "repl driven development". update: it seems i just need to start the server at the same time. It would be cool if someone would give me a hint why this is, though i can probably guess its because ... actually im not sure. Some components that the repl are calling out to aren't ... idk.

hiredman01:12:00

in general, it is a constructor issue, because the compiler generates a call to a no arg constructor for random objects embedded in code

noisesmith01:12:28

and it's an issue that comes up with ((m)) but not (def f (m)) (f)

hiredman01:12:29

my guess would be (foo) followed by *1, doesn't actually get compiled to bytecode

noisesmith01:12:38

ahh! that would explain it

New To Clojure09:12:37

Is memoize thread-safe?

adityaathalye10:12:27

I don't know the answer, but hazarding a guess from the source for memoize. memoise uses an atom to maintain the cache, which means any cached value would be local to a thread.

schmee11:12:09

wait, what? AFAIK atoms are not thread-local

schmee11:12:07

but yes, it keeps the values in an atom so it is thread-safe

schmee11:12:13

a word of caution: if you have a lot of different return values memoize can eat up all your memory, since it never removes or expires values

New To Clojure11:12:47

@U3L6TFEJF oh wow, haven't expected that! Thank you @U051MHSEK, @U3L6TFEJF a lot

adityaathalye11:12:10

@U3L6TFEJF is correct --- atoms are not thread local (vars are). Apologies for the misinformation, @U879F6GV7

schmee12:12:17

if you are writing performance sensitive code, I recommend something like https://github.com/ben-manes/caffeine instead of memoize

schmee12:12:06

it has a bunch of different variants of retention and much better performance 🙂

New To Clojure12:12:32

@U3L6TFEJF Thank you, it's useful lib!

adityaathalye10:12:27
replied to a thread:Is `memoize` thread-safe?

I don't know the answer, but hazarding a guess from the source for memoize. memoise uses an atom to maintain the cache, which means any cached value would be local to a thread.

timo17:12:59

Hi there I am desperate to find a way to retrieve a list of keys from a map. It would be awesome if someone could point me into the right direction. following my map and my function so far:

timo17:12:21

so I want to filter by the second element of the vector-key and retrieve the values for multiple items

timo17:12:17

I can't wrap my head around how I could make a partial here or export the anon-func to a function. Any help much appreciated!

dpsutton17:12:15

so if you run filter on a map, it's not gonna see a map but map entries, which we can treat as two element vectors

dpsutton17:12:23

so calling key in your filter won't work

dpsutton17:12:43

although on second thought i was thinking keys so perhaps it does

dpsutton17:12:28

@timok what do you mean create a partial or export the function?

dpsutton17:12:28

advent.day3> (defn is-lib? [lib entry]
               (= (second (key entry)) lib))
#'advent.day3/is-lib?
advent.day3> (filter (partial is-lib? "bidi") libs)
([["bidi" "bidi"] 951])

timo18:12:18

let me see...

timo18:12:35

you answered yourself the partial...export the function means make it a second function

dpsutton18:12:45

(def bidi? (partial is-lib? "bidi"))

dpsutton18:12:10

or (defn is-bidy? [entry] (is-lib? "bidi" entry))

timo18:12:08

what I want to accomplish is to search in a huge list (like above but bigger) for a set of library-names like "bidi".

dpsutton18:12:05

what is missing from this solution

timo18:12:21

I have to understand it ... let me see

timo18:12:32

mmh...either I don't get it or it is not working... I need a function that I can give multiple names of libraries and that gives me their names and numbers back

timo18:12:47

how can I give above functions multiple arguments?

dpsutton18:12:19

so you want to take a set of libraries you care about and filter the list for libraries in that set?

timo18:12:43

exactly! thanks by the way for your help so far

dpsutton18:12:47

advent.day3> (defn is-lib [desired-libs entry]
               (contains? desired-libs (second (key entry))))
#'advent.day3/is-lib
advent.day3> (filter (partial is-lib #{"bidi" "suchwow"}) libs)
([["marick" "suchwow"] 736] [["bidi" "bidi"] 951])

dpsutton18:12:06

so now is-lib takes a set of desired libraries and we just ask if it contains the entry

dpsutton18:12:36

the reader syntax #{"bidi"...} creates a set. so we ask if the set of libs we care about contains an entry. and filter the list with that

timo18:12:36

awesome! thanks! I am trying to make this filter as a function...let me see

timo18:12:40

so with a multi-arity function I have to build in this filter-function another map, right?

dpsutton18:12:37

i'm not sure i follow

dpsutton18:12:51

what is the signature of the function you are envisioning?

dpsutton18:12:02

do you want it to take the collection and return the "good" ones?

dpsutton18:12:23

but this problem is inherently "multi-arity" as you have two variables. the list of libraries and which ones you care about

timo18:12:00

I am having this large list of libraries and I want to compare some of them by their number of downloads

timo18:12:30

this is the function that should take a variable number of args and return the corresponding libraries

timo18:12:41

so the user queries for bidi and suchwow and gets them with their corresponding number of downloads back

dpsutton18:12:45

seems to be the case of the function above? ([["marick" "suchwow"] 736] [["bidi" "bidi"] 951])

timo18:12:22

yeah right. I just don't get the additional map over the arguments at the moment...I will try it tomorrow

timo18:12:43

thank you!

drewverlee21:12:11

Any idea how you would get the output from expound to be readable in the repl? I'm using emacs

drewverlee21:12:00

the name was like eval-exp-fu-cider-pprint-eval-sexp-inner-list

avfonarev22:12:14

Hi! Is there an efficient way to use map with a vector and get a vector in the output? Other than (vec (map inc [1 2]))?

avfonarev23:12:25

Gosh, I'm getting blind. Thank you!

athomasoriginal23:12:25

I am starting to dig into spec and I want to contextualize it for my own understanding. To me, at a minimum, one could use spec as a type system, would this be valid? However, given that it can do so much more, how does the argument that Clojure does not have types still get asked? In order to get the same functionality in JS, I would be required to use a combination of Flow, Joi and Chance (types, assertions and test data generator respectively)