Fork me on GitHub
#beginners
<
2017-04-28
>
matan10:04:50

I still struggle with this... the docs are almost recursive about these things... I'd like to include a single function from a library in my namespace, and give it my own name

matan10:04:06

what are my options using use or require?

matan10:04:54

I'd like to accomplish something like this:

matan10:04:49

@not-raspberry thanks for the example code

matan10:04:10

is there no one-liner for it? looks like the line relies on the preceding require up there

matan10:04:30

Okay I got it by now

not-raspberry10:04:04

@matan AFAIK (:require ...) works just a normal require, but can be placed "declaratively" inside a (ns ...).

matan10:04:41

@not-raspberry thanks for your help!

matan10:04:32

so, at a higher level

matan10:04:45

require loads a namespace into memory

matan10:04:19

refer or use add whatever specified into the current namespace's symbol table?

not-raspberry11:04:23

treat that as syntax

matan12:04:43

:thumbsup:

matan12:04:53

Finally I got it, after all the confusingly recursive docs

not-raspberry14:04:59

Lesson learned: don't read the docs 🙂

kkruit17:04:15

Question. I have a database connection (hikari-cp) that I'm currently using a future to get the datasource so i don't have to wait for the connection. But now I need the ability to change the connection information and reconnect. I'd like to use an atom for that but using an atom of a future seems weird. Is there a better option? I could use an agent but I like that the a future blocks on a dref where an agent just has a default value.

noisesmith18:04:08

it almost seems like the convenient thing would be a construct that implements the protocols for deref, and had the ability to change from ready status to pending

noisesmith18:04:27

but I haven’t deeply considered the implications of that…

noisesmith19:04:48

for a beginner it probably just suffices to say we have nothing that behaves that way, and the simplest thing is to do nested deref of a future inside an atom @@db-conn

kkruit19:04:59

Okay, I think that will work fine. It's technically what i want; just felt weird.

john19:04:27

@kkruit @noisesmith I was recently playing around with something like this:

(defn refi [k]
  (reify
    IPending
    (-realized? [x]
      (not (nil? (get-in @db [k :o]))))
    IDeref
    (-deref [_]
      (if-let [o (get-in @db [k :o])]
        o
        "pending"))))

john19:04:38

though not very beginner-level

kkruit19:04:48

@john Thanks, I almost get it... I haven't done a lot with protocols yet. I'll save that for later, if it rubs me wrong when I come across my double dref again.

noisesmith19:04:33

@john that breaks this use case as it never blocks on deref

john19:04:37

Yeah, I was playing around in CLJS, where there's no blocking.

noisesmith19:04:24

also it doesn’t differentiate something that never started from something not ready yet

noisesmith19:04:02

(which may be OK, but is worth pointing out when compared to futures)

kkruit19:04:50

I could almost use a promise if i could deliver to one more than once.

noisesmith19:04:20

right - but there’s nothing in clojure that is dereffable and can freely go from realized? to not-realized?

kkruit19:04:33

I think i'm good with deref-ing twice.

john19:04:13

@noisesmith my above code was not built for @kkruit's problem. I was just tossing it up there to give them a starting point. And on the CLJ side, you'll want to implement blocking

not-raspberry19:04:38

I'd be OK with derefing an atom an a future inside.

john19:04:27

But you can see that with the above reification, you could construct a kind of double deref system yourself

kkruit19:04:31

@john I appreciate it. If nothing more it's something to pick apart and learn from.

matan21:04:11

how would you use a collection as a "rest" argument for a function, such that each element of the collection becomes, or is treated as, one argument to the function?

noisesmith22:04:01

(apply + 1 2 [3 4 5]) => 15

jeff.engebretsen22:04:27

I have some more info on the issue I’m having. It’s an Electron project so there’s a nodejs side and a browser side. The nodejs side has (:key map) and works fine. The browser app doesn’t. The only difference I can see is the compiler target. I started from scratch with a lein template this time. The template is electroncljs.

jeff.engebretsen22:04:53

It works from a figwheel repl…

jeff.engebretsen22:04:30

There it is... I was registering the callback fn before the app was ready.

noisesmith22:04:32

ahh - so its getting functions called that don’t exist yet

noisesmith22:04:22

@jeff.engebretsen what I do for this in my app is have the dispatch function check to see if init is done, and if yes it calls the callback, if no it adds the input to a list of messages to act on later (but I don’t have a setup where callbacks would be called before even basic clojure.core stuff is available - that’s trickier)

jeff.engebretsen22:04:10

It's called with a keyboard shortcut then file select. So it's well after setup.

jeff.engebretsen22:04:36

Not sure what happened. I didn't think there would be closure issues like that. But waiting until the app ready callback to register my listener fixed it.

jeff.engebretsen22:04:49

Maybe because my callback was an anonymous fn?