clojurescript

compiling snippets of clojure(script) into javascript

守給你的承諾、 提交于 2019-12-06 06:08:36
问题 Where in the clojurescript library can I access a function to compile snippets of clojure into js? I need this to run in the clojure (not clojurescript) repl: (->js '(fn [x y] (+ x y))) => "function(x,y){return x+y}" 回答1: Snippet compilation from Clojure REPL (require '[cljs.analyzer.api :refer [analyze empty-env]]) (require '[cljs.compiler.api :refer [emit]]) (let [ast (analyze (empty-env) '(defn plus [a b] (+ a b)))] (emit ast)) ;; result "cljs.user.plus = (function cljs$user$plus(a,b){

In clojure[script], how to return nearest elements between 2 sorted vectors

若如初见. 提交于 2019-12-06 06:03:46
In clojure[script] , how to write a function nearest that receives two sorted vectors a , b and returns for each element of a the nearest element of b ? As an example, (nearest [1 2 3 101 102 103] [0 100 1000]); [0 0 0 100 100 100] I would like the solution to be both idiomatic and with good performances: O(n^2) is not acceptable! Using a binary search or a sorted-set incurs a O(n*log m) time complexity where n is (count a) and m (count b) . However leveraging the fact that a and b are sorted the time complexity can be O(max(n, m)). (defn nearest [a b] (if-let [more-b (next b)] (let [[x y] b m

Ordinal/int/ascii value of character

非 Y 不嫁゛ 提交于 2019-12-06 03:51:09
In the clojure repl I can do: => (int \a) 97 In closurescript, I keep getting => (int \a) 0 In my current clojurescript project I've defined a var: (def ord-a (int \a)) When I inspect the emitted javascript I see: ord_a = ("a" | (0)); Which explains the discrepancy, but doesn't really do what I want. So: What am I doing wrong here? How do I get the ordinal/int/ascii value of a character in clojurescript? kongeor Clojurescript does not have character literals . As described here you can get it using js interop: => (.charCodeAt \a 0) 97 来源: https://stackoverflow.com/questions/37775349/ordinal

ClojureScript: How to add method via prototype to JS Object?

↘锁芯ラ 提交于 2019-12-06 03:10:56
问题 I'm trying to add some functionality to an existing JavaScript system. To be then used from JavaScript again (as opposed to within the ClojureScript namespace). Perhaps this isn't possible? Here's a simplification of what I want to do: // JavaScript String.prototype.foo = function() { return "bar"; } # CoffeeScript String::foo = -> "bar" I want to be able to run my script above, and then call it from elsewhere in the code. I've tried messing with extend-type and defprotocol , along with

how to output individual files to a specified directory for cljsbuild

依然范特西╮ 提交于 2019-12-06 02:21:43
问题 Using cljsbuild, I can compile all my .cljs files to one file. However, I wish to be able to pick a directory for output and have each .cljs file compile into its own .js file. How can this be specified? 回答1: You can use 'multiple build configurations': cljsbuild accepts a vector of configurations for :builds key, each element of which defines rules for compiling separate .js file (more info could be found in lein-cljsbuild README). Simple example: :cljsbuild {:builds [;; Config for first .js

How are Atoms implemented in Clojurescript?

会有一股神秘感。 提交于 2019-12-06 01:33:07
问题 In Clojure to address concurrency issues we can use an atom to write: user=> (def my-atom (atom 0)) #'user/my-atom user=> @my-atom 0 user=> (swap! my-atom inc) 1 user=> @my-atom 1 user=> (swap! my-atom (fn [n] (* (+ n n) 2))) 4 We know that this (in the Clojure implementation) is a wrapper around the Java Atomic object. Interestingly enough, Atoms are replicated in ClojureScript, at a Syntactic level - even though JavaScript runtimes don't have an Atomic reference. My question is, How are

Improve performance of a ClojureScript program

人走茶凉 提交于 2019-12-05 22:33:25
问题 I have a ClojureScript program that mainly performs math calculations on collections. It was developed in idiomatic, host-independent Clojure, so it's easy to benchmark it. To my surprise (and contrary to what the answers would suggest to Which is faster, Clojure or ClojureScript (and why)?), the same code in ClojureScript runs 5-10 times slower than its Clojure equivalent. Here is what I did. I opened a lein repl and a browser repl at http://clojurescript.net/. Then I tried these snippets in

ClojureScript map lookup slow

与世无争的帅哥 提交于 2019-12-05 18:20:18
I have a simple map: (def my-map {[1 2 3] 1 [1 2 4] 5 [3 4 2] 3 [4 5 3] 3 [5 2 5] 6 [9 2 1] 5 [8 3 1] 6}) that I use for performing lookups. This performs rather poorly, however: (time (doseq [x (range 500)] (my-map [1 2 8]))) "Elapsed time: 170 msecs" On the same machine, Clojure can do 500,000 in about 236 msecs, or about 700x faster. While it's not unexpected for Clojure to be faster than ClojureScript, I'm confused why ClojureScript would be so much slower. Any ideas about how I could make a simple multi-valued lookup map as above efficiently and in a readable manner in ClojureScript? I

A case-insensitive filter in Clojure / ClojureScript

百般思念 提交于 2019-12-05 16:54:28
I have this function : (defn list-data [alist filter-text] (filter (fn [x] (if (nil? filter-text) true (> (.indexOf x filter-text) -1))) alist)) (list-data ["Lion" "Zebra" "Buffalo" "Antelope"] "a") ;=> ("Zebra" "Buffalo") Is there a more idiomatic way of writing this function and respect the fact that I don't want a case-sensitive filter, meaning I would like that (list-data ["Lion" "Zebra" "Buffalo" "Antelope"] "a") returns the following: ;=> ("Zebra" "Buffalo" "Antelope") Thanks! (This would need to work in a .cljs file) In Clojure itself you would normally do this with a regular expression

Unable to display two components in OM

左心房为你撑大大i 提交于 2019-12-05 15:23:57
I am attempting to learn Om, and have come across something I don't understand. I would expect this code (defn search-page-view [app owner] (reify om/IRender (render [_] (dom/div #js {:id "search-block"} "Test") (dom/div #js {:id "results-block"} "Test2")))) (om/root search-page-view app-state {:target (. js/document (getElementById "app"))}) to result in this html: <div id="app> <div id="search-block"> Test </div> <div id="results-block"> Test2 </div> </div> However, it does not! The first div containing Test does not display. What am I misunderstanding? Edit with the solution (pointed out by