Fork me on GitHub
#clojure
<
2021-02-13
>
hiredman00:02:01

want you want to assert is that your input only contains a single valid edn form, the way to do that is to not use slurp and read-string, but instead use a reader and read one form, then try and read again and see if you get eof

didibus00:02:45

Hum, ya thats a good idea

didibus00:02:02

Ok, one more question, how can I mock a reader?

didibus00:02:16

Like can I create a reader over a string?

didibus00:02:32

In my test, I don't want to have to use actual files.

didibus00:02:47

That works!

👍 3
Takis_04:02:25

Hello I try to make a clojurephant gradle project,with both java and clojure clojure has one namespace with gen-class,and i want to use this class from a java file but it says class not found I did

clj -X:new :template clojurephant-clj-app :name myname/myapp

And my project structure is

<project>/
  src/
    main/
      java/
        sample_java/
          Sample.java (i want to use the class myname.japi from here)
    pre/
      clojure/
        myname/
          japi.clj (this is the gen-class)

And i added this in build.gradle after the plugins (saw it from )

sourceSets {
  pre
  main.compileClasspath += pre.output
}

configurations {
  preImplementation.extendsFrom implementation
}                    
The thing that i try to do is possible in clojurephant,and if it is how to do it,thank you

Takis_16:02:54

thank you , i will not use gradle i think, i will use lein , that i know how to do mixed projects

✌️ 3
seancorfield06:02:59

@takis_ I don't know how many people use Gradle with Clojure but you might try sharing that post into the #gradle channel and see if anyone answers.

kwladyka12:02:12

Anyone have experience with Clojure + zigbee / smart home to share with me? 🙂

souenzzo13:02:28

there is a clojure.core function like some but that returns the element rather then (pred element)

kingcode14:02:52

What about (first (filter…))?

p-himik14:02:00

AFAICT, it will be enough in the vast majority of all cases. But the ticket above explains why it may not be the best approach in all situations.

kingcode14:02:52

Sorry, I should have read it first..

borkdude15:02:01

I usually write (when (pred x) x)

p-himik15:02:28

Doesn't work when (false? x) or (nil? x).

borkdude15:02:56

when-some then

p-himik15:02:07

But the latter doesn't work with (first ...) as well. Yeah, that would do it.

borkdude15:02:31

(some #(when-some (pred %) %) ...)

p-himik15:02:46

Wait, I'm wrong - that won't work. And when-some has let-like bindings.

p-himik15:02:28

More like

(:value (some #(when (pred %) {:value %}) values))

borkdude15:02:01

(when (some? ...) ...) my bad

p-himik15:02:03

At this point, it becomes simpler to just write a loop-based function with a clear name. :)

p-himik15:02:20

@U04V15CAJ You still return false - and some ignores it.

borkdude15:02:55

some ignores false... hmm, arguably not consistent

p-himik15:02:45

Well, it's in its docstring: "Returns the first logical true value [...]" false and nil are the only values that are not logical truths.

p-himik15:02:28

user=> (some #(when (false? %) %) [false])
nil

borkdude15:02:14

then what was wrong with:

(some #(when (pred %) %) [false 1])
again?

p-himik15:02:05

If pred is false? then you will get nil whereas you want to get false.

borkdude15:02:30

but some ignores false as well, so?

borkdude15:02:17

you can never get false out of some so I don't see the problem?

p-himik15:02:27

Sorry, I'm not sure what's your point. The initial task - find a way to find an item in collection by a predicate. My thesis - you cannot use some as a top-level form for that because it will return nil for false.

p-himik15:02:59

The initial question was never about some. :)

borkdude15:02:39

The original post: > there is a clojure.core function like some but that returns the element rather then (pred element) Like some. I rest my case now.

p-himik15:02:22

But some does not return the element when it's false. Perhaps just differences in our interpretations.

borkdude15:02:11

some also doesn't return (pred element) in case of false so that is the like part ;)

p-himik15:02:35

Fair enough.

souenzzo22:02:01

filter first will do chunks

(first (filter #(do 
                  (prn %)
                  (odd? %))
               [1 2]))
1
2
=> 1
Maybe we can do something more generic with transducers
(defn transduced-some
  "like some, but uses a transducer. Return the first truty value"
  [xf coll])
;; possible reimplementation of some
(defn some
  [pred coll]
  (transduced-some (keep pred) coll))
(defn some-but-returns-el
  [pred coll]
  (transduced-some (filter pred) coll))

souenzzo23:02:08

(defn transduced-some
  [xf coll]
  (let [rf (xf conj)]
    (loop [[el & coll] coll
           acc []]
      (let [acc (rf acc el)]
        (if-not (empty? acc)
          (first acc)
          (when coll
            (recur coll acc)))))))

souenzzo23:02:45

oh, there is a halt-when

gklijs14:02:41

Anyone know a ‘modern’ way of doing ‘modules’ with clojure? It’s mostly about sharing some common dependencies, and being able to build several uberjars at once. I’m currently using lein-modules but it broke with the leiningen 2.9.3.

borkdude15:02:32

@gklijs why not just make a library that includes other libs and uberjar that lib?

borkdude15:02:43

(if that is what a module is?)

gklijs15:02:43

Yes, it’s just making it easy with common dependencies and running commands for all modules, like ‘lein uberjar’ but is also easy to script. I’m also not sure how ‘common’ leiningen is still with clojure. Could also convert to deps.edn I think..

kingcode15:02:44

I thought frequencies-by (or something similar) was already in clojure.core, as e.g. partition-by and others, but it isn’t. Has anyone else come across a need for it? https://gist.github.com/KingCode/30894e53da19d712021906629cf1583c

dpsutton16:02:27

is there a name for the type of reducing function offers the zero and one arity calls in addition to the standard two arg reduction part? ie, one suitable for use with transduce?

Ed13:02:00

I use "complete reducing function" as implied by this: https://clojuredocs.org/clojure.core/completing ... But I don't think that's all that common

noisesmith16:02:16

all else being equal, "transducing function"?

ghadi16:02:22

I don't say anything more specific than reducing function

dpsutton16:02:07

ok. i was wondering if there was some organic term emerging. seems like a very important distinction

caumond16:02:03

Hi all, I am looking for a library offering basic https://en.m.wikipedia.org/wiki/Configuration_management, I need to develop some mechanisms I believe quite general like: commit, diff, tags to manage version, stageing,... very close to mechanisms available in git. For understanding I am building a product referential with their bill of material. When Im looking for these keywords management and configuration are too general words.... so if you are aware of something close to that, I would be interested.

noisesmith17:02:37

isn't the normal approach here to have a static file with the config data (in eg. JSON or edn) which is checked into git?

noisesmith17:02:59

in a complex app the config could be a library resource

noisesmith17:02:57

or is it that a non-programming end user wants to maintain a config, and you want the version control facilities you are used to over that?

caumond17:02:54

Oh no I am not speaking of the configuration of an app but the concepts present in git. In a product referential that concepts are in the core of the app, the ui shows a product in its latest version and objects it refers two. Each evolution is generally concerning more than one object. When you change one object you need to commit all consequences at the same time (kind of transcation).

noisesmith17:02:22

this is exactly what people us SQL databases for?

noisesmith17:02:56

oh, my apologies, I didn't realize this was a term of art you were using

caumond17:02:51

No pb, as I said these are two too general words ...

caumond17:02:39

It s not that hard, but it is a little bit technical

caumond17:02:59

And as it is a standard concept I could expect some lib

noisesmith17:02:43

this might be a good use case for cognitect's datomic, since its model is transactions that add facts to a graph, and queries on some state of that graph

caumond17:02:48

ok, need to have a look, I saw some references to datomic many times (I understood it was rich's answer to db). I will have a look. It's true that all objects are "linked" together. Need to check if those link could be "resolved" together. The resolution mechanism is the one which, in git, helps you to show all the files together, you have the latest commit for each of same. Classical database don't help to do that.

noisesmith17:02:42

the way datomic works is that you get a handle to a certain transaction representing a state in the db's history, and all queries are done explicitly against that one point in time

noisesmith17:02:34

so the first class thing is which transaction you are looking at (that's something you always do explicitly before querying)

caumond18:02:19

Definitely I need to have a look

jeremie18:02:53

Datomic fits with the requirements as its design can be thought as similar to git. With git we have: pointers (https://git-scm.com/book/en/v2/Git-Internals-Git-References) towards immutable chunk of data (Tree and Blobs). These articles are great to understand git internals: https://maryrosecook.com/blog/post/git-from-the-inside-out, https://git-scm.com/book/en/v2/Git-Internals-Git-Objects, https://medium.com/@pawan_rawal/demystifying-git-internals-a004f0425a70 With Datomic we have Transaction -> Value -> Attribute -> Entity. By the way, https://github.com/tonsky/datascript offers the same Datomic model but in-memory. I believe translating Git design with identifier pointing to Clojure Persistent Data Structure should be quite easy. However, Datomic and Datascript offers a query language (Datalog) that can be quite handy later on. A good intermediary solution should be Datascript data-structure feeded by all the “transactions” on a particular product from the referential.

R.A. Porter18:02:59

You might also want to look at Crux. It's bitemporality might prove useful for working with versioning.

caumond20:02:57

@U01GXCWSRMW, I had a look to crux. I understand it as "event oriented", with small independent event. A part of configuration management is close to events, in my understanding, but all configuration management is about tying all that events together, explaining what version of an object is compatible with what version of another object. an be able to manage transactions on that, holding that bunch of objects together. What I read about crux seems not to help me to do that. do you confirm?

R.A. Porter20:02:30

That might be the case. You’d probably have to build extra scaffolding on top. At most, it could give you, “versions at this time” semantics out of the box.

caumond22:02:05

@U0FTDSQQ3 I read carefully the articles concerning git, the useful part of git for my use case is in the structure of ref commit trees and blobs. It seems reasonable to implement indeed. So maybe a good shot. Datascript and datomic needs both more investment of my time to understand how it can fit the requirements. Thanks for all for your insightful messages

didibus06:02:56

As it sounds pretty industrial, I feel there is most likely a Java framework or library for it. I'd look for that as well.

dpsutton19:02:31

is there a good way to profile memory usage? The only way i know off hand is to use visualvm and watch the graph as i execute a form. Is there a better way?

jumar19:02:48

There are always multiple threads and background processes that can affect memory usage so your form will rarely be the only factor But you could use clj-async-profiler to record allocations

dpsutton19:02:32

awesome. thanks!

dpsutton19:02:48

(and i mean memory usage of a particular form vs a different form)

vemv19:02:06

Can I rewrite a third-party ex-info message in such a way that my custom message will be the first thing that is seen in stracktraces/reports? (e.g. in the repl, test runners) My intent is making an overly concise message more informative, for fast feedback (i.e. prevent having to read buried ex-data) I know about the cause argument, but when using it my custom exception/message will be the second thing being shown, not the first one.

Alex Miller (Clojure team)20:02:55

Can you give a concrete example of what you see and what you want?

Alex Miller (Clojure team)20:02:09

In general, the first line of any triaged error message will be about what happened, in what phase, and where (and root cause message in second line)

Alex Miller (Clojure team)20:02:09

(Talking about repl usage) but repls in tools vary, pst is different, and tests are yet different

vemv04:02:46

Thanks for the response! Here's a repro:

~ $ clj
Clojure 1.10.2
user=> (throw (ex-info "Improved error message" {} (ex-info "Original error message" {})))
Execution error (ExceptionInfo) at user/eval1 (REPL:1).
Original error message
I want to see Improved error message because in my domain, the Original error message that comes from a third-party is overly concise I understand why Original error message is being show first - it's technically its root cause. But perhaps there's some way to hack things here (without losing the original exception either)?

Alex Miller (Clojure team)06:02:03

the error triage stuff will always prefer the root cause so you would need to replace rather than wrap in the throw

vemv08:02:54

👍 thanks.

Carlo19:02:02

what's the best way of writing a spec for a collection that has to start with :and and continue with a number of things for which I have the sub-spec?

borkdude19:02:59

@meditans probably (s/cat :and #{:and} :rest (s/+ :sub-spec))

🙌 3
danielglauser22:02:16

(set "hello") -> #{\e \h \l \o}
(sorted-set "hello") -> #{"hello"}

danielglauser22:02:31

Wasn't expecting that. 🙂

danielglauser22:02:51

You can always do:

(-> "hello" set sort)
So it's not a big deal, just unexpected.

borkdude22:02:36

user=> (apply sorted-set "hello")
#{\e \h \l \o}

3
dpsutton23:02:37

Sorting a set is very different from a sorted set though

dpsutton17:02:49

One is a set that has constant membership check and is a set. The other is just a sequence of the items

jumar08:02:31

And that implies you can easily add the items that are already in the collection:

(conj (-> "hello" set sort) \h)
;;=> (\h \e \h \l \o)