Fork me on GitHub

A question to anyone who has good insight in random numbers in javascript. I need a random 32-bit integer. Is there something wrong with the following function:

(defn random-32 [] 
  (Math/round (* (- (Math/pow 2 32) 1) (Math/random))))


otherwise you seem to follow the recommended approach, but Math/floor should be more uniform


@vikeri : If you want cryptographic random numbers, you can use Isaac.js. Here's a copy I have where I've added all of the JSDoc annotation to make the Closure compiler happy -


Chrome/Safari/FireFox all expose crypto.getRandomValues, which can fill in a UInt32Array, Int32Array, or the 8 bit equivalents with secure random values. IE 11 has a similar service msCrypto.getRandomValues


Here's a routine I have that tries these various ways of generating random number and falls back to Isaac.js if they aren't around -


@mfikes : As per our conversation in #planck, I think @vikeri is an example of a user who might want crypto.getRandomValues available 😉


@xcthulhu Great! Btw, could you explain why the method I mentioned would not be considered sufficiently random?


Well, so it sort of depends on how js/Math.random is implemented. This varies from platform to platform. If it's a linear congruential generator, then you'll see a lot of correlation in the lower order bits. You can see that Chrome's old V8 4.7 used to use such an RNG -


If you care deeply about this, you can try the DieHard or DieHarder statistical test suites -


how to write in clojurescript: <Form inline>...</Form>?


trying to use react bootstrap in reagent


(def form (r/adapt-react-class (aget js/ReactBootstrap "Form")))


[form {:class "inline"}] doesn't work...


from your example above “inline” is not a class, it is form element’s attribute, so you should write something like this: [:form {:inline true} …]


not sure about that true part, I’m not that familiar with reagent, you might have better luck asking in the #reagent channel


@darwin yes it works. Thanks!


[:form {:inline true}]


@xcthulhu Alright, I’m actually using React Native, so that makes it JavaScriptCore


@vikeri No crypto.getRandomValues for you then 😞


What's your RNG for?


Some sort of UUID, so it’s not sensitive from a security perspective. But I’m still interested in the subject, since I’ll do more security sensitive things further ahead.


The library I linked is my attempt to wrap all of the JavaScript cryptostuff and provide fallbacks and polyfills when it's not around.


It looked great! A little sparse on the documentation side maybe 😉


I'm dealing with crypto.subtles Promise hell right now


I know, but its 128-bits right? What I’m using only accepts 32-bit integers...


Looking at that function, it calls rand-int


So do (rand-int 0x100000000)


Aha, great! And that implementation will be better than Math.random()?


🌟 for using hexadecimal btw


Nah, it's just wrapping Math.random.


Alright, will use that for now then. And migrate to your library in the future when I need more security sensitive values.


If you are doing digital signatures, it's best to just use RFC 6979 anyway, although the performance is horrible on Nitro and Nashorn.


...if you are rolling your own like I am.


Alright, it’ll be an interesting venture. There should be a #security channel for these matters...


@mfikes : I'm seeing that the latest figwheel won't build andare: Could not Analyze target/out/cljs/core/async.cljs ----> No such var: ioc/state-machine


presumably this is ns clashing


@pat Ahh, plz log a ticket. (It could be the case that Andare is not working properly with non-self-hosted ClojureScript.) (In any case, I’d suggest only using it with things like Planck and use the official core.async with JVM ClojureScript, if you can.)


@mfikes yes I'm trying to use fw to dev some bootstrap friendly libs. it was working fine until recently


@pat there’s a bug in the latest ClojureScript release that just got fixed, probably the same thing


@dnolen same problem on master. I think it's a change in fw somewhere after 0.5.4-7


@pat huh, ok - in anycase I just cut ClojureScript 1.9.229 with the js-dependency-index fix


@pat fwiw I had similar looking issues with CLJS+Figwheel until @anmonteiro’s patch and they are gone for me now


Does anyone by chance use cursive with lein projects using checkout dependencies ?


@pseud I don’t directly, but my coworkers do. Whats the issue? I haven’t seen any differences with editor specific setup (they do what I do, and I use emacs).


Well it's just that I'm unable to figure out what conjuring is required to the damned thing to play ball with a lein project which uses checkout dependencies (meaning the classical approach of running a script/repl.clj to load figwheel via sidecar seems out of the question)


One thing I would do is make sure you add your checkouts to src paths. For instance :source-paths [“checkouts/my-lib/src”] for clj and cljs builds. Also core namespace can be fiddly if you are calling set-refresh-dirs


In fact. I think I'd be willing to drop some 200USD on some tool that could just work (tm) with regular lein projects. I really can't understand why the tooling is always this poor. Seems to me that lein has done a great job at abstracting all the insanity, how come then that all tools (cursive, cider) reintroduce all the problems ? What am I missing here?


I use checkouts fairly regularly, and those things work great for me


Oh the cljsbuild stuff is easy enough, if I run "lein figwheel" from a terminal everything is peachy. checkout dependencies work, reloading works, everything works (tm).


and how do you actually get going then ? I'm dropping figwheel, nrepl, piggieback, sidecar and god knows what into every project.clj I need to work with and I need to use a script like to get going - but once I do, I'm running a regular jvm process, not lein - and that means I'm hosed as far as checkout dependencies go


No, checkouts doesn’t care where the repl comes from. My coworkers don’t use lein repl.


So is the problem checkouts directly?


The link above specifically mentions that it breaks checkouts. I haven't been able to run a say.. "lein figwheel" session from within cursive - I have to run a regular jvm process, load a script file as the guide indicates and that then triggers figwheel.


Well, the problem, I guess, is that I'm just trying to write some code and I have (after some struggling) figured out how to make lein behave - but apparently it's a whole new thing getting cursive to work with these things. Seems like (from the run configurations dialog) that cursive wants an nrepl session - and I'm guessing that's where the misery starts


Yeah, the cursive part I am not as up to date on. But I use figwheel, and haven’t had a problem with checkouts


I just make sure I point the source paths in to look at checkouts


lein, figwheel - all the cli tools are perfect, once you finally get them configured - it's just cursive, which I'm trying out after repeated issues with cider... x_x


I think there is a setting in the cursive repl to tell it to not use lein.


There is, almost nothing but. It's lein support is abysmal. You can't even run a task. if you select lein, all you can do is specify the profile(s) to run...


yeah, I just got cider figured out, and haven’t spent a whole lot of time with cursive


I think I'll just grab a break. This is infuriating me and I don't think I'll get anything constructive done - as evident by my whining. But man, the IDE <-> clojure interplay is stoneage compared to Racket...


Thanks though, hopefully I'll figure it out once I'm more relaxed again 😛


@pseud I use boot so I don't know how to solve your issue with lein. Have you checked out the Leiningen tab? If you go to Leiningen projects > your project name > Tasks you can select a task and run it.


I have - the lein tab is really just a prepopulated list of standard lein tasks - meaning if you install any plugin like cljsbuild or figwheel they won't show up there (nor will things like 'repl'). 😞


Plug for boot.. boot's checkouts work nicely with Cursive (really you just run in the command line). You add :checkouts to your environment and boot will automatically pick up changes from all projects added. Not sure why extra tooling is needed for this to work in lein.


Well I'm forced to work with my colleagues so as nice as boot might be, it's not an option. Also, I'd expect a tool which someone actually wants to charge money for could help to make popular use-cases work, at least provide some damned documentation if automation is out of the picture. But oh well.


All I do is run my boot task in the command line (e.g. boot web-dev) and it just works. You can't do this with lein?


You can also add that command to IntelliJ's external tools so all you have to do is go to Tools > External tools > boot web-dev and click and it auto runs that command.


Could even set up a key binding for that if you wanted


You could also run figwheel from the command line and connect to the remote nrepl port from Cursive. I've used it quite a bit and it works well.


Not that I know of. But it seems that I can either bombard each lein project with third-party deps and special clj files to auto-run such that booting a nrepl (lein repl) session and then starting figwheel might work - that way I can connect via cursive to the remote nrepl (at least Cursive can't ruin that). Its just.. not elegant. So I'm looking into whether I can move all dependencies to ~/.lein/profile.clj but then I'm back to not having any easy-to-run function to start figwheel and dump me in a cljs-repl...


shaun-mahood: yea, exactly my thought 😛 Have you somehow abstracted away all the dependencies, auto-start scripts and whatnot such that you're not including a modified user.clj/repl.clj (whatever you call it) and a bunch of dev-time deps for each project ?


@pseud I think you could use lein run to run your own clj with classpath populated from project.clj setting, that would be analogous to running your standalone scripts/repl.clj


@pseud there’s also a #cursive channel, I’m sure people have run into this before. FWIW, I don’t bother with Lein here, a REPL launched via the clojure.main option works just fine


@darwin yea I'm pursuing that avenue just now 🙂


If anyone know how to achieve something like this: then I'd be golden


I would create a separate lein profile myprofile with all deps/setup needed for that, then do lein with-profile +myprofile run -m myproject.mynamespace and there define -main function which would call (figwheel)


isolating stuff into a profile is a good habit


OK, interesting, I guess that's a good approach, better than what I had considered, even


as an inspiration for you, this is pretty fresh code I still have in my head: once you get -main launched you should have checkout dependencies on your classpath and from there you can call figwheel (or do whatever)


@darwin Ooo. That lib looks useful. I have also ran into the problem with externs being slightly out of date causing exceptions in prod. Any ETA docs?


@kenny I’m sorry, won’t promise anything at this point 🙂 first I want to convert all my libraries/projects to use it, to battle test it, then I will maybe spend some time on the docs


Cool. Will keep an eye on it 🙂


is there a way to get a cljs repl from within clojure w/o using figwheel ? I have some library projects too where I really just want a repl of some sort and right now I seem to have some troubles with figwheel in those projects. Can I just get a cljs repl to use for evaluating forms and the like ?


Yea, writing a custom .lein/profile.clj with an accompanying .lein/figboot.clj which I ensured got loaded (via :repl-options) didn't work either, when it finally boots the checkout dependencies aren't observed so any changes in the parent project are not reflected until I restart the repl... Not surprisingly, it's the same if I start a repl by letting cursive start a regular java process and load some file which spawns a figwheel repl. 5 hours of no progress... 😞


Is there a nice way of loading in a JSON data file to clojurescript on load? Basically I have a bunch of data I want to search over, but I don't want it to have to make an API call, and I don't want a long loading time on the user's end.


if you have a lot of JSON data, perhaps would be the right tool 😃


I'm not sure how that tool is going to preload JSON into cljs for me...


it should help you search data without using cljs at all


I'm making a webapp.


ok, in such case JQ is bad idea 🙂


@slester not sure if that's what you're asking but you could inline the JSON or an edn representation of it using a macro


you could also just put it into the HTML file that loads your application


@martinklepsch sounds more helpful! in what format should I do that, though? just load the json into a variable, then use js/varname within the clojurescript? is there an order I need to do it in?


if you put it into the html you'd define a global that you access via js/varname, yes


you can just put the JSON into a <script> tag, not sure what other format you're asking about?


As for order it would make sense to put the JSON first if your app isn't useful without it. You can probably do it either way depending on your application code.


@martinklepsch I just wanted to read it in from a file instead of plopping it directly into the HTML


@slester you could generate the HTML, inlining the JSON? all that said simplest is very likely just loading it via an ajax call


@slester You could parse the json from another cljs file. In the end the user has to download the data one way or another into his memory. Or just parse the json to edn map during development if the data is static.


@hlolli hmm, interesting; could I read it into a variable from a static file on compilation?


you could have an immutable map and to (get map :key) or (:key map), just like you had any other clojure map.


@martinklepsch maybe this belongs in #beginners but could you point me in the right direction on how to import it via macro?

hlolli21:09:31 there are some json->edn parsers here.


oops this it clj not cljs... I personally use transit so im rarely working directly with json. This data.json will not do the trick for you.


(defn json-parse
  "Returns ClojureScript data for the given JSON string."
  (js->clj (JSON/parse line)))

cljs.user=> (json-parse "{\"a\" : 2}")
{"a" 2}