Fork me on GitHub

There's not really much magic to it. If you target Node then goog.require("cljs.nodejscli");ends up in the output, which causes this bit of code to run


Right, I don't really have any issue with it, just stumbling upon differences from Clojure as I go, because I'm so used to it

Garrett Hopper02:07:00

Um... What's going on here?

(reduce + (for [_ (range 0)]

(reduce + (for [_ (range 1)]

(reduce + (for [_ (range 2)]


@ghopper (+ nil) returns nil. While (+) returns 0.


@ghopper In the case of range 0, for in an empty sequence. Thus reduce calls the function with zero arguments, and returns. That means (+), thus it returns 0. In the case of range 1 and over, for is no longer an empty sequence, so reduce calls the function with elements of the sequence, in your case nil. Thus (+ nil), and you get nil.


Thats my guess


Hum, actually, its weird, (+ nil nil) returns 0, but (+ nil) returns nil


Right, so this is a case of garbage in -> garbage out


The + fn is made to return 0 with no argument, and to be an identity with one argument


Thus, with an empty sequence, reduce calls (+) and you get 0


With one nil, you get the identity, so you see nil back


That one is already a case of garbage in -> garbage out. It works only by luck.


After that, (+ nil nil) you get whatever JavaScript gives you. In this case (null + null) in JS returns 0


While in Java, it throws a null pointer exception


Finally, in JS, (0 + null) gives you null back. That's why all extra nils to + will return 0 in CLJS. While in Java, all extra nils throw nul pointer exception


@ghopper And to explain reduce, notice the docstring mentions with an empty coll, it calls f with zero arg. With only one element, it does not call f, and just return the element in coll. With more than one element, it will repeatedly call f with the first and second element, etc.

Garrett Hopper12:07:06

Thanks, @U0K064KQV! So is (reduce + 0 $seq) good enough, or should I filter out nils first to be correct?

Garrett Hopper02:07:24

It only happend with (reduce +) over a LazySeq with a single item in it.

Garrett Hopper02:07:41

Oh... Maybe I'm crazy. It happens with (reduce + [nil]) as well. Maybe I just don't understand reduce.


(where non-cljs console logs stay within the window, and wrap down to the next line)

Garrett Hopper02:07:10

Maybe this just a cljs idiosyncrasy with the way its number function shandle nil. (+ nil) returns nil, but (+ nil nil) returns 0. :man-shrugging:


quite simple really. + with one arg just returns that arg. (+ nil nil) becomes null + null in JS which results in 0. ask the JS authors why, CLJS just inherits this behavior.

Garrett Hopper12:07:03

Thanks, @thheller, I assumed it was some weird JavaScript thing. :)

Garrett Hopper02:07:20

(reduce + 0 $lazy) solved the issue for me. Oh well ¯\(ツ)


@ghopper Whereas clojure throws a NullPointerException for (+ nil nil) and returns nil for (+ nil). Neither clj or cljs behaviour seems quite right. I would have thought the best behaviour would be to either return nil in all cases or throw an error in all cases. Good to know though.


Clojure's (+ anything) will return whatever value is in anything, that's the strange behavior. Also, (+) will return 0. That's the reason for (reduce + []) to return 0


Ops, not really: only ClojureScript's + will return the first argument...


Probably because, in Clojure, there's a type hint for + to only accept numbers, so we can't send anything that's not a number (and it seems that nil is a correct number for Clojure. Maybe it's that Java's "Integer wrap-unwrap")


(+ x) where (number? x) is false is an invalid program and thus has undefined behavior. It can throw, return x, or play a movie with a dancing and singing frog jumping out of a cigar box.

👍 4

@mauricio.szabo It does return the first arg, acting as an identity, but it casts it to a Number, nil can be casted to a Number, so (+ nil) returns nil. In cljs, it just returns it.


That said, reduce f function is never called with one argument. So in the case of reduce, (+ nil) would never be called.


Does it matter if I choose commonjs or amd for :foreign-libs :module-type when working with a UMD packaged library?


Bah, Error: ENOENT: no such file or directory, lstat '/home/hukka/repos/test/https: when I tried to use https-url in :foreign-lib :file


Also, Error: Cannot find module '@cljs-oss/module-deps'


No idea where that's coming from


Hello all, glad I found this Clojure chat. Does anyone have experience with re-frame? Is this the right room for re-frame help?


@aaronmmartz try #re-frame

❤️ 8

Thanks Mike


I'm writing a CLJS library that wants to supply some CSS for use by its clients. What is the best way to do this?


Make it part of the jar (the CSS file) and provide instructions for how to serve it using ring or similar


At least that would be my approach 🙂


Oops, I should have been more specific. This is a pure cljs library. No server.


do you expect users to not use a server in some capacity?


i.e. fully frontend based?


yes. It is a library of re-frame controls, wrapping CSS grid layout.


what I mean is: do you expect people who use your library to have a backend of some kind?


You are right that many users will have some server component, but It will lay well outside the domain of responsibility of this library. It may not even be a Clojure-based server.


anyway, if you want to avoid any server side stuff you can also put it in a file and load that file via a cljs macro, then inject it into the page using some goog.stylesheet functions


(not sure if the namespace is actually called goog.stylesheet but you’ll find it 🙂)


Thanks. That's one of the directions I envisioned (though I knew fewer of the details). I can sort of imagine a few other directions too. But, I'm not real sure of the pros and cons. But, feels like I'm working too hard. This should be a solved problem, I'd hope.


one thing to make extra sure in any case is that your CSS should not conflict with the CSS your users may have, i.e. make sure you scope everything within some artificially complex class like .rf-4knt4


Yup (or, I was thinking, something more meaningful built on my library name)


I think it’ll be pretty straightforward (very little code) once you figured it out, and then you can blog about it and everyone after you will have an easier time 🙂


Yup, but I seem to spend too much time doing that.


You could also use something like


Thanks, I'll take a look.


hey guys, is there a safe way to read a clojure(script) file as a data structure? I have tried all readers possible but EDN just doesnt support autoresolved keywords (`::foo`) and specials tags like #js, #(%) or @(hello mutable) are also not supported. Since the clojure’s read is not suppose to be safe I just did a dirty string replacement but I am wondering if there is a better way 😕


You can use from tools.reader


Never mind I read you wanting the special tags and autoresolved keywords


The ClojureScript Analyzer API might be what you want


I've used it in the past to read in ClojureScript files as data structures, it takes a bit work to read ClojureScript files in the way you describe


It's the closest thing I can think of off the top of my head


Thanks for the info. I tried it but it doesn't work for my use case :/


@carocad I think some of the issues are dealt with in the way the REPL source macro is implemented. See


(For example, I suspect you can make use of *alias-map* to get some things to resolve.)


@mfikes Oh nice I think that is exactly what I was looking for. One thing though, afaik the reader is not suppose to be safe for code reading due to the posibility of code evaluation/injection. Although in my case only the developers own files are meant to be read, is that still the way to go or should I use another reader?


Perhaps you can bind *read-eval* to false to prevent #=


yeah I think I would go with that. I just got scared by the big warning in clojuredocs 😅:


@thheller Thank you. I'm still working out exec-maven-plugin usage with regards to dependencies (guava conflicts, I think)...will report back.


Yes, just a version conflict, since the project was using very old closure.jar


Is there any library in ClojureScript to communicate with sockets (not websockets)? Or some kind of "socket REPL" client in ClojureScript? I'm trying to do some tooling in Atom editor


I’m confused as to why you need this? the standard REPLs are simple stream based things - can you clarify a bit what you want to do?


@dnolen Currently I'm using nREPL to connect to a clojure REPL. I'm interested in working with the new socket REPL (instead of nREPL) and unrepl to see if I can drop nREPL (and maybe work with Lumo and other implementations that support Socket REPL too)


I could open a process and listen to stdin and stdout/stderr, but with sockets I could debug remote apps too


k, but then there isn’t much to do, doesn’t Atom support socket stuff via Node.js?


Yep, I just want to know if someone wrapped sockets (or made somekind of client) in ClojureScript already, so I would not reinvent the wheel


does tubular do that?


ah I guess not


@mauricio.szabo I’m having trouble reconciling what you’re saying with your goal?


are you going to write ClojureScript for Atom?


ah k - yeah not aware of anything - fortunately Node.js socket stuff isn’t that troublesome


Maybe that'd make implementation a little easier