Affichage des articles dont le libellé est clojure. Afficher tous les articles
Affichage des articles dont le libellé est clojure. Afficher tous les articles

samedi 20 février 2016

java8 nashorn javascript and clojure run into a repl

Java javascript engine (nashorn) is easily accessible through the javax.script fAPI. So I popped a clojure repl to instanciate a javascript compiler so Java could run my js bytecode.

(ns three.lisp.tower)

(def js (.getEngineByName (javax.script.ScriptEngineManager.) "nashorn"))

(defn jsc [code]
          (let [prelude "{add: function(a,b){return a+b;}, inc: function(n){return n+1;}}",
             link "(function(prelude){return (function(prelude){ %s })(prelude);})(%s)"
             emit (fn [l p s] (format l s p))
             js (fn [s] (.eval js s))]
        (js (emit link prelude code))))


user=> (jsc "return Array(1,2,3).map(prelude.inc).reduce(prelude.add)")
9.0

#EnoughProcrastination

mercredi 10 février 2016

frictionless Java with .. clojure

Late realization: you can use clojure with Java. Not with Java. For Java. We all know clojure java interop is good, but that's not what is at play(sic). See, Java is verbose (duh), compiled (duhbl) and mute. On the other hand, clojure, being a lisp with a repl, is real-time, short and generic, and doesn't even require `print` to inspect values most of the time. So instead of writing long main println-full methods, compiling, running, parsing the output ... you just import some java classes and start poking around. Frustration: gone. Even better, clojure is human friendly, has clojure.reflect/reflect to inspect classes and/or instances; clojure.pprint/pprint for quick formatting needs. If java bytecode came with docstrings and javadoc... clojure repl would be a better IDE than Eclipse. And it's probably a few lines away to scrape some remote or local html set and get going. People probably delegate that to manual browsing or a text editor extension. Anyway, my java project makefiles [1] have now a `repl:` rule to enjoy Java without Java (sorry dude).

[1] yes, I like IDE-free projects so a makefile with a few rules. I even learned about jdb, because yeah, text mode java debugging !

mercredi 21 août 2013

I though I'd like Clojure ...

I've been following this language and community from a distance, with wide open loving eyes. It had nice abstraction underneath, far less mutation than vintage~ Lisps, some ~syntactic niceties (words like first, rest instead of acronyms).

But coding in it felt had too much. I guess that's what Rich Hickey said, it's on the pragmatic side of things. All it brings has value on day to day problem solving. And I'm still fresh out college scheme/ml courses. But that's the thing, if I had to iterate on the lisp idea, I'd bring it closer to ML, not closer to ruby/perl (that's how I felt for the last few days). Using sml I felt my mind was constrained just enough to find abstract recursive solutions. In clojure I'm floating in a river of gems in brownian motion.

It's shallow criticism, not even criticism, just firsthand expression. I'm probably not smart enough to dive in it in 3 days and understand its idioms. This dialect different enough from lisp/schemes that my brain needs more time (says more about my brain than clojure vs others) . When finished with other things (SICP, 4clojure) I'll do a medium-- sized project in it (something I've never done on my own actually). Hoping the function based, mutation free idioms will lift me above my current limitations, whereas other languages feels comfortable doing one liners, I can never write systems in them. Maybe clojure shines at helping your mind scale above non-trivial exercises.

(lambda (k) ...)

mardi 20 août 2013

The loop/recur constructs in clojure are really nasty, and not in a good way. Let's say it will force your brain to re-evaluate its knowledge on recursive processes.
 (fn [n]
      (loop [i 0
           a [1 1]]
      (cond (< n 3) a
            (= i (- n 2)) a
            :else (recur (inc i)
                         (conj a (apply + (drop i a))))))


Blame it on Sun's JVM.

ps: this is probably the ugliest guess-fested fibonacci function I've ever seen (and written). So far.