Fork me on GitHub
#clojure
<
2018-03-27
>
qqq00:03:13

C++ has a great notion of destructors. I can create a "smart pointer" that points to an external resource, where the destructor releases the resource. Thus, when nothing points at the smart ptr, we auto release the resource. Is there anything like this in Clojure?

andy.fingerhut00:03:24

For Clojure on the JVM, I believe there are articles people have written about Java finalizers, which are intended to be run when a Java object is about to be garbage collected, that give all kinds of warnings and caveats about there being no guarantee such a thing will ever be called.

andy.fingerhut00:03:38

This Wikipedia page may contain mentions of most of the known issues: https://en.wikipedia.org/wiki/Finalizer

justinlee00:03:32

in C++ destructors are also either expressly invoked via delete or by the close of lexical scope. if that’s enough for you, you should be able to simulate that effect easily enough

jgh01:03:04

i dont deal with java all that much but last I looked for a destructor-like mechanism people basically just added a “close” method by convension

jgh01:03:38

i guess this just comes down to differences in garbage collection….c++ is really explicit about when that happens (which is, right now), whereas jvm it may be several minutes before destruction/deallocation of an object

jgh01:03:55

or if you’re like that old newsgroup post and you’re writing flight control software for cruise missiles destruction happens in the most literal way possible

qqq01:03:36

Let me expand my problem a bit more. Suppose we have:

(let [a1 (alloc some resources)
    a2 (alloc some other resources)
    a3 (allow yet more resources)
    a4 (and more resources)]
...
)
now, any of these allocs may fail. If a3 fails, I need release to be called on a1 & a2. I believe C++ scoping rules takes cares of this with regards to exceptions unwinding the stack. With Clojure, this seems like atleast four try-catch clauses.

the2bears01:03:04

Depending on what the resources actually are, this is similar to 'with-open', except you don't want to close things when they leave the function's scope. It's also similar to the component/mount/integrant life-cycle ideas.

the2bears01:03:42

"at least four try-catch clauses"... well, that's what macros are for 🙂

qqq01:03:17

this is for allocating webgl buffers, textures, programs, shaders

the2bears01:03:20

I think the idea can transfer to those. Are they cleaned up only in case of a failure to allocate? (well, and at the "proper" clean up time, too)

the2bears01:03:35

Sort of an all or none?

qqq02:03:29

I think I'm going to just ignore recovery.

qqq02:03:01

If any allocation fails, it'll just display an error and die.

justinlee04:03:15

@foxlog I assume you mean it works when you execute it in the repl? I think the issue is that for is lazy so it doesn’t execute any side effects. Stick a doall around it

✔️ 4
seancorfield04:03:28

@foxlog Since you are executing updatewithchinese purely for side effects and you do not need/use its value, you'd be better off using doseq instead of for.

👍 4
foxlog05:03:04

Great! Thanks.

seancorfield05:03:44

(defn translate [from to]
  (doseq [result (querywords from to)]
    (updatewithchinese (Translate/translate (:words result)) (:words result))))

the2bears05:03:54

Well, you're printing them out in the order you see. The order of the (println ...) expressions is explicit.

benzap05:03:38

Yeah, line 35 through line 37 are blocking calls. So (<!! c1) blocks until it retrieves it's value, and so on. One way around this is to use alts

benzap05:03:14

Why do you say you don't need the alts! method?

benzap05:03:50

if you know the execution time of each of those queries beforehand, you could try reordering your blocking calls programmatically

benzap05:03:44

ie. (println (<!! c3)) (println (<!! c2)) (println (<!! c1))

foxlog06:03:51

@benzap how about if I don’t know execution time? I mean let first finished first printed.

ajs08:03:00

I have an interop call for a protobuf message that looks like this: (RxAPI1.Rxapi$ServerMsg/parseFrom msg) and I'm trying to figure out if there is a way for me to type-hint msg, as the reflection warning says call to static method parseFrom on RxAPI1.Rxapi$ServerMsg can't be resolved (argument types: RxAPI1.Rxapi$ServerMsg).

borkdude09:03:59

I created a #joker channel for scripting/linting in https://github.com/candid82/joker

orestis09:03:02

In this case you should not use 3 channels, but only one. Have all queries write to the channel, then take 3 times from it, you will take the results in the order they were put in the channel.

orestis09:03:03

Ah, now I see that the queries are not actually doing anything with the channels. If you want to do three queries then use the fastest, then you need to create three channels and use alts!.

qqq10:03:32

for transients, I see assoc! -- is there an update! ?

qqq10:03:15

Something that looks like:

(defn update! [tm k f]
  (assoc! tm k (f (get tm k))))

bronsa11:03:02

use your own

pooboy11:03:13

Long time people !

Maris11:03:23

Is it possible to exclude some folders from ag search path in emacs? I also use projectile but .projectile file didn't help.

bronsa11:03:48

might be a better question for #editors

ajs11:03:44

or for #emacs

pooboy11:03:13

(import java.util.ArrayList)
( def cart (new ArrayList))
(.add cart "sugar")
(println cart )

#object[java.util.ArrayList 0x2db68f [sugar]] 

pooboy11:03:32

can it print only sugar ,when I call println()

rauh11:03:50

Try (println (first (seq cart))

Cynical3.1416:03:36

Hi, everyone! I am new here, don't quite know how it works. I was looking for support on how to install the required resources for running anglican code in closure. I can't find useful documentation

jcr16:03:32

@black.franco.formicol you're talking about this: https://github.com/probprog/anglican - right? You need to install lein first: https://leiningen.org/ Then add [anglican "1.0.0"] to your project.clj file, then run lein repl and follow their intro tutorial

Cynical3.1419:03:34

I'll try that in an hour and let you know as now I can't, but thank you for your reply!

Cynical3.1420:03:34

I don't know exactly where I should add [anglican "1.0.0"] to my project.clj file

Cynical3.1420:03:55

is under :dependencies?

Cynical3.1420:03:24

that row was :dependencies [[org.clojure/clojure "1.8.0"]]

Cynical3.1420:03:41

shall I make it :dependencies [[org.clojure/clojure "1.8.0"] [anglican "1.0.0"]]

Cynical3.1409:03:16

Hi, again! Sorry to bother, but I tried to follow the instructions on the paged linked but the link from which I should download using git doesn't work and I can't find any alternative tutorial

jcr17:03:41

Sorry, I don't quite follow what you're talking about. What exactly you want to download using git?

jkouros07:04:24

@black.franco.formicol There is an anglican channel on slack, activity is rather low, but still: https://clojurians.slack.com/messages/C471VDS9L

jkouros07:04:44

Also, the official repo for anglican has moved to bibucket: https://bitbucket.org/probprog/anglican I assume that you have already seen the anglican website. Unfortunately, the page for the examples has been broken for a while, but I already created an issue and the developer said he is going to fix it. If you have further questions an using anglican, just post them on the channel or ask me directly.

Cynical3.1420:03:57

(thanks in advance)

jongar21:03:14

What is a "key function"? The term is being used in "Clojure for the Brave and True" book

tanzoniteblack21:03:55

@xjongar can you provide context of how it's being used?

the2bears21:03:03

@xjongar I think that book is on line, if you could link to the context that would be easier.

noisesmith21:03:31

my first guess is it means using a keyword as a function

jongar21:03:52

> If your sorting needs are more complicated, you can use sort-by, which allows you to apply a function (sometimes called a key function) to the elements of a sequence and use the values it returns to determine the sort order. I am confused about the difference between a key function and a normal one.

Michael Fiano21:03:02

Oh it's just how it's used. Key means a function used to compare values, and originates from other Lisp's use of the term.

Michael Fiano21:03:34

Common Lisp's sort function (and a lot of other functions) for example, accepts an optional "key" parameter, which is a function used to sort by, such as <, >, etc.

tanzoniteblack21:03:00

(sort [3 5 4]) ==> [3 4 5] clojure knows how to sort numerics (sort [{:my-key 4} {:my-key 3}]) ==> ClassCastException clojure.lang.PersistentArrayMap cannot be cast to java.lang.Comparable clojure.lang.Util.compare (Util.java:153) Clojure doesn't know how to sort maps (sort-by :my-key [{:my-key 4} {:my-key 3}]) ==> ({:my-key 3} {:my-key 4}) I tell clojure to sort by running a function on each value, in this case :my-key (which is the same as (fn [x] (get x :my-key)), and use the resulting value to sort

jongar21:03:28

I see, thanks for the clarifications. I started learning Clojure in the past few days and I am enjoy it so much 🙂

👍 20
Michael Fiano21:03:46

As have I. It is nice, and welcome!

👍 16
Wesley Matson21:03:23

Is there something like clojure.data/diff that compares doubles with a tolerance?

Michael Fiano21:03:31

(<= (Math/abs (- i j)) tol)?

Wesley Matson21:03:54

Sort of, but for all the values in two map structures

Alex Miller (Clojure team)21:03:35

you could wrap diff and just remove the differences that were caused by a double delta < epsilon

Wesley Matson21:03:25

(fancy-diff {:a {:b 3.14159265358979 :c 2}} {:a {:b 3.141592653589793}} tolerance) => [{:a {:c 2}} nil {...}]

Wesley Matson21:03:46

@alexmiller probably will do that, just was hoping there was something already done, thanks

Alex Miller (Clojure team)21:03:49

generally, trying to change the meaning of equality in Clojure turns out poorly

Wesley Matson21:03:26

Yup, I just have some doubles coming from a network request that end up (Math/ulp the-lower-one) in distance

jcr21:03:34

diff-by would be a good idea, don't you think?

Wesley Matson21:03:25

Thanks for the source link, I didn't see it in clojuredocs when I looked

Wesley Matson21:03:48

Have to catch a bus now, but this gets me more or less where I was looking to me 🙂

seancorfield21:03:46

@xjongar Since you're new to Clojure, you'll find the folks in #beginners are super helpful! Everyone in there is either a beginner or someone who has "signed up" specifically to help beginners. And "Welcome to Clojure!" 🙂

👍 4
dominicm22:03:32

Is it possible to resolve and "call" a macro, in the same way that you would a function?

dominicm22:03:27

For a CLI program, I am expecting a symbol passed in, which points at a macro. I need to use this macro to run some code.

schmee22:03:53

seems to work?

user=> (resolve 'clojure.core/->)
#'clojure.core/->

schmee22:03:32

right, but then it's the calling...

schmee22:03:26

user=> (def arrow (resolve 'clojure.core/->))
#'user/arrow
user=> (eval `(~arrow {} (assoc 1 2)))
{1 2}

dominicm23:03:57

Evil, I love it

dominicm22:03:27

For a CLI program, I am expecting a symbol passed in, which points at a macro. I need to use this macro to run some code.