This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-18
Channels
- # announcements (4)
- # babashka (2)
- # beginners (72)
- # calva (2)
- # cider (18)
- # clj-kondo (30)
- # cljs-dev (2)
- # clojure (106)
- # clojure-austin (2)
- # clojure-europe (17)
- # clojure-italy (6)
- # clojure-nl (4)
- # clojure-uk (109)
- # clojurescript (31)
- # cursive (6)
- # data-science (2)
- # datomic (30)
- # events (1)
- # fulcro (20)
- # graphql (4)
- # jobs (2)
- # joker (8)
- # kaocha (2)
- # meander (31)
- # off-topic (1)
- # pathom (53)
- # re-frame (22)
- # reitit (1)
- # shadow-cljs (26)
- # specter (2)
- # sql (20)
- # testing (2)
- # tools-deps (2)
- # tree-sitter (1)
- # xtdb (20)
- # yada (6)
@mac (
io/file also accepts "/path/to/file.suffix"
@noisesmith Excellent, thank you.
Or a performant way of building up a result [b]
given a a -> [b]
function and a sequence of [a]
as input, not caring about intermediate results
(let [f (fn [n] (repeat n n))]
(loop [acc (transient [])
xs [1 2 3]]
(if (seq xs)
(recur
(concat! ;; <= doesn't exist
acc (f (first xs)))
(rest xs))
(persistent! acc))))
or just (into [] (comp (map f) cat) [1 2 3])
- it uses transients for you under the hood
I guess (mapcat f)
is better, but (comp (map f) cat)
should perform identically
user=> (into [] (mapcat #(repeat % %)) [1 2 3])
[1 2 2 3 3 3]
a simple perf comparison:
user=> (time (dotimes [_ 10000] (into [] (mapcat #(repeat % %)) (range 1000))))
"Elapsed time: 55650.88141 msecs"
vs.
user=> (time (dotimes [_ 10000]
(let [f (fn [n] (repeat n n))]
(loop [acc (transient [])
xs (range 1000)]
(if (seq xs)
(recur
(reduce conj!
acc (f (first xs)))
(rest xs))
(persistent! acc))))))
"Elapsed time: 54154.708998 msecs"
so for a 3% difference in perf at first measurement (that might be smaller than the margin of error(?)) you get a very big difference in code complexity
Yeah, for this case I'd definitely use a mapcat, but in my real example I had additional state that needed to be threaded through the functions
In particular I need to be notified about new files and want to read them as soon as they are copied completely
hi! Does anybody knows a function to efficiently calculate nested clojure data structures differences? Given some state and and a (fn [state]) => state' efficiently figure out what paths changed from state to state'
thanks @mike1452, will take a look!
So we use RxJS on our frontend. And today this came up https://rxjs-dev.firebaseapp.com/api/operators/distinctUntilChanged Anyone knows if there are transducer implementations of this?
Is there a library that can report syntax and other errors in clojure files? Preferably with line-numbers and so-on, like shadow-cljs would do.
There is also #eastwood http://github.com/jonase/eastwood
But they cannot give feedback on executed code failing, right?
Bit more context: I want to create a make-like tool, where the DAG is described in clojure data-structures and perhaps helper functions in clojure. For this, I can just slurp a Make(-like) file written in clojure, read it and eval it, however, then any errors produced by the program will be cryptic.
ah, maybe you're looking for https://github.com/clojure/tools.reader#differences-from-lispreaderjava
clj-kondo can lint invalid code (e.g. {:a 1 :a 2}
). if you are looking for a clojure interpreter:
https://github.com/borkdude/sci/
you can have control over what functions are and aren't allowed in your clojure DSL while also getting good error messages about unexpected symbols etc
the interpreter is compatible with GraalVM so you can make an executable out of it with amazing startup.
the list of projects in which it is used should give you some idea of what's possible
@borkdude Thanks, will keep that in mind. I actually want to use a long-running server for my current program, but it is good to know 🙂
Cool, I see that it does more than I thought. So sci is meant for use within long-running programs?
You can use it in long running programs. E.g. babashka uses sci and babashka has a socket REPL which you can leave running for a few days I guess 🙂
Note that interpretation with sci is likely slower than clojure.core/eval so if you're looking for performance with regards to hot loops, etc. then clojure.core/eval is faster
sci keeps everything isolated. clojure.core/eval also works but it can pollute your clojure RT (created namespaces, vars, etc.)
if you're looking for a clojure parser for the DAG tools.reader might just work. there's also https://github.com/borkdude/edamame which is configurable: you can allow optional code like features on top of EDN, limit it the way you like.
Okay, I will need to try it out. Performance in clojure might be a problem: I want users to be able to write custom clojure code for processing data. Would it be able to use metadata to annotate functions that should be interpreted with eval and for sci to do that?
this is basically the API of sci:
(sci/eval-string "(plus 1 2 3)" {:namespaces {'user {'plus +}}}) ;;=> 6
you can use this in any configuration you wantThanks.
dad is an example of a Make-like tool that's made with sci: https://github.com/liquidz/dad
spire is a provisioning tool (like Ansible) also made with sci: https://github.com/epiccastle/spire
Thanks, I will read those for inspiration. I am still a clojure-newb 🙂
I see you looking for a parser that can report syntax errors with location information: edamame also does this.
No, it is good.
Can I use arbitrary clojure-libraries from sci?
Is it okay if I ask more questions?
you will have to bring those libraries in via the :namespaces
option like I did in the example before with the plus
function
I am trying it out now 🙂
Neat! It seems to be exactly what I wanted.
I see that functions are represented as
#object[sci.impl.fns$parse_fn_args_PLUS_body$run_fn__962 0x6f3a2011
But if these are called from the JVM, will they not be optimized like regular java functions with java hotspot?The performance penalty is if you use GraalVM, right?
the difference is more like interpretation vs compilation. clojure compiles expressions to efficient JVM bytecode. the sci interpreter doesn't emit bytecode, but will invoke the interpreter to handle the code
Understood 🙂 :thumbsup:
Like, if sci allowed you to use arbitrary functions by default, would it still be safer than eval in some way?
all side effects that happen in sci are local, so vars and namespaces you define you won't see in your clojure.lang.RT
if that's not important to you, then I guess you might as well just use clojure.core/eval
I will need to play around with both and see what I need 🙂 Thanks for all the help
And either way, I will use edamame 🙂
What benefits do Spire https://github.com/epiccastle/spire have over Ansible ?
@UCMNZLJ93 consider asking this in #spire. the author himself can explain it better to you than I can
Long-running programs should bias you away from any graal native-image tools (though they are cool)
a lot of native-image web tooling (micronaut, etc.) has low memory usage and fast startup, but not the greatest GCs and no runtime optimization
Hi everybody, should I ask about oAuth2 + Ring here or somewhere else? I can not move for two days so it is time to ask 🙂. Thanks
There's #ring but the question should also be fine here if it doesn't end up creating even more questions. In any case, EAFP. :)
Someone asked Paul Graham what programming language to learn 😜 Coincidently, it was searching twitter and seeing him tweet this same response years ago was what motivated me to start learning Clojure a few months back. https://twitter.com/paulg/status/1240218967865778177
Can you remember me why
(s/def ::uuid uuid?)
(gen/sample (s/gen ::uuid))
Unable to construct gen at: [] for: :spec.common/uuid
while there is https://github.com/clojure/spec.alpha/blob/31165fec69ff86129a1ada8b3f50864922dfc88a/src/main/clojure/clojure/spec/gen/alpha.clj#L156(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[clj-uuid :as uuid])
This is because of the uuid
alias in this nshmm now it works even with this ns. I don’t know what is happening, but I am sure I killed REPL and run again a few times to be sure.
What benefits do Spire https://github.com/epiccastle/spire have over Ansible ?