Skip to content

Instantly share code, notes, and snippets.

View viebel's full-sized avatar

Yehonathan Sharvit viebel

View GitHub Profile
(ns foo.me
(:require [clojure.test :refer [deftest is are run-tests]]))
(defn me [x]
(+ x 2))
(deftest test-me
(is (= 4 (me 2))))
(ns my.test
(:require [cljs.math :as m]
[clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop :include-macros true]))
(defn d= [a b] (or (= a b) (and (js/isNaN a) (js/isNaN b))))
(def safe-integer (gen/choose js/Number.MIN_SAFE_INTEGER js/Number.MAX_SAFE_INTEGER))
@viebel
viebel / associations.clj
Created December 20, 2021 12:55
Implementing associations (a la ActiveRecord) in Data-Oriented Programming
;; https://guides.rubyonrails.org/association_basics.html
(def book-info {:title "Seven Habits of Highly Effective People"})
(def author {:name "Stephen Covey"
:id "steph"})
(def association {:association-field :author-id
:parent-field :id})
(defn add-to [parent child association]
{
"type": "object",
"required": ["firstName", "lastName"],
"properties": {
"firstName": {"type": "string"},
"lastName": {"type": "string"},
"bookList": {"type": "array",
"items": {
"type": "object",
"properties": {
import (
"fmt"
"math/big"
)
func factorial(x *big.Int) *big.Int {
n := big.NewInt(1)
if x.Cmp(big.NewInt(0)) == 0 {
return n
}
@viebel
viebel / godel-escher-bach-g-fractal.cljs
Last active August 18, 2021 01:48
Godel Escher Bach - G fractal from Chapter 5
;; Godel Escher Bach - G fractal from Chapter 5
(set!
(.-innerHTML js/klipse-container)
"<canvas style='width:100%; height:100%'></canvas>")
(def canvas (aget (js/document.getElementsByTagName "canvas") 0))
(set! (.-height canvas) (.-innerHeight js/window))
(def ctx (.getContext canvas "2d"))
@viebel
viebel / join-arrays.js
Created April 15, 2021 05:17
Joining arrays in JavaScript (like in SQL)
function joinArrays(a, b, keyA, keyB) {
var mapA = _.keyBy(a, keyA);
var mapB = _.keyBy(b, keyB);
var mapsMerged = _.merge(mapA, mapB);
return _.values(mapsMerged);
}
var dbBookInfos = [
{
"isbn": "978-1982137274",
@viebel
viebel / progcat.md
Last active March 31, 2021 04:46
Programming languages categorization

| | Object-Oriented | Functional |
|----- | ----- | | | Statically-typed | Java
C#
C++ | OCaml
Haskell
Scala | | Dynamically-typed | Ruby Python | JavaScript Clojure Erlang Elixir |

@viebel
viebel / proxy.cljs
Last active October 19, 2021 08:54
Make a ClojureScript object accessible as a JavaScript object without converting it
;; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
(defn proxy [m]
(js/Proxy. m #js {:get (fn [target prop]
(let [prop' (if (vector? target)
(js/parseInt prop)
(keyword prop))]
(let [v (or (get target prop')
(get target prop))]
(if (coll? v)
(proxy v)
function setVerbatim (obj, k, v) {
// In Lodash, a dot in a key is interpreted as a nesting object
// For instance, _.set({}, "a.b", 2) returns {"a": {"b": 2}}
// While setVerbatim({}, "a.b", 2) returns {"a.b": 2}
return _.merge(obj, {[k]: v})
}
function flattenObject(obj, prefix = '') {
return _.reduce(obj,
function(acc, v, k) {