Fork me on GitHub
#beginners
<
2020-11-13
>
didibus01:11:57

@pmonks Hum, not that I know, you can make one yourself. That said, you could also consider using metadata to mark a namespace as pure or not, same on your functions.

👍 3
pmonks02:11:28

Yeah that doesn’t let me differentiate at-a-glance in the filesystem (my main intent here is to communicate to humans, not to the computer).

didibus02:11:18

Hum... I guess it depends where the human is looking

didibus02:11:38

But (defn ^:pure foo [] ...) is pretty visible to a human

didibus02:11:16

Same for: (ns ^:pure my-ns)

pmonks05:11:38

Yep those could work, though neither qualify as “at a glance in the filesystem” mechanisms.

didibus08:11:51

Ya, I have no idea what you want this for, so you know best. Just saying it's an option as well.

👍 3
st3fan01:11:34

Hmm is there no if-let that takes multiple bindings?

alexmiller02:11:09

you can destructure though and let multiple values out of that (while if-ing on the collection)

alexmiller02:11:34

(if-let [{:keys [foo bar]} some-maybe-nil-map] ... )

alexmiller02:11:04

the if is based on whether some-maybe-nil-map is nil

st3fan02:11:02

My code would be something like when-let [foo (get-foo) bar (get-bar foo)] …

hiredman02:11:11

You may just want some->

xlfe04:11:57

@st3fan I've been using https://github.com/ptaoussanis/encore which includes when-let plus other -let constructs for multiple bindings

xlfe05:11:22

👋 hi all. Just wanted to introduce myself. I'm the co-founder of a startup in Canberra Australia that's building enterprise software on a clojure/script stack. key parts of our stack include datomic, keechma-next, sente and shadow-cljs. We're just starting out but over time I'd like to give back to the clojure community in various ways.

❤️ 6
👋 15
pithyless08:11:13

@st3fan https://github.com/Engelberg/better-cond exports a version of if-let and when-let that checks multiple bindings; I don't use them myself, but I do use the better/cond macro that is useful when writing code that starts leaning right towards multiple indent levels.

3
st3fan12:11:49

Thank you I’ll take a look at that one

Daniel Östling11:11:05

Is there an idiomatic way to merge vector of maps so that some values for some keys are aggregated, but others are not? Something like

[{:a "1", :b :some-keyword, :c "x"} {:a "1", :b :some-keyword, :c "y"}]

would be transformed into

{:a "1", :b :some-keyword, :c ["x", "y"]}
I’ve been playing around a bit with merge-with, but I can’t get it quite right.

Ben Sless11:11:53

I wrote this for a colleague some weeks ago, you might find it useful https://gist.github.com/bsless/e76a6537907ad4eecd69dcb321318b95

kongeor11:11:07

it's not that pretty but it works:

kongeor11:11:09

(def data [{:a "1", :b :some-keyword, :c "x"} {:a "1", :b :some-keyword, :c "y"} {:c "z"}])

(apply merge-with (fn [m v]
                    (if (= m v)
                      m
                      (if (vector? m)
                        (conj m v)
                        [m v]))) data)

kongeor11:11:49

check the source code, it should be relatively simple to follow the code: https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L3051

Daniel Östling11:11:36

Thanks, I appreciate it 🙂

kongeor11:11:44

cheers 🙂

Daniel Östling11:11:46

And thanks @UK0810AQ2, that also looks useful 🙂

kongeor11:11:30

sorry, there is a bug:

kongeor11:11:32

(merge-with (fn [m v]
              (if (vector? m)
                (conj m v)
                [m v])) {:a 1 :b 5} {:a 1 :b 3})

kongeor11:11:41

this will produce => {:a [1 1], :b [5 3]

kongeor11:11:45

which I think it's what you want

Ben Sless11:11:56

my foldmaps might even be overkill for your task, because you have only one "index". You can ofc just rip the indexing part out of it

Ben Sless11:11:27

you can just (reduce (map-combiner {,,}) ms)

Daniel Östling12:11:08

Yeah, I’ll take a closer look. I like to understand code I use 🙂

Daniel Östling12:11:49

Thanks guys 🙂

🙂 3
Ben Sless12:11:40

godspeed 🙂

oly12:11:39

anyone used googles artifact repository with clojure jars managed to upload but cant convince deps to checkout the jar

Jim Newton13:11:31

help calling java method. I'm looking at the source code for isa? and I see the line:

(and (class? parent) (class? child)
            (. ^Class parent isAssignableFrom child))
How can I call the isAssignableFrom method from the repl? Do I need to qualify it with some java.this.and.that/isAssignableFrom, or do I need to require something into my repl? Here's the error I'm getting.
(. Number isAssignableFrom Long)
Syntax error (IllegalArgumentException) compiling . at (clojure-rte:localhost:51477(clj)*:318394:23).
No matching method isAssignableFrom found taking 1 args for class java.lang.Number

pyry13:11:21

(.isAssignableFrom Number Long)

✔️ 3
pyry13:11:42

Sorry, realized that I wasn't actually answering your question, but the above should work nonetheless.

😀 3
st3fan13:11:03

Oh no I need a YAML library

st3fan13:11:15

What is an idiomatic way to translate something like this to Clojure:

if (foo && bar) {
   return;
}

if (something == false) {
   return;
}

if (something_else) {
   return
}

dosomething()

st3fan13:11:46

I’m trying to avoid inverting those conditions and creating a huge indented if pyramid

st3fan13:11:48

factor it out in a function and just use or ?

st3fan13:11:54

inverting expressions makes the decision logic hard to read

alexmiller13:11:09

I mean, this is already weird because it's a void method and those don't exist in Clojure

alexmiller13:11:15

but:

(cond
  (and foo bar) nil
  (false? something) nil
  (something_else) nil
  :else (dosomething))

alexmiller13:11:21

is effectively the same

alexmiller13:11:14

maybe at that point I'd do:

(when-not (or (and foo bar)
              (false? something)
              (something_else))
  (dosomething))

oly14:11:50

from what I can figure out i need to use a wagon in deps.edn but not finding anything online

alexmiller14:11:17

clj does not have support for custom wagons/transporters right now

alexmiller14:11:37

filing a request at https://ask.clojure.org would be a great place to make a votable thing that we can track

alexmiller14:11:29

the underlying tools.deps lib does support custom procurers but does not specifically support custom transporters or wagons, but that is possible to add

oly15:11:44

okay thanks for the info, i was about to ask on reddit 🙂 I will post on ask.clojure as suggested

oly15:11:07

although ask only seems to let you register with github 😞

oly15:11:11

thanks anyway, i will go back to using lein for now

oly15:11:39

perhaps i will post on reddit as well just so there is some history 🙂

alexmiller16:11:06

well posting there will cause nothing to change if that is your goal

alexmiller16:11:31

http://ask.clojure.org is where we collect and determine the priority of requests

oly16:11:14

how come it only has github sign up seems flawed if its the main way to track stuff

oly16:11:45

will have another look perhaps i missed sign up button

alexmiller16:11:08

it is github only right now

alexmiller16:11:44

most devs have a github acct so that has not generally been an impediment so far. if you don't have one, I can file a request for you there

oly16:11:10

that would be very helpful, I am also a bit suspect of the legitimacy of that site as it has various issues or perhaps its just very new

oly16:11:10

not a site I have come across in the clojure eco system previously anyway

oly16:11:48

posting on reddit for posterity anyway as i will probably need the work around again

oly16:11:56

no ask.clojure

alexmiller16:11:00

https://ask.clojure.org is the official clojure Q&A site run by the core team, of which I am a member. I run it.

alexmiller16:11:18

I'm also the primary dev on tools.deps and clj

oly16:11:42

oh I did not knwo that

oly16:11:03

is it supposed to look like that on a desktop ?

oly16:11:44

feels like a mobile version and the x is mis aligned

alexmiller16:11:13

that does look like the mobile layout - but it's a responsive layout so looks different as you widen it

oly16:11:24

I get column if i widen it and a categories box, not try to offend just thought i would point out if there is an issue

oly16:11:05

good work on tools.deps anyway I actually really like it as a build tool for managing my projects

oly14:11:07

{:mvn/repos
 {"artifactregistry" {:url ""}}}
I have added the above to deps.edn but seems it does not understand artifact-registry from what i can work out it needs to launch something to add that protocol format, not finding any details on how to go about that how ever 😞

Gleb Posobin14:11:57

I have read a bunch of stuff about spec, but still don't understand when I am supposed to use it. Should I spec every function definition? What about the performance costs of that? Also, spec suggests that the keywords I use in hash-maps should be namespaced, is that a standard practice? I haven't seen that recommendation anywhere else I think, besides fulcro docs maybe.

andy.fingerhut14:11:16

I believe it is fairly common for those who use spec to use it in production only to check inputs at the boundaries of major subsystems, e.g. for requests from outside of one piece of software, or perhaps sometimes between major subsystems. The run-time cost can be mitigated by being selective. For test time, it can be worth the extra checking time to enable checking at many more places. For testing individual functions or groups of your functions, generative testing, e.g. with test.check can help provide good test coverage at test time.

andy.fingerhut14:11:44

Is this one of the articles you read on spec? It is written by a long time Clojure developer who has used spec in their production Clojure code for several years now.

Gleb Posobin14:11:13

No, I don't think I have read that one, thank you!

Gleb Posobin14:11:27

So I should define some of the specs inside test namespaces then, if I want them to run during test time?

andy.fingerhut14:11:31

I believe some people create completely separate namespaces for spec definition, so that they can be loaded only when desired. By doing that, they can be loaded at test time, true, but they can also be loaded at production time without loading in all of the test code.

Gleb Posobin14:11:05

I see, makes sense, thank you. I'll read the post and ask more questions if I have any.

andy.fingerhut14:11:37

Probably the only reason the author didn't answer your question before I did is because it is very early Pacific time right now 🙂

😄 3
oly14:11:50

seems mixing lein and deps.edn using this plugin https://github.com/lurodrigo/lein-artifact-registry/ and then using lein-tools-deps to read the deps.eddn file seems the dependencies are merged would be nicer to do this all in deps file if its possible ?

alexmiller14:11:33

see my thread above

Louis Kottmann15:11:08

do I get it right that clojure.core.async/go and future have the exact same purpose, except for their respective thread/pool subtelties?

Louis Kottmann15:11:09

and then, considering the error handling overhead that comes with future, I should use go if I don't care about threading boundaries?

alexmiller15:11:52

I'd say they have entirely different purposes actually but if you're not doing any async ops, I'd use future

Louis Kottmann16:11:25

what do you mean, "not doing any aync ops"?

alexmiller16:11:02

parking ops like >! <! etc

Louis Kottmann16:11:11

my actual current project uses futures, but I never dereference them (their purpose is network side-effects)

alexmiller16:11:28

you should not do blocking io operations in go blocks

Louis Kottmann16:11:38

and I'm really torn between keeping them and using core.async

alexmiller16:11:39

as it is a fixed size pool and you can lock it up that way

alexmiller16:11:09

it does not seem like you have a use case that matches core.async afaict

Louis Kottmann16:11:37

and using go blocks with futures inside for the IO parts, would be overkill?

Louis Kottmann16:11:26

I guess I really don't like the errors popping in places totally different from the actual crash place (I use try/catches with every futures), and naively believe that go blocks would somehow solve the problem

alexmiller16:11:06

yeah, it would not help you with that

alexmiller16:11:28

it does not seem like core.async solves any problem you have and likely introduces new ones :)

Louis Kottmann16:11:08

ok let's avoid that pitfall then, thank you!

Roger Amorin Vieira18:11:01

Hi, I dont know if is a bug, but last week, this work like that:

> (format "%.3f" 2.0)
"2.000"
Now the same thing is giving to me:
> (format "%.3f" 2.0)
"2,000"

noisesmith18:11:12

@contato509 that looks like something that would be controlled by the LOCALE environment variable

noisesmith18:11:23

oops - not LOCALE itself, but the locale system, that uses LANG, LANGUAGE, LC_CTYPE, LC_NUMERIC etc. (on linux at least)

Roger Amorin Vieira18:11:52

Was that thanks

🍻 3