Dip A Toe In Some ClojureScript
JavaScript's Math.random()
(.random js/Math)
(Math/random)
Clojure's Rand
(rand)
(* (Math/random) n)
Rand-Int
(rand-int 5)
Rand-Nth
(rand-nth ["a" "b" "c"]) (rand-nth '("a" "b" "c"))
Writings about one coder's stories & experiences.
Dip A Toe In Some ClojureScript
If you are new to ClojureScript I highly recommend copying and pasting the code snippets below into app.klise.tech, my personal favorite online REPL for evaluating ClojureScript code.
JavaScript's Math.random()
Since ClojureScript has great JS interop, we can call our good old "Math.random()" function from JavaScript. In clojure syntax we put the function first prefixed with a dot ( . ), followed by object / class containing the function, in this case the Math class of our super interop js construct.
(.random js/Math)
Since the JavaScript Math class in converted to the ClojureScript Math namespace you can even just write this:
(Math/random)
Is this not the most beautifully succinct line of code you've ever seen? "Evaluate the random function in the Math namespace". Muah. It's a slap in the face to OOP and brings a tear to the eye of bruffles the unicorn who lives on the planet neptune. Remember that we are in JavaScript land now, and this still gives you a random JS floating point number between 0 (inclusive) and 1 (exclusive).
Clojure's Rand
My second way of creating random numbers in clojurescript is to use the built-in clojure function, rand.
(rand)
Rand has been around since back in the good ole' days when there was only JVM clojure. An interesting fact is that ClojureScript's rand is actually implemented using the same Math.random() as our first method. Rand is a thin abstraction on top of Math.random, and a cool little thing to note about it is that you can pass in a number n to get a number between 0 (inclusive) and the number n (exclusive) which you can't do with JavaScript's Math.random. I normally use Webstorm with the Cursive plugin, and I was actually able to just type "(rand)" into a cljs file, ctrl + click the word rand, then jump directly to the implementation for rand in clojurescript's core library. Try doing that in emacs (and definitely leave a comment if it actually is possible). Just in case you have a lame editor where you can't ctrl + click right to the file and line, you can check out the file and line I'm talking about here. It's really not complicated at all and is based upon this simple calculation:
(* (Math/random) n)
Imo just "(math/random)" it much shorter, cleaner, and just makes me feel good when I read and write it.
Rand-Int
Don't you just love function names that are so obvious you almost don't even need to look at the documentation to make sure what it does? Well, rand-int just returns a random int, and to quote the doc string exactly it, "Returns a random integer between 0 (inclusive) and 1 (exclusive)." This one basically just takes the random float that we would make between 0 and n as passes it into "Math/floor". You can see the true implementation in all its breathtaking awe and beauty here.
(rand-int 5) Rand-Nth
I'll be honest, I found out about rand-nth from the first time I ctrl+ clicked rand and started snooping around clojurescript's core.cljs file, but I'm glad I did because it's one cool ass motherf%ckin' function! Basically, it's a rand function for collections that will return a random element from the collection.
(rand-nth ["a" "b" "c"]) (rand-nth '("a" "b" "c")) Why Random Numbers Are Beautiful
A random number is like a blooming cherry blossom that must be nurtured and appreciated. If you have three objects in front of you, how do you truly randomly choose one? There's something about as humans that we cannot by our own brains generate totally random numbers. If you needed a random number on the spot what would you do? You might pull out your cell phone and Google for "random number generator", and that's exactly my point. The ability to create this unbiased variety of selection is an incredible power wielded by few other than the computer programmer. Take it, my friends, and do great things with it!
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorThe posts on this site are written and maintained by Jim Lynch. About Jim...
Categories
All
Archives
August 2021
|