Fork me on GitHub
#beginners
<
2018-08-30
>
rmprescott05:08:43

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)

dpsutton05:08:43

I think Alex added something like that recently

johnj05:08:44

there's swap-vals!

👍 4
rmprescott05:08:50

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)

rmprescott05:08:20

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.

dpsutton05:08:11

What is not working for you?

rmprescott05:08:30

works fine. This is a basic question of style / best practice. "wax-on / wax-off"

henrik06:08:56

When under duress, I’ve sometimes made a stateful function using a loop inside a go block.

henrik06:08:49

Doesn’t make sense if you’re not already working with channels in the app, of course.

deliciousowl09:08:46

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?)

rmprescott05:08:45
replied to a thread:there's swap-vals!

@since 1.9

😞 4
deliciousowl09:08:03

How much does clojure knowledge translate to lisp?

lispyclouds10:08:35

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.

quadron11:08:43

@coinedtalk lisps are by design very small at their core. a handful of primitives + IO stuff. implementing one will give you an idea:

quadron11:08:14

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.

bmcferren13:08:21

has any one had success finding a "cursive-like" clojure debugger for vs code? something that could launch my ring web server application?

admay13:08:41

@mcferren Any reason why you can’t use cursive?

admay13:08:26

VS Code doesn’t get much attention from the Clojure dev community. It’s mostly Emacs, Vim, Cursive, and Sublime/Atom

admay13:08:31

@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

rmprescott14:08:30

No, not really. I can translate any similar simple example you think is better. DM?

admay14:08:54

So what’s the purpose of that function you’re trying to write?

rmprescott23:08:17

@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.

rmprescott23:08:52

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.

dpsutton13:08:56

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

bmcferren13:08:26

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

bmcferren13:08:48

thanks @dpsutton - I will checkout Calva

dpsutton13:08:20

I don't know if the debugger from CIDER is used in it but the maintainer is quite active in #editors and #calva-dev

dpsutton13:08:19

also, colin -- the developer of cursive-- is quite responsive and would probably love bug reports about those issues.

admay14:08:19

@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

bmcferren14:08:32

maybe i have an older version of cursive installed - i will try to update

deliciousowl14:08:27

hey everyone, quick question: if I have (def equals_zero (- 1 1)) and want to get '(- 1 1) out, what do I do?

danm14:08:59

Er, when? At run time?

deliciousowl14:08:45

I'd like to replace (+ 1 1) with (- 1 1) and then eval it

deliciousowl14:08:53

or other way around w/e

mfikes14:08:05

Like this?

user=> (def foo '(- 1 1))
#'user/foo
user=> (eval (cons '+ (rest foo)))
2

deliciousowl14:08:17

yeah, but def foo doesn't have ' in it

danm14:08:33

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

danm14:08:02

You could probably do some horrible stuff with macros, but I think the question here is why. What's this trying to solve?

deliciousowl14:08:51

just want to show someone how you can manipulate functions in clojure

deliciousowl14:08:12

curiosity, doesn't solve anything unless you know any practical applications of this

danm14:08:58

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

manutter5114:08:16

Maybe what you’re wanting to demonstrate is how macros work?

manutter5114:08:50

The thing about functions is that once you evaluate them, they’re replaced by the result of evaluating

4
mfikes14:08:56

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.

danm14:08:11

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 😉

john15:08:17

@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

gorjusborg15:08:01

are leiningen questions out of scope for this channel?

danm15:08:58

Well there is a #leiningen channel. Did you not get a response there?

gorjusborg16:08:11

thanks @carr0t didn't know about those chans

gorjusborg16:08:17

will try them first

rcustodio17:08:55

Last update was 2 years ago, not sure if that is good, or is it?

hiredman17:08:14

are you familiar with java.util.concurrent.Executors?

rcustodio17:08:15

Yeah, a little yeah

rcustodio17:08:22

Not that much though

rcustodio17:08:36

It uses that, doesnt?

hiredman17:08:20

I've never used claypoole, I use Executors lots

rcustodio17:08:28

Directly you mean

hiredman17:08:10

they come with the jvm and don't require any libraries to use, and are pretty good

rcustodio17:08:54

I see, thanks, gonna check on that

rcustodio17:08:51

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

rcustodio17:08:03

gonna check on that, thanks

rcustodio17:08:59

even if im executing something, i use .submit, right?

hiredman17:08:32

if you are using rabbitmq, I believe there is a setting somewhere for how many messages a connection will claim without acking

rcustodio17:08:31

I use langohr, not sure where to do that, there’s prefetch, and using a fixed threadpool

rcustodio17:08:50

found it, he uses this

(defn execute*
  ([f]
   (execute* pools/main-pool f))
  ([p f]
   (.execute ^ExecutorService p f)))

justinlee18:08:36

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

justinlee18:08:40

oh nevermind. i just discovered fnil.

✔️ 12
danny20:08:40

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?

tbsvttr21:08:23

Is LightTable still a thing? I found the concept very intersting. But I do not see that it is still talked about.

sbauer21:08:42

Development of light table is largely dead due to inactivity

sbauer21:08:36

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

rcustodio22:08:54

I know there`s always a better way, what would you guys say about this thread

rcustodio22:08:05

This, I mean

(defn execute [^ExecutorService pool func & args]
  (.execute pool (fn [] (apply func args))))

mg22:08:22

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))))

noisesmith22:08:05

small point - fn already has an implicit do

rcustodio22:08:50

so @noisesmith

(defmacro with-pool [pool & body]
  `(.execute ^ExecutorService ~pool
    (fn [] (~@body))))
like this?

rcustodio22:08:03

Or without the ()?

rcustodio22:08:21

I dont use macro much, I should use it more often

jaihindhreddy-duplicate05:08:55

first rule of macro club 😉

rcustodio14:08:58

Sorry, you mean we should not use it much?

jaihindhreddy-duplicate14:08:56

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.

rcustodio14:08:31

I see, thanks for the tip

noisesmith22:08:45

if you use macroexpand it's easier - one version turns (with-pool p (+ 1 1)) into ... (fn [] ((+ 1 1)))... which is obviously wrong