Clojurians
#clojure
<
2016-03-04
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

ag06:03:31

how can I map over list of functions and apply each of them on a value, to avoid constructs like this:

(str "//" (.getHost cs) (get-port-str cs) (.getPath cs))

rauh06:03:45

@ag juxt

ag06:03:41

@rauh: yeah, right… thanks!

ag06:03:47

errr.. juxt seems to be failing for java methods, what am I doing wrong?

ag06:03:12

I mean dot special form doesn’t work

ag06:03:03

I can do:

((juxt #(.getHost %) get-port-str #(.getPath %)) cs)
but defeats the purpose

rauh06:03:36

@ag you could use memfn or just wrap it in a quick anonymous function #(.getHost %)

danielcompton07:03:33

@ag you can't treat a Java interop methods as anonymous functions (unfortunately, I've wanted the same in the past)

dm307:03:06

@danielcompton: there's memfn that works around that somewhat

george.w.singer09:03:42

Is it possible to define a multimethod as a (let...) binding?

george.w.singer09:03:17

The purpose for doing so is that I need to invoke the <! macro within the definition of a multimethod's method. So I'm trying to put the definition of this multimethod within a (go ...) block. And I know that I can put a go block within a let statement. So if I could define a multimethod as a let binding (wrapped inside a go block), I would safely be able to use <! within the multimethod.

dm309:03:21

did you try?

george.w.singer09:03:28

I don't know how I would go about it? I guess I would need to invoke an anonymous multimethod? So then I could go

(go (let [m (anonymous-defmulti ...)]
         (defmethod m :default 
         ;; ...
          (<! ...))))

george.w.singer09:03:55

Does clojure have anonymous multimethods?

dm309:03:18

(go
  (defmulti x ...)
  (defmethod x ...))
?

dm309:03:01

the defmulti/defmethod macros create a var, they don't return a value

dm309:03:36

basically a bunch of side effects

dm309:03:54

still can't say I understand what you are trying to achieve

rauh09:03:24

@george.w.singer: You can't, the problem is the go macro. It won't walk into function definitions since they have a potentially different execution context.

rauh09:03:56

But I'd argue that you approach this problem incorrectly. Pass channels around and do your own go block in the defmethod

george.w.singer10:03:59

You're probably right

george.w.singer10:03:27

Basically: I'm trying to avoid a remote read function (within a parser) from returning channel values.

george.w.singer10:03:47

I'd like it to instead wait, pull the value of a channel, and return it to the client

rauh10:03:32

Make an example. I'm confused

george.w.singer10:03:25

(defmethod server-reader :default
  [env key params]
  {:value (fn-that-returns-chan)})  ;; <-I'd like to be able to just go (<! (fn-that-returns-chan)) so that the client doesn't have to deal with a channel.

george.w.singer10:03:06

Does that make sense?

rauh10:03:26

@george.w.singer: Use the blocking <!!

george.w.singer10:03:24

I'm unable to since I'm using clojurescript on the backend targetting nodejs

rauh10:03:45

Oh I remember, you're the "node guy" :simple_smile:. Well in that case you'll have to deal with async hell all the way back to the response. I don't know node.js well enough so I doubt I can help you.

george.w.singer10:03:51

At least I don't have to wait 5 seconds for my clojure server to spin up every time I restart it. With node.js it's about 100 milliseconds. ...the down side is I only get one thread :disappointed:

dm310:03:42

hehe, but in Clojure you don't have to restart the server on every exception

dm310:03:44

:simple_smile:

george.w.singer10:03:36

But when you DO restart it, by the time it's done, you'll be as old as Rich Hickey :joy:

rauh10:03:37

@george.w.singer: This is getting off-top: 1. The correct channel for this should be #C03S1L9DN . 2. I haven't restarted my REPL in a few days, no waiting :simple_smile: 3. To address your problem: IIRC there is tons of async libraries in node.js land where you can "deal with" multiple async results to be merged together. I'd pass in some function (in env) that registers your async result. Then before you shoot off the response that async library should wait for all of the async tasks that are pending. Then somehow get the value out of the async result and actually do the response. I might be missing some details.

george.w.singer11:03:08

If I have a clojure map m which is {:a "a" :b "b" :c "c"}, how can I generate a new map which has exactly the same keys as m but passes each of the keys through a function f? The output in this case would be {:a (f "a") :b (f "b") :c (f "c")}

george.w.singer11:03:18

Is this a general way to make this happen in clojure?

schmir11:03:27

george.w.singer: I don't think there is a function in clojure.core to do this.

dm311:03:51

medley.core/map-vals

george.w.singer11:03:53

@dm3: yes that's perfect

andmed11:03:18

Hello everybody. I am from Java. Like lisp, SICP. Will be rewriting a small project in Clojure.//Moscow

gnejs13:03:02

ohoj and good morning!

mbertheau13:03:43

What's idiomatic for (or (= n 3) (= n 8) (= n 920))?

gnejs13:03:12

I’ve got some code that just doesn’t feel right… https://gist.github.com/metamorph/e1eb70ae65f5451aeefc — I check an input hashmap for existence of an entry, if that’s true then I try to apply a regex to another entry and then finally create a new entry in the map. If any of these steps are falsy i want to return the input. Any suggestions?

mpenet13:03:36

I guess you could (contains? #{3 8 920} x)

mpenet13:03:57

its a lot slower tho

mpenet13:03:09

but it might or might not matter

mbertheau13:03:36

It doesn't really in my situation. contains? looks nice! Thanks :simple_smile:

gnejs13:03:11

@mbertheau: also (some? (#{3 8 920} n)) should work

mpenet13:03:50

you can even skip contains and abuse Ifn on Sets

mpenet13:03:11

(#{3 8 920} x)

mpenet13:03:31

not very readable tho

mbertheau13:03:44

Yeah, contains? looks the most readable so far.

yogidevbear14:03:40

Just curious how many people are actively using Clojure in a data science or machine learning role on a daily basis?

schmir14:03:51

gnejs: cond-> sometime helps...but in your case I'd just factor out a get-func-class function

yogidevbear14:03:01

Or working in a data science or machine learning role, using a different language?

gnejs14:03:34

@schmir: thanks for having a look… I’ll read up on cond->

schmir14:03:24

gnejs: I also sometime use something like (merge thing (when foo? {:x "x"}) (when bar? {:y "y" :z "z"}))

gnejs14:03:25

@schmir: what annoys me most is the multiple returns from the fn.

gnejs14:03:35

hmm.. that could be a suitable form.. return the input by default and explicitly decorate the result if “somecondition”.

gnejs14:03:30

oh, that looks interesting..

urbanslug15:03:03

Hello are you guys here?

mmeix15:03:17

some are ... :simple_smile:

urbanslug15:03:24

So I noticed some very interesting things while doing some profiling.

urbanslug15:03:41

Equality checks are very expensive and I wonder why. Let me show my results.

urbanslug15:03:01

This is the function with the equality check

(defn to-prof
  [listt]
  (let [head (first listt)]
    (if (= head nil) ; equality check
      0
      (+ head (to-prof (rest listt))))))

This is the function without the equality check
(defn to-prof
  [listt]
  (let [head (first listt)]
    (if head ; no equality check
      0
      (+ head (to-prof (rest listt))))))

urbanslug15:03:13

Results vary greatly.

jethroksy15:03:12

@urbanslug: could you try that again with (nil? head)

urbanslug15:03:56

@jethroksy: Give me a min:

dm315:03:43

your 'fast' case is wrong

dm315:03:53

you need to if-not to have the same result as the slow case

jethroksy15:03:48

oh yeah good catch

urbanslug15:03:26

With the nil? it's the same...

urbanslug15:03:46

dm3: Well the question is really why the equality check is so expensive.

urbanslug15:03:56

dm3: Let me try the if-not

dm315:03:02

it's not expensive

dm315:03:12

your code doesn't do anything in the 'fast' case

urbanslug15:03:17

@jethroksy: nil? is the same as an equality check

dm315:03:26

102 > boot -d tulos/boot-criterium bench -g "'`cat slow.clj`" -l slow -Q -- bench -l fast -g "'`cat fast.clj`" -Q -- report -f table -O

| :benchmark/goal |       :mean |  :variance |    :upper-q |    :lower-q | :evaluation-count | :outlier-effect |
|-----------------+-------------+------------+-------------+-------------+-------------------+-----------------|
|            fast | 201.6449 ns | 24.3393 ns | 239.6927 ns | 179.9124 ns |           3336420 |       :moderate |
|            slow | 171.7123 ns | 43.9129 ns | 244.3292 ns | 142.8573 ns |           4134948 |         :severe |

urbanslug15:03:39

dm3: What did you use for that?

dm315:03:02

ah, sry, didn't really call the functions there

dm315:03:12

but my comment is still valid :simple_smile:

urbanslug15:03:46

dm3: I don't quite get it and which profiling tool is that? I'm new to clojure all I can tell is that you're using a lib called boot-criterium

dm315:03:33

well, my comment is regarding the snippet you pasted as the 'fast' case. The snippet doesn't do the same work that the 'slow' case does.

dm315:03:44

the if condition should be inverted

urbanslug15:03:14

dm3: It doesn't? Okay let me look at it again. Now I get your argument.

dm315:03:11

and the bench result that I pasted is using boot together with bench and report tasks from tulos/boot-criterium

urbanslug15:03:59

@dm3: So in the first one it resolves the = then head and applies = to head. In the second one it doesn't call the = it resolves head to a value and if to it just checks if it's nil and short circuits. Am I right?

dm315:03:51

so the second one should be if-not

inhortte16:03:52

I posted this on #C053K90BR, but it seems rather un-occupied... heh: Hello. I'm relatively new to boot and am just trying to create a (pure clojure - no cljs) project that uses 'watch' and recompiles when I change a file in my source-paths. [17:09] I have these tasks: (deftask bundle [] (comp (speak) (aot) (pom) (uber) (jar))) (deftask dev [] (comp (watch) (repl) (bundle))) [17:10] When I change a file, I get Could not locate james/imbibe__init.class or james/imbibe.clj on classpath. (after i edit imbibe.clj) [17:11] Killing boot and executing 'boot dev' once again takes forever to restart (building the uber?). What is the best way to do this? [17:11] Thanks...

swizzard16:03:55

does anyone know if there’s a way to do “parent-of” selecting in enlive?

reefersleep16:03:28

Is it only possible to call deftest`` and have it work properly in top level forms - and why?

reefersleep16:03:21

I wanted to create a number of deftests following a similar pattern, but when I do eg (defn new-test [name arg1 arg2 arg3] (deftest name (is (= arg1 (+ arg2 arg3))))), I get a compile exception

reefersleep16:03:09

a NullPointerException

reefersleep17:03:55

As an attempt to get close to what I want, I've insted done (defn add-test [arg 1 arg2 arg3] (is (= arg1 (+ arg2 arg3)))) and then called it with (deftest two-and-four (add-test 6 4 2)), (deftest seven-and-ten (add-test 17 7 10)) and so forth

reefersleep17:03:15

Try to ignore the silliness of the examples :simple_smile:

jan.zy17:03:44

hmm, https://conj.io/ was my favorite go-to place for searching documentation but a few weeks ago its layout changed and now it’s unusable for me

jan.zy17:03:23

could you suggest any other place where searching clojure docs would be more pleasant than on https://clojure.github.io/clojure/?

jan.zy17:03:20

hm, a bit better. typing set gives me a link to clojure.set docs. http://conj.io displays list of methods from clojure.core only

jan.zy17:03:50

i liked old http://conj.io where i could search but also see a list of all functions on main page

jcromartie17:03:43

is there a way to run a test fixture once for all namespaces being tested?

jcromartie17:03:48

like with (run-all-tests)

jcromartie17:03:37

AFAIK use-fixtures with :once is for each namespace

arrdem22:03:22

@jan.zy: sorry you feel that way, there's a ticket tracking this if you have comment to add. https://github.com/clojure-grimoire/grimoire/issues/231

base69822:03:02

Anyone using the newest Cider?

base69822:03:17

it seems basically broken for me

base69822:03:22

at least the debug parts

base69822:03:43

i keep having to kill the java process or quit emacs altogether

base69823:03:24

How do I install Cider 0.10? I downloaded 0.10 and don't know where to put it. Most everything I install with elpa

base69823:03:22

i did make and it did a bunch of stuff but don't know where to put the dir and there is no elpa

base69823:03:27

no make install i mean

gnejs23:03:14

@base698: wish I could help out :confused: I’m running Spacemacs. No issues with Cider.

gnejs23:03:45

(0.12.0-snapshot)

base69823:03:05

i versioned it, so i just reverted back. I was smarter than I thought

base69823:03:14

can you do locals?

base69823:03:30

hitting the l key in cider debug seems to crash the process

base69823:03:44

or at least it's permanently non responsive

cch123:03:44

Does anyone know how to read clojure source from a file/resource and have it evaluated with a given set of bindings?

cch123:03:52

read doesn’t allow arbitrary bindings that I could find.

smw23:03:30

(binding [*read-eval* false] (read-string "(def x 3)”))

smw23:03:33

will something like that work?

smw23:03:59

(I don’t know, I’m guessing)