Fork me on GitHub

in re-frame I'm building a simple timer with a button. On button press I :start-timer and save the current time, then after another press I save the new current time with :stop-timer. Is a fn calculating the duration a view function?


@slack.jcpsantiago In re-frame world you could do that with a subscription

👍 5

but calculating it in the view is also possible


I'm trying to make a function which merges two vectors together recursively, but for some reason I'm getting an weird vector in return and I'm not sure what's happening here.


I guess I could just flatten it


vector builds a vector containing its arguments, which in this case is first item of either x or y and then another vector returned by the recursive call to my-merge


you can use cons instead, but it doesn’t return a vector


another option would be to stick apply before the vector


also consider using loop + recur for an efficient recursion


this looks reasonable, but the recursive mrg call should be replaced with a recur call


@nicholas.jaunsen can the vectors be of different lengths?


if not, this also works:


(into [] (sequence (comp (map vector) cat) [1 2 3] [4 5 6])) ;;=> [1 4 2 5 3 6]


(sort (concat x y))


this is presumably a merge function for merge sort. the vectors will be of differing lengths and calling sort will circumvent the purpose of writing the function in the first place


so when looking at docs it seems ok to use one word parameters, eg. s for string, n for numbers, f for functions. But I also see people do things like pull in spec :as s Does that make it confusing for others or does the context usually make it obvious? How far can I go? c for char? Any other ones I should know? Collections seem to be coll right?


for example, the exercise asked me to create a function finding the frequency of a letter in a string. So I wrote this:

(defn freq-of-letter [c s]
  (count (filter #(= % c) (seq s))))
Will clojure programmers know what I'm saying there or is it too obtuse?


I'm no authority, but the use of "letter" and "frequency" in the function name makes me intuitively realize that we will be working with a seq of characters


so c and s are automatically recognized as what they are meant to be

Alex Miller (Clojure team)17:02:50

Namespace aliases are meaningful only in a small set of places where they can only be aliases, so I never worry about confusion there

Alex Miller (Clojure team)17:02:41

In your code I might use letter or ch instead of c


Ok, I got it working with cons but now I've rewritten it with a loop, but it think it's stuck in an infinite loop. I can't get emacs to evaluate a merge anyways .


Can you give me an example that this hangs on?


(my-merge [1 8 9] [3 4 6])

Alex Miller (Clojure team)18:02:01

The x and y in the loop refer to the outer params, not the loop bindings

Alex Miller (Clojure team)18:02:15

So it never hits the termination conditions

Alex Miller (Clojure team)18:02:45

Instead of [a & as], do [a & as :as xs] then xs inside the loop, same for bs/ys


Since the var x is never rebound to anything else, it will never be empty. So you have to use the :as keyword for destructuring, which will be rebound on every iteration. Is that right?

Alex Miller (Clojure team)18:02:56

If you write the loop head like a let with newlines it’s clearer that only the left hand sides are bound


From a language design perspective, it makes sense to keep x and y bound to their original values because you might want to test against the starting values at the end of the loop or something like that.


Instead of like an "implicit" :as


so you want something like this. Might have gotten the variables mixed up but the idea is to use the :as keyword to refer to the bound var


why is it that loop recur is more performant than just calling the function name recursively?

Alex Miller (Clojure team)20:02:21

Calling the function adds a stack frame, which consumes memory (and stack depth is limited). recur does not - turns into a loop (really a goto at the bytecode level)


I literally just read about that! Look on this page for the recursion section about half way down:


To add to the above, java doesn't have native support for tail call optimization, so the recur macro adds that for our benefit.


I think the tl/dr is that if you use the function name recursively you have to hold every single recursive call in memory until it's all complete. recur tells the compiler you don't need to hold on to it.


Or it is actually a special form. But there is a small benefit to using the "recur" explecitly for TCO, because you will be warned when your recursion point isn't in the tail position.


if I re-frame/dispatch two events at the same time are they evaluated at the same time or fifo? eg (re-frame/dispatch [:event1 :event2]) (or should that be two vectors?


There is a #re-frame channel if you don't get an answer here.


I think there's a dispatch-n


They are evaluated fifo


It would be two calls to dispatch, or use dispatch-n like John said


thanks Mitchell and lilactown , john I'll look at dispatch-n


When using specs - if I want to run all the generative tests, how can I do it easily with Leiningen? Is there something like lein test


are there literals to enter numbers in base 16? if not, what would be the idiomatic way? i'm talking about clojurescript in case that depends on interop


Clojure on the JVM lets you write things like 0xdeadbeef for hex literals in the source code. I just tried that in a ClojureScript node.js REPL, and it worked there, too.

Adrian Smith20:02:45

Is there something that's the equivalent of this function in Clojure?

Lennart Buit21:02:18

(Integer/toBinaryString 12)

Adrian Smith20:02:17

looks like I can use this to go from base 10 to base 2:

Adrian Smith20:02:04

I'm thinking about using iterate to generate the sequence from 32 (for example) back down to 0 (by div 2), there will be a point where I want to stop generating the sequence (at 0) is that the responsibility of the thing moving over the sequence or the iterate function?

Adrian Smith20:02:12

if it is the iterate f how do I signal the end of the sequence?


iterate returns an infinite lazy sequence. You can use take, take-while, etc, to consume a portion of that sequence. For example, take-while could be used to consume everything up to the first zero value.

👍 5
Gustavo Isidio21:02:19

Hello, I started with clojure and spacemacs yesterday and have created an simple project

lein new project_name
. When i try run cider on core.clj a bunch of warnings come out and repl does’nt start

Gustavo Isidio21:02:23

Can anyone help me?


Come chat in #cider @gisf


do you guys know some GUI-using project that isn't "hello world"? I'm trying to figure out how people usually do it I saw a while ago a clojure library for dealing with Swing, but I also would like to use animations (e.g.


and if I'm going to use this library and the swing API's objects directly, I might as well interop with Java directly


but perhaps there is something like this for GUI clojure already and I don't know about it


would appreciate stories of GUI development or example code : - ]


(I have no idea about this area -- I never build Java UIs -- but Seesaw is the library mentioned most when folks ask about Swing)


I thought Swing was sort of deprecated in favor of JavaFX these days?


I think both have pros and cons. AFAICT Swing was more flexible, JavaFX more features out of the box


haven’t built anything with either of them myself