clojurescript

Server push of data from Clojure to ClojureScript

南楼画角 提交于 2019-12-12 07:29:58
问题 I'm writing an application server in Clojure that will use ClojureScript on the client. I'd like to find an efficient, idiomatic way to push data from the server to the client as realtime events, ideally using a some combination of: http-kit core.async Ring (But I'm open to other possibilities) Can anyone provide a good example / approach to doing this? 回答1: I prefer to use aleph, here is the wiki, you can simply use wrap-ring-handler function to wrap existed handlers. For the 'push' function

getting the positions of the occurrences of one item in a sequence

谁说我不能喝 提交于 2019-12-12 04:16:07
问题 From a a sequence, I need to fetch all the positions of the occurrence of a given item. I'm asking myself if this is a good way to solve the problem: (defn get-positions [item coll] (->> (map-indexed vector coll) (filter (fn [[_ v]] (= item v))) (map first))) This also works for strings, they'll be transformed to a sequence by the first map. However, if I know the input's are strings, would there be a more string specific approach for this problem? 回答1: Since you already have a general

ClojureScript - list functions of a namespace

吃可爱长大的小学妹 提交于 2019-12-12 03:55:17
问题 How do I list the functions/vars of a ClojureScripts namespace ? This is a question that has an equivalent for Clojure, but the solutions mentioned did not work for me. 回答1: clojure.repl/dir works for me with latest ClojureScript (1.7.228) 回答2: Fow now I am doing : (.keys js/Object my.ns) As it looks like a namespace is an js object. 来源: https://stackoverflow.com/questions/35038798/clojurescript-list-functions-of-a-namespace

Execute compiled ClojureScript from the commandline with Rhino

你离开我真会死。 提交于 2019-12-12 02:46:10
问题 I understand ClojureScript can be executed within a JavaScript REPL or it can be compiled into JavaScript, then run in a browser. I couldn't find a way to use it on the server side with Rhino. Here is my way of thinking, I have a simple source file: (ns simple.hello) (println "Hello, world") I compile it to hello.js . I try to run java -jar js.jar out/goog/base.js out/goog/deps.js out/hello.js Nothing happens. How can I make it work, or is only Node.js supported on the command line? 回答1: This

Clojurescript/Reagent Unit testing component - simulate onChange

冷暖自知 提交于 2019-12-12 01:39:23
问题 For a component where I have a textbox, I need to be able to change the text in it from the test: (defn choose-city-component [] (let [inner-state (r/atom {:text ""})] (fn [] [:div [:input#txt_city { :type "text" :value (@inner-state :text) :on-change #(swap! inner-state assoc :text (-> % .-target .-value))... In the test I render it on the screen: (deftest choose-city-component-test-out ;;GIVEN render component in test (let [comp (r/render-component [w/choose-city-component] (. js/document

Testing core.async code in ClojureScript

风格不统一 提交于 2019-12-11 16:27:53
问题 I'm trying to test some core.async code, but I'm having trouble writing tests. All of my test code is in a go block, but it seems like the process my test is on isn't parked in the way I expected it to be. After I do the put on the actions channel, I would expect execution of the go block to be parked until the value is taken, but instead the is function runs straight away which causes my test to fail because the state-atom hasn’t been updated yet. Here's my test code: (deftest tab-change

:on-click renders as text

孤人 提交于 2019-12-11 15:38:34
问题 My ClojureScript function: (defn node-function [node] [:<> [:div (node :name) {:on-click #(prn "hi")}]]) renders as html text in the dom: My Documents{:on-click #object[Function]} My code looks exactly the same as :on-click examples I've found online. Why does the compiler think this is text and not a function? Thanks. 回答1: I'm not sure how the rest of your project is structured, but it looks like the property map and inner html of the div you're trying to render are transposed. The reason

Code not called from go block, but it works from REPL

蹲街弑〆低调 提交于 2019-12-11 13:44:39
问题 I have code that updates the DOM. new-recipe! calls an API to get a new recipe string. update-recipe-state next updates this state in the screen. Finally we have a call to update-transition-buttons . (defn- add-listener-to-recipe-button! [] "Listens to go button, creates a new recipe and displays it" (create-click-event-listener! (dommy/sel1 :#button-start) #(go (new-recipe!) (<! (timeout 2000)) (update-recipe-state!) (<! (timeout 2000)) (update-transition-buttons! "onboarding")))) ;; define

ClojureScript (nodejs) function that returns the string content of a file

拥有回忆 提交于 2019-12-11 13:01:00
问题 I am trying to make a function that takes a file path and returns its content as a string. Here is my code snippet: (defn get-string-from-file-path [path] (let [fs (node/require "fs") c (chan)] (.readFile fs path "utf8" (fn [err data] ((go (>! c (str data)))))) (go (def r (str (<! c)))) ; is using def correct here? r)) Is using def correct/idiomatic on line 4? After calling this function with a valid file path, I get the following error: TypeError: (intermediate value)(intermediate value)

Iteratively Construct Trie from a Tree

被刻印的时光 ゝ 提交于 2019-12-11 12:14:57
问题 Introduction The following function iteratively traverses a tree structure made of nested vectors. It tests each leaf against a predicate. The paths to all leaves which pass that truth-test are returned in a Trie structure. The later describes all found paths in a non-redundant way. (defn get-trie-of-matches [is? tree] (loop [[tree i path fk] [tree 0 [] nil] accum {}] (cond (>= i (count tree)) ;; end of level / go up (if (nil? fk) accum (recur fk accum)) (vector? (tree i)) ;; level down