Fork me on GitHub
#beginners
<
2020-06-25
>
sova00:06:33

Hi i'm wondering about url encoding

sova00:06:53

I use cljs to get the location of the window href location but it has nonenglish characters... it seems to lose its ascii'ness... can I recover this in cljs land

noisesmith00:06:55

setting the URL automatically encodes it

sova00:06:43

thx resolved in cljs

marcus.akre07:06:13

If I have a map of strings. {:a "aaa" :b "" :c "ccc"} what is the best way to make a concatenated string of these values including a prefix only for those keys having a non-empty string. So in this case the resulting string should be "Something: aaa. Something: ccc"

marcus.akre07:06:23

I could maybe use lets or functions but I guess there is an easier way

marcus.akre07:06:29

maybe with-out-str?

dharrigan07:06:30

This works, but I'm sure better minds would make it smaller

dharrigan07:06:44

(clojure.string/join ". " (map #(str "Something: " (val %1)) (remove (comp clojure.string/blank? second) foo)))

dharrigan07:06:02

user=> foo
{:a "aaa", :b "", :c "ccc"}
user=> (clojure.string/join ", " (map #(str "Something: " (val %1)) (remove (comp clojure.string/blank? second) foo)))
"Something: aaa, Something: ccc"

teodorlu07:06:35

Thread-last seems to be suitable, otherwise copying @dharrigan's code:

(def foo {:a "aaa" :b "" :c "ccc"})

(->> (vals foo)
     (remove clojure.string/blank?)
     (map #(str "Something: " %))
     (clojure.string/join ", "))
;; => "Something: aaa, Something: ccc"

dharrigan07:06:52

yes, I debated thread too, but I though that comp would be a bit easier to grok initially, but that said, thread macro is pretty straight forward too

dharrigan07:06:03

yours is nicer 'tho 🙂

marcus.akre08:06:19

thank you guys! I appreciate it and will try them out 🙂

michael.e.loughlin10:06:00

Are watchers intended to be used as part of regular programming flow? Or are they mainly for observability?

danieltanfh9510:06:56

iirc most of the time you want core.async instead

lukas.block15:06:24

Hey, is using a multimethod a good way to model the following situation: I get either a scalar or a collection of the scalar, when a collection is transmitted I just call the "base" method with map or reduce. Or is it better to write my own top level dispatch function?

noisesmith15:06:16

the best practice is to never have a function that takes a collection or non-collection in those cases where it's mandatory (some api you don't control sometimes gives you an item outside a collection, sometimes gives you a collection) use the following at the point you acquire the data:

(defn fix-api-result [x]
  (if (coll? x)
     x
     [x]))
other solutions (including multimethods) end up profilorating the implementation detail of a bad API design into the rest of your codebase

lukas.block15:06:50

Ah ty good point. I haven't thought about, just wrapping my type.

noisesmith15:06:03

depending on how you get the data, coll? might be the wrong pred (eg. you might want seqable? and then call seq if true)

lukas.block15:06:34

thx I will keep that in mind

lukas.block16:06:58

How do you configure pprint with cider? What I would like to achieve is that when I press C-c C-e the evaluation result gets pprint. Is that possible? I tried (set! nrepl.middleware.print/*print-fn* clojure.pprint/pprint) but this only works inside the repl ...

lukas.block16:06:29

nvm found it 😇 C-c C-p is what i was looking for

andy.fingerhut16:06:56

FYI there is also a #cider channel here, which is often good for such questions. No problem asking here -- just the expertise on Cider is likely more concentrated there.

abhiyantsingh18:06:02

Hey, couple questions on macros: 1. Is it accurate to say that defmacro is equivalent to defn except that the former is executed at compile time and the latter at runtime? 2. How often do you guys end up writing your own macros in "real-world" code?

abhiyantsingh18:06:55

I guess another way defmacro is different than defn is that its arguments aren't evaluated before invocation. Are there other differences?

smith.adriane18:06:43

no. 1 is not accurate. a common counterexample is that you can call functions defined with defn within a macro

delaguardo18:06:13

Not a compile time but during Read phase in repl

hiredman18:06:26

I think it is accurate, just runtime and compile time are interleaved

hiredman18:06:03

or you could say, it is compile time for the code being macroexpanded, but runtime for the macro and functions that the macro calls

smith.adriane18:06:18

it's not a completely useless distinction, but there's less of a distinction between runtime and compile time in clojure

hiredman18:06:31

because the macro (meta) language and the language are the same it is easy to tie yourself into a knot

smith.adriane18:06:02

macros are not run during the read phase

hiredman18:06:20

they are not

smith.adriane18:06:25

> (read-string "(defn a [] 1)")
(defn a [] 1)

hiredman18:06:53

the compiler does macro expansion before emitting jvm bytecode

delaguardo18:06:04

Read string is for reading string as a list of symbols

hiredman18:06:23

the reader reads things and produces data structures, the datastructures are evaluated (which in clojure's case means compilation to bytecode and execution of the bytecode), and the result is printed

hiredman18:06:43

macro expansion happens in the compiler

hiredman18:06:54

and during the compilation of a given form, the results of previous evaluations (functions, definitions, etc) are available to macros

smith.adriane18:06:30

results aren't printed during evaluation. the printing is a separate step that may or may not happen

hiredman18:06:58

so while compiling form F, the compiler may invoke some other function

hiredman18:06:10

and it is compilation time for form F, but runtime for that other function

smith.adriane18:06:56

but, I concur with @, macros are run at compile time. during compilation, functions created using defn may also be run

abhiyantsingh19:06:18

This helped - thanks all!

abhiyantsingh01:06:25

Are reader macros different from regular macros? Are they compiled at read time or also compile time?

seancorfield01:06:09

Reader macros are really just shorthand in the reader itself and they are very limited -- see https://clojure.org/guides/faq#reader_macros

seancorfield01:06:38

The specific reader macros themselves are discussed here https://clojure.org/guides/weird_characters

smith.adriane01:06:47

but they do happen at read time:

> (pr-str (read-string "@foo"))
"(clojure.core/deref foo)"

seancorfield01:06:12

They're not really "compiled" though. They are almost the very definition of "syntactic sugar" in terms of the source transformation.

abhiyantsingh01:06:35

Makes sense. Thanks!

seancorfield01:06:24

And I don't think anyone answered your original second question @ "How often do you guys end up writing your own macros in "real-world" code?" -- the answer is "rarely".

seancorfield01:06:55

Here's some raw stats from our codebase at work

Clojure build/config 56 files 1006 total loc
Clojure source 318 files 77927 total loc,
    3304 fns, 742 of which are private,
    469 vars, 27 macros, 79 atoms,
    631 specs, 33 function specs.
Clojure tests 349 files 22445 total loc,
    4 specs, 1 function specs.

abhiyantsingh01:06:57

Appreciate the perspective and the data!

abhiyantsingh01:06:57

Also wow that may be the world's largest clojure codebase.

seancorfield02:06:48

@ Oh no, not by far. But it is big enough that it brings some interesting problems in terms of code organization (and code duplication that is hard to find).

sova18:06:16

Hi, I get a strange error that I think has to do with class path... ?

Syntax error (UnsupportedClassVersionError) compiling at (datahike/impl/entity.cljc:1:1).
datahike/java/IEntity has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0

lukas.block18:06:29

I'm not an expert but seems like a java env/path error? Java Runtime only recognizes class file versions up to 52.0 This part is saying you need at least java 8

lukas.block18:06:52

what does java -version say?

lukas.block19:06:14

you may have to configure your ide to use the right java version

noisesmith19:06:46

it really depends on where you got the jar from - for local dev, configure it to compile and be compatible with your VM for using libs, either update to a newer jvm, or find an alternative that works on your old VM

noisesmith19:06:42

(I don't think most of us are using IDEs)

lukas.block19:06:44

🤔 I thought that intellij cursive is quite popular but thats just a guess 🙈😇

sova19:06:37

Thanks I upgraded the server box to java 14 and it is all better now

brandon14920:06:44

I'm using cruxdb, and I'm trying to figure out a good way to return documents that have other documents embedded inside them. The document itself stores an id, but I ned the full data to send to my client.