Fork me on GitHub
#clojure
<
2020-01-07
>
seancorfield06:01:17

No, the hash map is created when the namespace is loaded (compiled).

seancorfield06:01:44

I would expect it to be constructed once at compile time... but I'd have to verify that...

seancorfield06:01:48

I'd decompile that code but I don't have a JDK 8 on my laptop, only on my desktop 🙂

seancorfield06:01:16

Hmm, oh, I could ssh into my desktop... just a sec...

seancorfield06:01:47

Yup, confirmed, It compiles into static initializers:

static {
        const__0 = RT.var("clojure.core", "apply");
        const__1 = RT.var("clojure.core", "str");
        const__2 = RT.var("clojure.core", "map");
        const__3 = RT.var("clojure.core", "comp");
        const__12 = (AFn)RT.map(7L, "rwx", 6L, "rw-", 5L, "r-x", 4L, "r--", 3L, "-wx", 2L, "-w-", 1L, "--x", 0L, "---");
    }

seancorfield06:01:36

The top of that class (for the function):

public final class user$fn__194 extends AFunction
{
    public static final Var const__0;
    public static final Var const__1;
    public static final Var const__2;
    public static final Var const__3;
    public static final AFn const__12;

    public static Object invokeStatic(final Object permissions) {
        return ((IFn)const__0.getRawRoot()).invoke(const__1.getRawRoot(), ((IFn)const__2.getRawRoot()).invoke(((IFn)const__3.getRawRoot()).invoke(const__12, new user$fn__194$fn__195()), ((IFn)const__1.getRawRoot()).invoke(permissions)));
    }

seancorfield06:01:17

It's often surprising how efficient the simple, obvious approach in Clojure can be...

andy.fingerhut07:01:02

You can of course have maps that have keys and/or values that are not compile-time literal constants, and then the map is constructed on every call.

andy.fingerhut07:01:14

And I don't think there are any kinds of techniques in Clojure's compiler to try to recognize the largest "sub-map" that is constant, versus the part that is variable, but I haven't checked the decompiled Java bytecode to confirm that.

Hi08:01:28

Hello, how to make JVM running Clojure and Timbre logging to log into systemD log system (journalctl, etc.) ?

thumbnail10:01:52

i’m not certain; but i think systemd can be setup to listen to stdout. so if you configure the service correctly it’d just work without timbre configuration

Hi10:01:39

Yes it can easily do so, but log entries then break into separate lines. instead of being same multiline entry...

vemv12:01:59

someone asked exactly this a couple weeks ago. recommendation being to use a logging solution that doesn't reject the jvm ecosystem, where systemd appears to be a solved problem I don't doubt this can be solved otherwise (w/ a custom appender), though

antonmos17:01:21

it has a timbre appender

Hi10:01:51

Who wants lots of karma 💫 to earn. can you answer this question about loging? https://stackoverflow.com/questions/59306521

tianshu16:01:05

hi, I found almost all tutorial about transducer is talking about how to use and how to write a transducer? is there some article or post about how to write function that receive a transducer?

tianshu13:01:42

@U0JUM502E yes, I'm looking for something like this, the first article you give. It has a implementation for ptransduce, it's a great example. thank you very much

folcon13:01:03

no problem =)…

huthayfa18:01:50

Hi, I need help with clj-http! I am trying to write exception handler for when clj-http return 503 (service unavailable, socket-timeout, connection-timeout ...), The following is the function I have

(defn get-from-client
  ""
  [endpoint]
  (let [service-name (str (:name envs) endpoint)]
    (log/info {} (str "service API:" service-name))
    (future (try
              (client/get service-name client-config)
              (catch Exception {:keys [status headers body]}
                   (log/error status)
                )))))
I am expecting the status, headers and body to be bound to the values returned in the exception map??

huthayfa18:01:07

but all these keys are empty

ghadi18:01:45

exceptions can't be destructured like that @huthayfa.ainqawi

huthayfa18:01:41

https://github.com/dakrone/clj-http this is the library, can you check the Exceptions section @ghadi

p-himik18:01:33

They don't use try, they use try+ from https://github.com/scgilardi/slingshot.

ghadi18:01:38

that section is advocating the use of a library named Slingshot to handle exceptions

huthayfa18:01:45

yeah I used try+ too

p-himik18:01:14

It's just try in your example through. A typo?

ghadi18:01:47

I wouldn't recommend this

p-himik18:01:41

Out of interest - why? I've never used it before but wanted to check out.

ghadi18:01:44

1) I'd rather deal with data, not exceptions (where possible) 2) ex-data and ex-info are built into core, and largely obsolete Slingshot

p-himik18:01:20

Makes sense, thanks!

huthayfa18:01:55

So, you are recommending to use slingshot with :throw-exceptions false?

ghadi18:01:02

I’m recommending to avoid slingshot

ghadi18:01:23

If you use that setting, a 500 status will still return a map

ghadi18:01:38

Then you can inspect it and do whatever you want with it

ghadi18:01:45

Like rethrow if desired

ghadi18:01:16

consider using :throw-exceptions false in your request map

👍 4
ghadi18:01:23

and then using the response as data

huthayfa18:01:00

you wouldn't recommend slingshot? @ghadi

ghadi18:01:44
replied to a thread:I wouldn't recommend this

1) I'd rather deal with data, not exceptions (where possible) 2) ex-data and ex-info are built into core, and largely obsolete Slingshot

ghadi18:01:10

One gotcha with clj-http + :throw-exceptions false is that it will still throw an exception if the host name fails to resolve

mmeix18:01:19

What is the most idiomatic way to or over a collection? Obviously (apply or [false true false]) doesn’t work (`or` being a macro). I wrote (reduce #(or %1 %2) [false true false]) , which works. Better ideas?

mmeix18:01:24

ok. (some true? [false false]) => nil instead of false (which is falsey of course). Just wanted to know if I oversaw something)

papachan18:01:02

(some #(= false %) [true false true])

mmeix18:01:52

fair enough 🙂

papachan18:01:00

and this one seems to work as well: (some #(true? %) [true false true])

dpsutton18:01:02

can wrap with boolean

mmeix19:01:02

(some #(true? %) coll) seems to be the same as (some true? coll) , no?

dpsutton19:01:45

(fn [x] (f x)) is equivalent to (f x)

mmeix19:01:38

ok, just wanted to make sure, i understand papachan’s second answer

dpsutton19:01:22

yeah. my thing there is a bit different since f isn't invoked there. should be ((fn [x] (f x)) 3) is (f 3)

mmeix19:01:49

obviously

jaihindhreddy06:01:26

Definitely not identical behavior though: especially in scenarios where the fn is called incorrectly. If f has an arity-2 and (fn [x] (f x)) is being called somewhere with 2 args and the fact that it throws ArityException is used as control flow, this replcaement will break. That is of course horrible code that no one would ever write, right? Just sayin' 😄

hiredman19:01:22

use identity, not true?

mmeix19:01:27

both (*some* *identity* [false false]) and (some true? [false false]) result in nil. Question of idiom here?

hiredman19:01:48

some returns the result of the first time the function returns a truthy value or nil

hiredman19:01:26

user=> (some #{:x} [:x])
:x
user=>

hiredman19:01:41

user=> (if (some identity [false]) :a :b)
:b
user=> (if (some identity [true]) :a :b)
:a
user=>

hiredman19:01:49

slack has now started mangling code blocks, great

mmeix19:01:43

(some true? …) has the advantage of sounding like a spoken sentence …

papachan20:01:48

user> (some boolean? [true false true])
;; => true 

mmeix21:01:41

that’s a real truism 🙂

mmeix21:01:13

(*some* *boolean?* [false]) =>true

hiredman19:01:19

is sounding like a spoken sentence an advantage?

mmeix19:01:31

not per se, was just a thought … the design choice to call this function “some” might come from this …?

mmeix19:01:08

whatever 🙂

p-himik19:01:10

In Python, thinking that this is an advantage has led to such great achievements as

x = 1 if some_number > 0 else 42
I don't think this is an advantage. :)

mmeix19:01:18

ok, let’s file this under newbie thoughts then 😋

ghadi19:01:32

@huthayfa.ainqawi there are some other things that are thrown when even when you ask for :throw-exceptions false

ghadi19:01:00

Timeouts, for one

borkdude22:01:02

For a private class like java.lang.UNIXProcess which is the return type of (.start (java.lang.ProcessBuilder. ["ls"])), what's a good way to get its public name using reflection (which is java.lang.Process)? Just walk up the inheritance tree and find the first public parent?

vemv22:01:49

I took a look into .getCanonicalName and friends but none of those return java.lang.Process, sadly

borkdude22:01:36

the only way to get this is probably to do (pseudo-code):

(if (java.lang.reflect.Modifier/isPublic (.getModifiers the-class)) the-class (recur (.getSuperClass the-class)))

👀 4
ghadi22:01:36

yes

👍 4
ghadi22:01:20

public in java 8 -> class is public public in java >8 -> class is public and package is exported if a module package

ghadi22:01:18

;; java 8
clj -e '(.start (java.lang.ProcessBuilder. ["ls"]))'
#object[java.lang.UNIXProcess 0x6f19ac19 "java.lang.UNIXProcess@6f19ac19"]

;; java 13
clj -e '(.start (java.lang.ProcessBuilder. ["ls"]))'
#object[java.lang.ProcessImpl 0x25f7391e "Process[pid=43203, exitValue=0]"]
@borkdude

borkdude22:01:22

yeah, I noticed the difference.

ghadi22:01:20

It's not super helpful to see a non-public class in a REPL output

ghadi22:01:40

nor private methods when calling clojure.reflect/reflect