clojurescript

Baking-Pi Challenge - Understanding & Improving

心不动则不痛 提交于 2019-12-01 00:26:50
I spent some time yesterday writing the solution for this challenge published on Reddit , and was able to get through it without cheating, but I was left with a couple of questions. Reference material here . This is my code. (ns baking-pi.core (:import java.math.MathContext)) (defn modpow [n e m] (.modPow (biginteger n) (biginteger e) (biginteger m))) (defn div [top bot] (with-precision 34 :rounding HALF_EVEN (/ (bigdec top) (bigdec bot)))) (defn pow [n e] (.pow (bigdec n) (bigdec e) MathContext/DECIMAL128)) (defn round ([n] (.round (bigdec n) MathContext/DECIMAL128)) ([n & args] (->> [n args]

Baking-Pi Challenge - Understanding & Improving

微笑、不失礼 提交于 2019-11-30 19:05:12
问题 I spent some time yesterday writing the solution for this challenge published on Reddit, and was able to get through it without cheating, but I was left with a couple of questions. Reference material here. This is my code. (ns baking-pi.core (:import java.math.MathContext)) (defn modpow [n e m] (.modPow (biginteger n) (biginteger e) (biginteger m))) (defn div [top bot] (with-precision 34 :rounding HALF_EVEN (/ (bigdec top) (bigdec bot)))) (defn pow [n e] (.pow (bigdec n) (bigdec e)

ClojureScript and HTML entities

为君一笑 提交于 2019-11-30 18:38:53
I'm having trouble getting a non-breaking space into HTML via ClojureScript. If I use " " the string is simply printed literally. I'm using the Crate library. Google's closure library that clojurescript leverages includes string helpers that will allow you to expand HTML entities. (require '[goog.string :as gstring]) (gstring/unescapeEntities " ") Got it after reading: https://github.com/ibdknox/crate/issues/12 Basically, the issue seems to be that Crate inserts directly into the DOM thus skipping entity expansion (please someone correct me if I've misunderstood). One solution is to use the

What is Clojure spec?

夙愿已清 提交于 2019-11-30 15:42:30
问题 I could not understand the intent of clojure . spec What kind of problems does it solve? Why should we use it? 回答1: spec allows you to create specifications for data and functions. Specifications are at their core predicative (based on existing Clojure predicates) and structural, rather than type-based as you might see in a statically typed language. By basing spec on predicates, you can write specifications that are far more expressive than most type systems and using the same language as

What is Clojure spec?

旧巷老猫 提交于 2019-11-30 14:34:08
I could not understand the intent of clojure . spec What kind of problems does it solve? Why should we use it? Alex Miller spec allows you to create specifications for data and functions. Specifications are at their core predicative (based on existing Clojure predicates) and structural, rather than type-based as you might see in a statically typed language. By basing spec on predicates, you can write specifications that are far more expressive than most type systems and using the same language as your code. Specs defined on a function specify the specs for the args, the return value, and a

How do I create an JS Object with methods and constructor in ClojureScript

扶醉桌前 提交于 2019-11-30 10:51:16
问题 Imagine the task is to create some utility lib in clojurescript so it can be used from JS. For example, let's say I want to produce an equivalent of: var Foo = function(a, b, c){ this.a = a; this.b = b; this.c = c; } Foo.prototype.bar = function(x){ return this.a + this.b + this.c + x; } var x = new Foo(1,2,3); x.bar(3); // >> 9 One way to achieve it I came with is: (deftype Foo [a b c]) (set! (.bar (.prototype Foo)) (fn [x] (this-as this (+ (.a this) (.b this) (.c this) x)))) (def x (Foo. 1

call a clojurescript function by string name

梦想的初衷 提交于 2019-11-30 05:27:46
问题 I'm searching for a way to call a function given its string name in clojureScript. Something like: (call "my-fun" args) Any help welcome 回答1: A pretty hackish solution that should work: (ns eval.core (:require [clojure.string :as str])) (defn ->js [var-name] (-> var-name (str/replace #"/" ".") (str/replace #"-" "_"))) (defn invoke [function-name & args] (let [fun (js/eval (->js function-name))] (apply fun args))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Clojure & ClojureScript: clojure.core/read-string, clojure.edn/read-string and cljs.reader/read-string

微笑、不失礼 提交于 2019-11-30 02:59:11
I am not clear about the relationship between all these read-string functions. Well, it is clear that clojure.core/read-string can read any serialized string that is output by pr[n] or even print-dup . It is also clear that clojure.edn/read-string does read strings that are formatted according to the EDN specification . However, I am starting with Clojure Script, and it is not clear if cljs.reader/read-string comply with. This question has been triggered by the fact that I had a web service that was emiting clojure code serialized that way: (with-out-str (binding [*print-dup* true] (prn tags))

Is it possible to use :refer :all in a ClojureScript :require?

回眸只為那壹抹淺笑 提交于 2019-11-30 00:18:40
问题 I'm just trying out ClojureScript, starting out by converting something I wrote in Clojure into cljx. When I try to compile it I get: clojure.lang.ExceptionInfo: :refer must be followed by a sequence of symbols in :require I'm finding some oblique references online, but nowhere where it's spelled out whether I should be able to use a :refer :all in a ClojureScript program. If I can't do it, what's the reason for this restriction? 回答1: No, it's intentionally not possible. There was a

How do I create an JS Object with methods and constructor in ClojureScript

我的未来我决定 提交于 2019-11-29 22:39:11
Imagine the task is to create some utility lib in clojurescript so it can be used from JS. For example, let's say I want to produce an equivalent of: var Foo = function(a, b, c){ this.a = a; this.b = b; this.c = c; } Foo.prototype.bar = function(x){ return this.a + this.b + this.c + x; } var x = new Foo(1,2,3); x.bar(3); // >> 9 One way to achieve it I came with is: (deftype Foo [a b c]) (set! (.bar (.prototype Foo)) (fn [x] (this-as this (+ (.a this) (.b this) (.c this) x)))) (def x (Foo. 1 2 3)) (.bar x 3) ; >> 9 Question: is there more elegant/idiomatic way of the above in clojurescript?