Fork me on GitHub
#beginners
<
2019-02-02
>
Santiago07:02:24

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?

borkdude12:02:51

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

👍 1
borkdude12:02:15

but calculating it in the view is also possible

skykanin13:02:15

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.

skykanin14:02:02

I guess I could just flatten it

rakyi14:02:41

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

rakyi14:02:25

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

rakyi14:02:52

another option would be to stick apply before the vector

rakyi14:02:25

also consider using loop + recur for an efficient recursion

rakyi14:02:55

this looks reasonable, but the recursive mrg call should be replaced with a recur call https://gist.github.com/baabelfish/6573984

borkdude14:02:07

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

borkdude14:02:46

if not, this also works:

borkdude14:02:56

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

Mitch15:02:50

(sort (concat x y))

dpsutton15:02:31

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

Chase17:02:32

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?

Chase17:02:49

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?

Mitch17:02:34

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

Mitch17:02:54

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

alexmiller17: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

alexmiller17:02:41

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

skykanin17:02:37

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 .

Mitch18:02:15

Can you give me an example that this hangs on?

skykanin18:02:24

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

alexmiller18:02:01

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

alexmiller18:02:15

So it never hits the termination conditions

alexmiller18:02:45

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

Mitch18:02:54

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?

alexmiller18:02:56

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

Mitch18:02:00

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.

Mitch18:02:24

Instead of like an "implicit" :as

Mitch18:02:00

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

skykanin18:02:48

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

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

Chase18:02:34

I literally just read about that! Look on this page for the recursion section about half way down: https://aphyr.com/posts/305-clojure-from-the-ground-up-macros

Mitch18:02:38

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

Chase18:02:04

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.

Mitch18:02:44

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.

Santiago18:02:05

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?

Mitch18:02:24

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

john18:02:49

I think there's a dispatch-n

lilactown19:02:32

They are evaluated fifo

lilactown19:02:07

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

Santiago20:02:47

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

Ajay19:02:33

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

heyarne19:02:30

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

andy.fingerhut23:02:51

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? http://php.net/manual/en/function.base-convert.php

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: https://youtu.be/kVvP5MNIND4

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?

jumpnbrownweasel20:02:44

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.

👍 1
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?

dpsutton21:02:00

Come chat in #cider @gisf

reaysawa23:02:28

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. https://github.com/kirill-grouchnikov/radiance)

reaysawa23:02:05

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

reaysawa23:02:42

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

reaysawa23:02:46

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

seancorfield23:02:50

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

seancorfield23:02:23

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

lilactown23:02:51

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

lilactown23:02:01

haven’t built anything with either of them myself