This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-30
Channels
- # announcements (1)
- # beginners (94)
- # calva (12)
- # cider (5)
- # cljdoc (4)
- # cljs-dev (4)
- # clojure (170)
- # clojure-austin (2)
- # clojure-dev (45)
- # clojure-germany (53)
- # clojure-italy (20)
- # clojure-nl (6)
- # clojure-spec (25)
- # clojure-uk (242)
- # clojurescript (161)
- # cursive (16)
- # data-science (3)
- # datomic (20)
- # defnpodcast (6)
- # emacs (40)
- # events (1)
- # fulcro (80)
- # funcool (46)
- # jobs (1)
- # leiningen (23)
- # liberator (2)
- # lumo (43)
- # mount (3)
- # off-topic (21)
- # onyx (1)
- # pedestal (15)
- # re-frame (23)
- # reitit (4)
- # schema (1)
- # sfcljs (1)
- # shadow-cljs (167)
- # spacemacs (1)
- # tools-deps (21)
- # yada (2)
Is there a common idiom/macro to evaluate to the current value of an atom before a swap!
other than the obvious
(let [current @a] (swap! a f) current)
Thanks. That's a lead-in to my real question: Is this an idiomatic way to make a "stateful" function. There is a surprising dearth of succinct answers from googling. Or I'm using the wrong terms.
(defn stepper []
(let [s (atom (cycle [1 2 3]))]
(fn []
(let [n (first @s)] (swap! s rest) n))))
(def f (stepper))
(repeatedly 4 f)
;; (1 2 3 1)
I mean, is there another way to keep state other than using an atom? I can't seem to make this work with a closure over an immutable.
works fine. This is a basic question of style / best practice. "wax-on / wax-off"
When under duress, I’ve sometimes made a stateful function using a loop inside a go
block.
I think atom is idiomatic, judging by the fact that every clojurescript web app uses it for app state (other than a database, I guess?)
How much does clojure knowledge translate to lisp?
According to me, the macro thinking, the homoiconicity, code data equivalence and the REPL workflow. All of these can be equally applied back to any LISP. But Clojure spoiled me with the awesome data structure literals, proper persistent data sturctures and the JVM which I sorely miss in other LISPs.
@coinedtalk lisps are by design very small at their core. a handful of primitives + IO stuff. implementing one will give you an idea:
clojure builds on the philosophy of immutability (which makes history(!) cheap through structure sharing) and simplicity (very few data interfaces: seq, maps, ... and a lot of functions that work across them). This maximizes developer power IMHO.
has any one had success finding a "cursive-like" clojure debugger for vs code? something that could launch my ring web server application?
VS Code doesn’t get much attention from the Clojure dev community. It’s mostly Emacs, Vim, Cursive, and Sublime/Atom
@rmprescott Have you gotten an answer for the question you asked earlier yet? I think I have an idea of the function you’re trying to write but the approach seems to be a bit off
No, not really. I can translate any similar simple example you think is better. DM?
@admay I was working on a CodeWars problem and didn't want to the details away. It was something, essentially, like:
<lots of work parsing an obscure input stream into a stream of numbers>
Feed that stream into a a cycle of math operators. Something like:
1 + 2 - 3 * 4 / 5 + 6 ....
So something like reduce
with a stream of functions and a stream of inputs.
So I wanted to write a function that returned a new function on each call -- i.e. a stateful function. That's a classic closure which updates the variable it closed over. It's that mutating part that's tricky here. So I stuck it in an atom. It seems like this might be a not uncommon problem.
VS Code has gotten a lot of love recently. And I think Calva is reusing a lot of CIDER components so it should be quite feature-packed
thanks @admay. I have been using cursive over the past year. Reasons being (1) intellij is a heavy app and affects the performance of my laptop, (2) cursive seems to pass over the same breakpoint multiple times giving me an inconsistent perspective on the behavior of loops and recursion (3) i get the red x circle many times when trying to add break points to various lines in my code (4) I often get the java.lang.RuntimeException: Unable to resolve symbol: ... in this context, compiling:(NO_SOURCE_PATH:0:0) when trying to evaluate code in my breakpoint windows
I don't know if the debugger from CIDER is used in it but the maintainer is quite active in #editors and #calva-dev
also, colin -- the developer of cursive-- is quite responsive and would probably love bug reports about those issues.
@mcferren Those are all really good reasons not to use Cursive haha That being said, I’d go with @dpsutton’s excellent suggestion in Calva. I will say though, Calva encourages a different kind of workflow. Rather than writing functions and debugging via breakpoints, Calva encourages interactive development via inline evaluation similar to Vim + Fireplace and Emacs + Cider
hey everyone, quick question: if I have (def equals_zero (- 1 1)) and want to get '(- 1 1) out, what do I do?
I'd like to replace (+ 1 1) with
(- 1 1) and then eval it
or other way around w/e
yeah, but def foo doesn't have ' in it
I'm still a bit confused. If you're in the repl, or at run time, then equals_zero
doesn't have the value (- 1 1)
. It has the value 0
, because the form (- 1 1)
has been evaluated and the result assigned to the var equals_zero
You could probably do some horrible stuff with macros, but I think the question here is why. What's this trying to solve?
just want to show someone how you can manipulate functions in clojure
curiosity, doesn't solve anything unless you know any practical applications of this
I don't think you can. If you want to muck about like that before the function is exec'd then you'd expect the original definition to be '(- 1 1)
, which you could then load and alter before execing
Maybe what you’re wanting to demonstrate is how macros work?
The thing about functions is that once you evaluate them, they’re replaced by the result of evaluating
At runtime, in a limited sense, you can “modify” a function by composing it with another. For example you can invert a predicate with complement
.
But yeah, all my function munging in the past has been with macros evaluated at compile-time. And I avoid it unless absolutely necessary, because writing macros hurts my brain 😉
@coinedtalk you want the source of foo
, before it was evaled, right? Not possible, without writing an extra def-with-source
macro of some sort
are leiningen questions out of scope for this channel?
thanks @carr0t didn't know about those chans
will try them first
Is this the best for threadpool? https://github.com/TheClimateCorporation/claypoole
A coworker did one to easier use, but something is off, its not as fast as it should 3 rmq consumers with pool for eachself, but terrible slow and the memory/cpu is not using that much
if you are using rabbitmq, I believe there is a setting somewhere for how many messages a connection will claim without acking
I use langohr, not sure where to do that, there’s prefetch, and using a fixed threadpool
found it, he uses this
(defn execute*
([f]
(execute* pools/main-pool f))
([p f]
(.execute ^ExecutorService p f)))
does anyone know if any of the various helper libraries have a defn
replacement that adds some kind of default argument syntax? the multiple arity technique is pretty tedious
fnil, cool!
midje’s contains
checker seems to get verbose quickly with nested maps. Is there a different checker that i should be using, or am i thinking about my checks in the wrong way?
Is LightTable still a thing? I found the concept very intersting. But I do not see that it is still talked about.
Meaning that there currently is not much happening but that could change with the right people... I know I have very little time to work on it
This, I mean
(defn execute [^ExecutorService pool func & args]
(.execute pool (fn [] (apply func args))))
I might be inclined to do that as a macro so there's more flexibility about what you're executing. Maybe something like:
(defmacro with-pool [pool & body]
`(.execute ^ExecutorService ~pool
(fn [] (do ~@body))))
small point - fn already has an implicit do
so @noisesmith
(defmacro with-pool [pool & body]
`(.execute ^ExecutorService ~pool
(fn [] (~@body))))
like this?first rule of macro club 😉
Yeah, macros > functions > data
in power and expressivity and this also means macros < functions < data
in our ability to reason about them. Also, macros aren't values like functions and data are, making them less composable.
without ()
if you use macroexpand it's easier - one version turns (with-pool p (+ 1 1)) into ... (fn [] ((+ 1 1)))... which is obviously wrong