Public
Edited
Jun 3, 2024
Insert cell
Insert cell
function sqrtjs(n, guess=1.0){
const thershold = 1e-15 * n
const average = (x, y) => (x + y) / 2
const good_enough = Math.abs(n - guess*guess) < thershold
const improved_guess = average(guess, n/guess)
return good_enough? guess:sqrtjs(n, improved_guess)
}
Insert cell
sqrtcj = cljs`(fn [n]
(def threshold (* 1e-15 n))
(defn abs [x] (if (< x 0) (- x) x))
(defn average [x y] (/ (+ x y) 2))
(loop [guess 1.0]
(let [good-enough? (< (abs (- n (* guess guess))) threshold) improved-guess (average guess (/ n guess))]
(if good-enough? guess (recur improved-guess)))))`
Insert cell
[sqrtjs(0.01), sqrtjs(4), sqrtjs(100)]
Insert cell
[sqrtcj(0.01), sqrtcj(4), sqrtcj(100)]
Insert cell
Insert cell
fact = cljs`(fn [n]
(loop [n n acc 1]
(if (= n 0) acc
(recur (dec n) (* acc n)))))`
Insert cell
Insert cell
function fibsjs_rec(n) {
return n===0 ? 0 : n===1 ? 1 : fibsjs_rec(n-1) + fibsjs_rec(n-2)
}
Insert cell
fibscj_rec = cljs`(defn fibonacci [n]
(cond
(= n 0) 0
(= n 1) 1
:else (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))`.root
Insert cell
fibsjs_rec(10)
Insert cell
fibscj_rec(10)
Insert cell
function fibsjs_iter(n, a=1, b=0){
return n===0 ? b : fibsjs_iter(n-1, a+b, a)
}
Insert cell
fibsjs_iter(100)
Insert cell
Insert cell
cljs`(Math/abs 10)`
Insert cell
cljs `(range 10)`
Insert cell
cljs `[1]`
Insert cell
cljs `{:a 1}`
Insert cell
cljs `(map inc (range 10))`
Insert cell
cljs `(clojure.string/join ["a" "b"])`
Insert cell
cljs `(clojure.string/join (reverse "reppot nee si seeK"))`
Insert cell
Insert cell
pow = cljs `(fn [x n] (reduce (fn [acc _] (* acc x)) 1 (range n)))`
Insert cell
pow(10, 2) + 20
Insert cell
Insert cell
xf = cljs `(comp (map inc) (map (partial * 20)))`
Insert cell
cljs(`(into [] xf (range 10))`, {xf: xf})
Insert cell
Insert cell
cljs = function(...args) {
var code, context, s = args[0]; // Depending on the usage `` or () args is an array of arrays or a an array of strings
if (s && typeof s.valueOf() === "string") code = args[0], context = args[1];
else code = args[0][0];
return sci.toJS(sci.evalString(code, {bindings: context}));
}
Insert cell
sci = require("@borkdude/sci")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more