Fork me on GitHub
#clojurescript
<
2016-02-08
>
taylor.sando01:02:12

Is there any global way to change how functions are printed with pr-str? The default way of printing out the javascript implementation isn't really helpful for my needs. From looking at the source, you could potentially override IPrintWithWriter, but there isn't really a specific function type that you can extend for just functions. There does seem to be the ability to give pr-str an option map where you can change the implementation, unfortunately, in my case, there is an external library that is making the pr-str call that I can't control.

mfikes02:02:50

@taylor.sando: There was a recent discussion with the conclusion “no; it would be a low priority to add that” but at the same time a note was made that extending IPrintWithWriter to js/Function seemed to work (even though you are not supposed to do that)

mfikes02:02:14

(extend-protocol IPrintWithWriter
 js/Function
 (-pr-writer [f w _] (-write w (.-name f))))

taylor.sando02:02:32

That works. If you just call it function, instead of js/Function it won't throw this warning: WARNING: Extending an existing JavaScript type - use a different symbol name instead of js/Function e.g function

mfikes02:02:07

Ahh. Interesting. Very cool.

samueldev02:02:00

anyone use cljs-time much? trying to parse a date to 5:55PMformat

samueldev02:02:35

which according to my understanding of the docs is done thusly: (f/parse (f/formatter "HH:mmA") (t/now))

samueldev02:02:10

But parse seems to expect the 2nd parameter (the date object) to be seqable and it's not in every different format I've tried

samueldev02:02:19

rather, parse*

mfikes02:02:08

@taylor.sando: For me, in bootstrapped ClojureScript, I'm finding that extending to js/Function works, but not to function.

taylor.sando02:02:04

Ya I just tried it again. It was because I was using boot's reload. It was still using the js/Function instance

mfikes02:02:08

@samueldev: Doesn't it parse a string (which can have seq applied to produce a seq of characters) rather than operate on a date object?

samueldev02:02:43

@mfikes: that's tha path I went down but then if I simple wrap the (time/now) call in (str), no dice

samueldev02:02:01

Unless there's some library fn to convert it to a string in a very specific way which I'm unaware of

mfikes02:02:49

(f/parse (f/formatter "HH:mmA") "12:32:00")

mfikes02:02:20

@samueldev: that's my guess ^ but I haven't used it

mfikes02:02:05

Or something along those lines—dunno what A means

mfikes02:02:46

(f/parse (f/formatter "HH:mmA") "12:32PM")

mfikes02:02:43

I'm not at a REPL, so just speculating :/

nberger02:02:18

@samueldev: you want f/unparse to get a formatted string from a date

datran02:02:48

I'm trying out cljs-ajax for the first time, and when I post and check the network tab on firefox I see this: ["^ ",":email","test.c",":password","cool"] - how do I force it to post in a clojure-friendly map so my backend can talk to the client?

datran02:02:54

whoops, they aren't crossed out, the keys are prefaced by tildas

samueldev02:02:42

@nberger: duh. thank you.

mfikes02:02:52

@datran: looks like you want to specify :format if you want something other than Transit

mfikes02:02:10

@datran: if your server is in Clojure, then Transit is probably the best encoding

datran02:02:37

I'm recieving it with a compojure-api setup, which I'm new to as well

datran02:02:56

does that speak transit natively, or is there a switch I should set?

mfikes02:02:13

@datran: don't know. There appear to be good docs here, recommending ring-transit. Probably just have to read up on it if someone here isn't experienced in it. https://github.com/yogthos/cljs-ajax/blob/master/docs/server.md

datran02:02:48

@mfikes Thanks! I'll explore a bit. I hadn't ever looked at transit before, so this should help.

mfikes02:02:06

@datran: Transit's awesome—especially with respect to perf in my experience :)

juhoteperi06:02:58

@datran: Compojure-api has built-in transit support

danielgrosse06:02:15

I read a tweet in the last days, about better support of clojurescript in google devtools, but can't find it anymore. Did some of you has a hint, what tool it could be?

meow07:02:21

@danielgrosse: perhaps #C0J01U7MY

meow08:02:36

You're welcome.

grav09:02:40

We’re using cljs-ajax for communicating with our transit-emitting backend. The backend is able to send out tagged literals such as ratios, but I’m having trouble understanding how to parse these on the frontend. @jaen wrote something about implementing a transit reader, but I’ve lost the concrete hints from that conversation

grav09:02:18

Any hints? And any opinions on our current http client?

grav09:02:55

@jaen: thanks! And these readers/writers are totally decoupled from which http client I am using, right?

jaen09:02:59

@grav: and then you can use it with cljs-ajax somewhat like here - https://gist.github.com/jaen/7414aebdebf43af8f80a - just be sure to verify with the docs, this code has about a year.

jaen09:02:08

They are decoupled

jaen09:02:25

The ones I linked to are used with sente for example.

grav09:02:35

@jaen: ah, great. thanks a bunch!

jaen09:02:52

@grav: if something doesn't work you could check read-handler and write-handler docstrings, but they are fairly mysterious and need understanding the relevant spec parts.

jaen09:02:03

I actually think those handlers are subtly wrong now I look at them.

jaen09:02:18

@grav: my current handlers look like this - https://gist.github.com/jaen/471319bd8c4c4bc739ce; as far as I understand the first arg to write-handler is a function that returns a tag, second function is a function that returns a representation that can be any of basic transit types (like number, vectors or such) and the third function returns a string representation; read-handler is one function that gets a string representation.

jaen09:02:39

The issue I was having I think, was that JSON transit worked with my previous handlers definition no problem, but msgpack transit broke - it didn't know how to serialise a Date object (JSON transit probably just either stringified or was using the other definition).

jaen09:02:48

Yeah, I think that might be the case - that is msgpack using repr function (second arg) and JSON using stringRepr function (third arg).

jaen09:02:03

I kind of wished it was easier to understand without trawling through the docs.

grav10:02:28

@jaen: Very cool! I’ll try to implement something today

pupeno11:02:05

I’m running ClojureScript 1.7.228 and system-time seems to not be defined. Does anybody know why?

pupeno11:02:08

cljs.user=> (system-time)
#object[TypeError TypeError: Cannot read property 'call' of undefined]
TypeError: Cannot read property 'call' of undefined
    at eval (eval at figwheel$client$utils$eval_helper (), <anonymous>:1:110)
    at eval (eval at figwheel$client$utils$eval_helper (), <anonymous>:9:3)
    at eval (eval at figwheel$client$utils$eval_helper (), <anonymous>:14:4)
    at figwheel$client$utils$eval_helper ()

delaguardo11:02:21

and you are sure that this variable must be defined?

mfikes12:02:36

@viebel: a minimal set of exact steps and small file descriptions without downstream tooling. https://github.com/clojure/clojurescript/wiki/Reporting-Issues

mfikes12:02:27

@viebel: in this ticket a patch is attached and I'm curious if it fixes it for you. It might be related http://dev.clojure.org/jira/browse/CLJS-1541

solicode12:02:30

@pupeno: I’m also on 1.7.228, and (system-time) is working fine here. I wonder if this has something to do with Figwheel? Just a guess.

solicode12:02:00

Though even with Figwheel, it’s working for me… so now I’m not so sure. I’m on 0.5.0-6 if that matters.

Yehonathan Sharvit12:02:22

@mfikes the patch didn't fix the issue

Yehonathan Sharvit12:02:58

@mfikes: what do you mean by "without downstream tooling"?

darwin12:02:57

@taylor.sando: there could be a way, redefine pr-opts function in cljs.core, use :alt-impl to wrap default printing implementation and then post-process it in your wrapper

darwin13:02:43

I do this in cljs-devtools, I provide my own implementation, which calls original implementation with own writer and then inspects its content and does some post-processing on it: https://github.com/binaryage/cljs-devtools/blob/master/src/devtools/format.cljs#L239 in my case I needed to revert stringification of javascript objects as “#object …” inside default implementation

borkdude13:02:48

@joelkuiper: I have used optimus for minifying and cache busting

pesterhazy14:02:14

What's a good (the official?) way to check for a js var that may or may not exist?

pesterhazy14:02:31

1. (try js/bla (except ..))

pesterhazy14:02:51

2. (when (exists? js/bla) js/bla)

pesterhazy14:02:35

3. (aget js/global "bla")

nberger14:02:44

I'd say it's number 2

solicode14:02:43

Yeah, I’d say 2 as well. It’s also used in the ClojureScript source code quite a bit

grav14:02:10

@jaen: this is what I ended up with:

(def reader
  (transit/reader :json
                  {:handlers {"ratio" (fn [[nom denom]]
                                        (/ (-> nom
                                               (.-rep)
                                               (js/parseInt))
                                           (-> denom
                                               (.-rep)
                                               (js/parseInt))))}}))

grav14:02:37

when I do an ajax request, I then use it the following way:

(ajax/GET 
{:handler ...
 :response-format (ajax/transit-response-format 
    {:raw false
     :reader reader})

pesterhazy14:02:33

@nberger: @solicode, thanks will go that route

jaen14:02:54

Hm, yeah, I think you can supply a whole reader as well, though I prefer to just supply the handlers.

jaen14:02:06

One question though - how do you serialise that ratio?

grav14:02:26

It’s done somewhere in our server stack, I don’t know how.

grav14:02:49

We’re using cheshire, might be that one?

jaen14:02:16

Hmm, you don't use transit on the backend then?

grav14:02:21

Yes, we do

jaen14:02:48

So I would have imagined you should have a handler for that in there.

jaen14:02:05

Actually I think transit should have been able to handle ratios without additional handlers.

grav14:02:13

Yes, I think you’re right on the last part

jaen14:02:39

So - great it works, but I would investigate why that's not the case later.

grav14:02:55

Btw - hwo do I find the “key” for a tagged literal? Eg ratios are “ratio”, and it seems BigDecimals are “f"

grav14:02:57

which seems random

grav14:02:47

I’m not sure I understand what you mean by “that’s not the case later”?

grav14:02:31

ah great simple_smile

jaen14:02:35

"random" as in, whatever the implementer thought was a good idea at the time.

grav14:02:47

couldn’t find that chart

jaen14:02:13

Hmm, it seems there's no ratio there

jaen14:02:27

So you should have some handler in your serialisation code; I'd look for that.

jaen14:02:55

> I’m not sure I understand what you mean by “that’s not the case later”? Maybe I suck at sentence structuring, what I mean is that you can look at it at a later time, since it's working.

jaen14:02:30

Ah, it's an additional handler in transit-clj

jaen14:02:35

So I guess what you did is enough.

dnolen14:02:24

@pupeno: what JS environment are you targeting?

darwin15:02:03

@taylor.sando: looks good to me, instead of cljs.core/pr-writer-impl you could use :fallback-impl in opts, in case this was chained (very unlikely)

octahedrion15:02:36

where is the documentation for Clojurescript's this-as macro ?

darwin15:02:39

a friendly recommendation: don’t be afraid to search and read cljs core, it is not that scary as it sounds simple_smile

octahedrion15:02:02

shouldn't have to - important features like that should be documented

octahedrion15:02:14

@darwin - searching the source code doesn't count as reading documentation, sorry

darwin15:02:00

sure, I look forward to the day when we will have full documentation coverage

octahedrion15:02:44

the two links I posted should both have this-as clearly documented, as this feature is crucial for Clojurescript interop

octahedrion15:02:25

in fact, Clojure's documentation is getting to be quite poor. In order to attract more people, documentation and examples are critical

grav15:02:10

@jaen: yes, whole stack is working - I got bigdecimal <-> js/bignumber working as well

grav15:02:25

thanks a lot for the assistance and clarifications simple_smile

darwin15:02:27

I’m not the right person to really defend Clojure’s documentation here, I did exactly zero contribution to it, but In general I agree that docs should be better, but it is not fair to blame random site on the internet to have poor cljs documentation (http://cljs.info is not officially affiliated AFAIK) simple_smile

octahedrion15:02:46

@darwin: errr, that's not a "random site", it's linked to from http://clojure.org/api/api

octahedrion15:02:06

therefor is officially affiliated

darwin15:02:22

@octo221: ok, didn’t know about it

dnolen15:02:51

@octo221: there’s an existing ticket to at least generate autodoc a la Clojure - anybody can take this one - definitely low hanging fruit and relatively newbie friendly

dnolen15:02:19

as far as high level documentation, the wiki is definitive and lot of effort has gone into it

octahedrion15:02:28

@dnolen: are you suggesting I go generate the documentation myself ?

octahedrion15:02:59

because I was searching for it for a friend who's new to Clojure

dnolen15:02:10

autodoc just needs small tweaks to be able to handle ClojureScript source - then we can setup automation and publish it to a sensible location

octahedrion15:02:18

it really annoyed me that I could send him a link

dnolen15:02:32

I’m sure @alexmiller would help out if the the autodoc bit was sorted out

octahedrion15:02:42

@dnolen: well, exactly, this is LISP - there's no excuse for lack of documentation when we can generate it from the source

octahedrion15:02:11

but in general we need more example code, to welcome newbies to the language

octahedrion15:02:04

not just for newbies, for everyone, as we forget

dnolen15:02:37

@octo221: sure the important part here is “we” simple_smile

dnolen15:02:51

as far as newbie friendly intros, the community has done a lot here

dnolen15:02:25

what’s missing is just base level API documentation, somebody can chip in a medium sized task and have a big impact

dnolen15:02:21

I’m sure there would be an avalanche of docstring improvements as well once we got that going

darwin15:02:48

I was under impression that @shaunlebron is working on it here: https://github.com/cljsinfo/cljs-api-docs

octahedrion16:02:01

@darwin @dnolen you know what would be really cool, is if ALL Clojure functions & macros included a simplest-possible example of usage, in their source -- like :example '(this-as this ...

dnolen16:02:09

@octo221: that specific request is unlikely to happen in the docstrings themselves, at least not without further discussion, and there are already existing community efforts that provide stuff like that and more

octahedrion16:02:01

@dnolen: I mean in the function's meta map

dnolen16:02:18

@octo221: yes I know what you meant

dnolen16:02:14

ClojureScript just follows Clojure conventions here, if people want to see some different thing adopted then start a discussion - but note that’s usually a involved process with lots of consideration

dnolen16:02:27

this is why community lead initiatives are valuable

futuro16:02:35

Does anyone know how to profile the cljs compile time? My whole project takes 17 seconds to compile, and when I specify :recompile-dependents false it drops to 0.2seconds

futuro16:02:34

so I'm trying to figure out if I can change my ns dependencies to allow recompiling dependents, but not take more than ~5 seconds to compile, since I'm using figwheel for hotloading

peeja17:02:58

Where would I go to see how a symbol in ClojureScript (like foo-bar.baz/quz) maps to a JS name (like foo_bar$baz$qux)?

peeja17:02:21

I'm attempting to build the same logic elsewhere and I want to make sure it matches

richiardiandrea17:02:31

hello guys, when Figwheel displays "Page not found" what usually can be the problem?

richiardiandrea17:02:40

the log is empty here...

thheller17:02:39

@peeja foo-bar.baz/quz would be foo_bar.baz.quz, the other name is just the local name and pretty much irrelevant

peeja17:02:10

@thheller: I'm hoping to use it as a CSS class. foo_bar$baz$qux is the only version I can get off a function object.

thheller17:02:30

never use any names like that

thheller17:02:54

they will be renamed in :advanced compilation

peeja17:02:07

Oh! That's good to know (and I should have guessed). simple_smile

peeja17:02:27

Right, of course they will.