clojurescript

More functional way to do this?

会有一股神秘感。 提交于 2020-01-24 21:14:42
问题 This post of mine discusses Thomson's paradox, and simulates it in Clojure. The state function returns the state of the lamp at time = t. (defn thomsons-lamp [] (iterate (fn [[onoff dur]] [(not onoff) (/ dur 2)]) [true 1])) (defn state [t] (let [t-vals (map second (thomsons-lamp))] (loop [i 1] (if (<= t (apply + (take i t-vals))) ((comp first last) (take i (thomsons-lamp))) (recur (inc i)))))) How do I define a cleaner state function (preferably without loop/recur)? 回答1: The only sins here

How do you reference javascript's this keyword from clojurescript?

不问归期 提交于 2020-01-22 17:51:12
问题 I'm integrating some ClojureScript code with a JS library call that takes a callback function. The JS library passes data to the callback using JavsScript's "this" keyword. I can get it to work using (js* "this"). For example: (libraryCall (fn [] (.log console (js* "this")))) Is there a way to get at the "this" context from ClojureScript without resorting to js*? 回答1: Use the built-in this-as macro. It takes a name and a body, and evaluates the body with the name bound to JavaScript this . e

How to analyze Closure Compiler bundle size

时光怂恿深爱的人放手 提交于 2020-01-11 09:20:07
问题 I have an app in ClojureScript, which uses Google's Closure Compiler as a compiler backend. The resulting bundle using advanced optimizations seems way too big for what it is. I blame the dependencies but how do I find out which modules are taking the most bytes in the output bundle? I scanned through all the Closure Compiler options and didn't find anything useful. Then I tried to learn about source maps and use that to calculate individual module size but with no success. I would like a

What was the reasoning behind ClojureScript not needing Clojure's defstruct?

让人想犯罪 __ 提交于 2020-01-11 05:19:13
问题 defstruct is not supported in ClojureScript - it would appear to be by design. Now it may be that this is effectively a deprecated part of the Clojure language, and the designers of ClojureScript were just hoping everyone had moved on. (But this is my speculation). My question is: What was the reasoning behind ClojureScript not needing Clojure's defstruct? 回答1: defstruct is effectively deprecated in the language, in favor of defrecord . We are supposed to move on in (JVM-based) Clojure, so I

What are the namespace gotchas for clojurescript when coming from clojure?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 10:34:10
问题 I'm trying to understand the namespacing model in clojurescript. I understand that javascript doesn't come built in with namespace support, so its been an add on via the google closure library. However, I don't understand how clojurescript deals with these differences. Can someone please explain? Lets say for example I want to use the google charts api and whenever possible would like to use advanced compilation. What do I use in the closure/build call, how would the ns form look like and

ClojureScript zipmap tricks me or what?

徘徊边缘 提交于 2020-01-06 06:19:50
问题 I use Clojurescript to develop webbrowser-games. (Actually a friend of mine teaches me, we started only a few weeks ago). I wanted to generate a map in which keys are vectors and values are numbers. For e.g.: {[0 0] 0, [0 1] 1, [0 2] 2, ...}. I used this formula: (defn oxo [x y] (zipmap (map vec (combi/cartesian-product (range 0 x) (range 0 y))) (range (* x y)))) (where combi/ refers to clojure.math.combinatorics). When it generates the map, key-value pairs are ok, but they are in a random

How can i use a session for both clojure/script

↘锁芯ラ 提交于 2020-01-04 13:10:53
问题 How can i use single session for both clojure and clojurescript. For my login web application Server side i am using clojure and client side clojurescript. And i need a session which is accessible from both client and server. Is that possible? 回答1: The example sente project has a session which is accessible from both client and server. You will probably need to spend some time with it and mould it to your needs. But the example itself shows logging in and then a :uid inside :session , which

How does Elm compare to ClojureScript?

回眸只為那壹抹淺笑 提交于 2019-12-31 10:59:12
问题 I'm reaching a point where GUI coding with Backbone.js object-oriented MVC pattern is getting quite complex, and looking around at other paradigms. MDV, FRP, ECS, oh my. How does Elm compare to ClojureScript? Where do they overlap? Both are languages very different from JS that compile to JS. I understand that Elm is a functional reactive programming (FRP) language. ClojureScript isn't necessarily FRP, but you can do FRP with it. Elm compiles with Haskell and ClojureScript with the JVM, so

Writing a “cheating” Quine in Clojurescript

China☆狼群 提交于 2019-12-25 07:19:11
问题 Suppose we wanted to write a cheating quine in clojure, we could do: (ns cheating-quine) ... stuff here doesn't really matter ... (println (slurp *file*)) Now, this does not work in Lein Figwheel because the value of file ends up being something like /tmp/form-init########.clj, and contains bootstrapping code of some sort. Question: how can we get this "cheating" quine to work in clojurescript? Note: the goal is NOT to write a quine. The goal is to write a cljs program which has access to the

In jQuery, how do i pass an event to a parent anchor if needed?

旧城冷巷雨未停 提交于 2019-12-25 03:53:08
问题 I am in clojurescript using jQuery but the answer should be the same for cljs and js I think. I have a helper function which creates creates an anchor element then puts an icon element inside of it. My anchor has a unique class for the type of button. When I do something like $('.my-btn-type').click(function(e) {console.log(e.target)}); prints if icon clicked (even though the handler is on the parent) prints if only anchor is clicked I constantly have an issue where the icon which is wrapped