Fork me on GitHub
#beginners
<
2016-07-15
>
krchia01:07:53

what would something like this look like in cljs?

krchia01:07:57

var tree = d3.tree().size([height, width - 160]);

krchia01:07:27

i tried variations of (.tree js/d3), (-> js/d3 .tree .size ..) etc

krchia01:07:57

if anyone could point me to a good resource for js interop i would be grateful as well

krchia01:07:04

would it be (-> js/d3 .-tree etc etc)

krchia01:07:12

i’ll check that link out , thanks

krchia01:07:23

both links very helpful, thanks!

Drew Verlee02:07:33

how would you go from [{:m 1} {:m 2}] to [1 2] and at meta level is there a better way to figure this out then ask in #C053AK3F9 ?

Drew Verlee02:07:30

also how light weight is specter? does it make sense to pull it in for small tasks like that or only when you feel the pain?

seancorfield02:07:39

You have a sequence of two things and you want a sequence of two things as a result — and the way to get from each thing to each result is the same "transformation" — so this would be some application of map.

seancorfield02:07:06

(if you specifically wanted a vector as a result, use mapv)

seancorfield02:07:14

Does that help with the meta question?

Drew Verlee02:07:37

yea, im not sure why i didnt think i couldnt do it with map

seancorfield02:07:47

For something lightweight like that problem, I’d rely on built-in (core) functions.

seancorfield02:07:48

{:m N} -> N is the "function" :m (realizing keywords can be used as functions is a "lightbulb" for a lot of beginners)

Drew Verlee02:07:46

yep! I think its also i sign that i need to get to sleep sense i did that 30 minutes ago for something else 😕

seancorfield02:07:10

Internalizing how to identify common low-level tasks and work with sequences / collections and the (vast array) of core functions just takes time and practice… Even after five years of production Clojure, I’m still "discovering" new core functions and reading and re-reading a lot of the basic stuff about abstractions and so on.

Drew Verlee02:07:30

I wonder if their are non clojure specific ways to train

Drew Verlee02:07:54

* => & & & map!

Drew Verlee02:07:40

sort of strip out the words and parens and just leave the transformations as a visual

Drew Verlee02:07:00

maybe that breaks down at some point...

cezar02:07:38

I need to create a bunch of dates of type #inst because I want to insert them in Datomic. I concocted this way but there must be a better way!

(require '[clj-time.coerce :as c])

    (defn random-instant [limit]
        (c/to-date (c/from-long (long (rand-int limit)))))

seancorfield03:07:39

Sounds like a job for generators in clojure.spec 🙂

naomarik06:07:39

would it make sense for API calls to end with a ! for either query based or an operation that causes an effect on the API end?

naomarik06:07:40

frame of mind from ruby it seems that functions ending with ! would imply some kind of state being mutated outside the context of the function

amashi07:07:29

Personally (and I haven't written that much clojure, but I've written a fair bit of Scheme, which has similar conventions) I wouldn't use bang for that.

amashi07:07:02

On some level you program is going to cause side effects in the outside world (unless you're running it to heat up your room a bit in the winter)

amashi07:07:16

That's something you should be very careful about, actually, so bang seems like too much and too little at the same time.

naomarik07:07:54

the problem as well is that there is some heavy rate limiting going on, so the code will sleep on a thread to not hit those limits

naomarik07:07:04

(on the query aspect)

naomarik07:07:03

also if you're going to do something like send a tweet, you kinda have sent a mutation outside your system, and it wouldn't let you call it again

naomarik07:07:19

i'm not really sure if that is considered unsafe in STM since i don't fully understand it right now

richiardiandrea17:07:15

@naomarik: it is a very tough one to choose, generally I don't mind having ! in functions as general warning that...something is going on within it and I should check the docstring or the source to understand what it is. So in this case I would put it. In this SO thread it cites IO as well https://stackoverflow.com/questions/20606249/when-to-use-exclamation-mark-in-clojure-or-lisp

richiardiandrea17:07:35

at the end of the day it is just a convention so you can make your own I guess

richiardiandrea17:07:25

the STM bit of it is there because STM code is retried over and over until it succeeds (ok not quite, but bear with me)

richiardiandrea17:07:08

so a function like the above, which would do IO (maybe keeping counters) and even block threads, would be considered unsafe in STM

richiardiandrea17:07:59

all this, IMHO of course

Prakash17:07:11

I think traditionally the ! is used to denote that the function is changing state

naomarik17:07:45

@pcbalodi: i would have thought so too, but the clojure style guide is pretty specific about the STM

naomarik17:07:04

which is something i don't understand myself so i have no clue how to create a mental model of when to use it

naomarik17:07:15

changing state is an easy thing to see

seancorfield18:07:03

@naomarik: The STM reference is along these lines: consider an expression that looks like it calls a function once but behind the scenes it might actually call that function two or three times. Would you be able to detect how many times it called that function? If so, the function should have ! to indicate it is "multi-call unsafe" in some way...

seancorfield18:07:39

That said, there’s a balance to be struck between sticking ! on everything that might yield a different result on each call vs just those that modify the system in a "destructive" manner.

seancorfield19:07:07

It’s why, for example, java.jdbc has query (no !) but insert! and update! (and execute!).

seancorfield19:07:30

Technically, query could return different data in subsequent calls, and you don’t really want an STM function to call a DB-accessing function repeatedly, but semantically that choice makes more sense.

seancorfield19:07:57

(so, whilst the style guide is pretty specific about STM, there’s still quite a grey area there)

shaun-mahood19:07:31

In case anyone is looking for a good book to help with learning Clojure past the basics, Clojure Applied is on sale right now and is a fantastic resource IMO. https://twitter.com/ClojureApplied/status/754021391565852673

naomarik19:07:35

@seancorfield: that's very useful thanks! what would you think about a function that has an effect outside the system (like replying to a user via an API), if all it does is simply one call and we don't care about the result?

seancorfield20:07:16

@naomarik: What happens if it is called more than once (with the same arguments)?

seancorfield20:07:27

That would be my determining question.

seancorfield21:07:20

If it’s idempotent — calling it repeatedly with the same arguments has no additional effects after the first such call — then I would say it’s "harmless" to do so and therefore it wouldn’t need !. But in general calling "destructive" APIs that change the world sounds like something that wouldn’t be idempotent and would therefore deserve a !.

krchia22:07:28

anyone used d3 with cljs before?

seancorfield22:07:18

Yes, with Om and Reagent, but it was a long time ago… What’s your question?

seancorfield22:07:14

Best I can point you at, based on my experience, is this: https://github.com/seancorfield/om-sente @krchia

seancorfield22:07:42

I thought I had the rewritten Reagent version up on GitHub as well but apparently not.

krchia22:07:42

i have a problem making a d3 call

krchia22:07:13

i get "#object[TypeError TypeError: Cannot read property 'ctrlKey' of null]" from (.. js/d3.event -ctrlKey)

seancorfield22:07:19

That example uses bare D3 in one example and NVD3 in another example, so take a look at the code and see if that helps you.

krchia22:07:22

a direct translation from the js equivalent "if(d3.event.ctrlKey) return;"

seancorfield22:07:58

you want (.. js/d3 -event -ctrlKey)

seancorfield22:07:50

edited to show .event as field access followed by .ctrlKey as field access

krchia22:07:07

i think both versions are equivalent, but i digress. i get the same error from your line of code as well

krchia22:07:57

i get the error from the latest version of cljsjs

seancorfield22:07:55

Perhaps you can put your code in a Gist or on http://refheap.com and share the link here? Sounds like we need to see more of your code to be able to help you.

seancorfield22:07:11

The error indicates that d3.event is null...

krchia22:07:43

i’ll be happy to, but i’ve just been playing around in the REPL

krchia22:07:57

i can do js/d3 calls to other classes properly, just not event

krchia22:07:09

doing a human js->cljs translation of http://bl.ocks.org/rkirsling/5001347

krchia22:07:38

i’m quite frustrated with this, it would be nice to do something like (dir js/d3)

seancorfield22:07:13

I would assume d3.event is only going to have a value while you are processing an actual (keyboard?) event and so it will be nil otherwise?

seancorfield22:07:00

All the cljs code I’ve seen that tries to reference (.. js/d3 -event …) only does so inside event handlers...

krchia22:07:24

the js code i’m trying to translate to cljs is here http://bl.ocks.org/rkirsling/5001347

krchia22:07:07

i didn’t quite think about that (that d3.event would be nil in the absence of output)

krchia22:07:32

maybe i should just chuck it in my code and see what happens later on, instead of trying to verify that it’s a valid cljs expression in the REPL

seancorfield22:07:09

And in that JS code the reference to d3.event is inside an event handler .on("mousedown", function(d) { if (d3.event.ctrlKey) return; …} )

seancorfield22:07:54

Your REPL experiment verified that it is a field (not a function) and it has a null value:

cljs.user> (.-event js/d3)
nil
cljs.user> (.event js/d3)
#object[TypeError TypeError: d3.event is not a function]

seancorfield22:07:15

So you should not expect to be able to do (.. js/d3 -event -ctrlKey)

seancorfield22:07:24

(i.e., not at the REPL top-level)

krchia22:07:06

sigh.. it should’ve been so obvious

krchia22:07:09

thanks so much 🙂

seancorfield22:07:40

Working with raw D3 in cljs is pretty painful. So much interop.

seancorfield23:07:07

That’s why I looked at NVD3 instead — dramatically reduced the amount of low-level boilerplate.

krchia23:07:08

in what applications is cljs a win over js?

krchia23:07:27

just curious, because im translating js to cljs line by line to gain some proficiency in js interop

seancorfield23:07:46

The usual wins of immutability etc.

seancorfield23:07:37

Translating JS to cljs is going to seem very painful, and probably produce much more bloated code.

seancorfield23:07:02

Comparing an app built with cljs / Reagent to one built in JS / React is where you’ll see the big wins.

krchia23:07:52

hmm, i think i’ll be better off for it if i can stick with this

krchia23:07:11

i’m going to try and be stubborn with this for awhile, and try another approach if nothing’s sticking

seancorfield23:07:01

Well, if you can subdue D3, that’s probably the worst interop mess you’ll have to deal with… so the rest should be a piece of cake! 🙂