Skip to content

Instantly share code, notes, and snippets.

View viebel's full-sized avatar

Yehonathan Sharvit viebel

View GitHub Profile
(ns simple.core
(:require [reagent.dom :as rdom]
[clojure.string :as str]
[re-frame.db :as db]
[re-frame.core :as rf]))
;; A detailed walk-through of this source code is provied in the docs:
;; https://github.com/Day8/re-frame/blob/master/docs/CodeWalkthrough.md
;; -- Domino 1 - Event Dispatch -----------------------------------------------
@viebel
viebel / fizzbuzz.cljs
Last active August 18, 2022 01:15
the most elegant implementation of FizzBuzz - without a single if
(defn choice [a b]
(clojure.string/join (or (seq a) (seq b))))
(defn fizzbuzz [n]
(let [fizzes (cycle ["" "" "Fizz"])
buzzes (cycle ["" "" "" "" "Buzz"])
words (map str fizzes buzzes)
numbers (map str (rest (range)))]
(take n (map max words numbers))))
(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))
;; 1. hello worlds
;; 1.1. Write a function that receives a name and prints to the console Hello <name>!
;; 1.2. Write a function that receives several names and prints to the console Hello <name>! for each name
;; 1.3. Write of code that checks your functions work properly (layman unit tests)
(defn hello [name]
(str "Hello, " name "!"))
(ns foo.me
(:require [clojure.test :refer [deftest is are run-tests]]))
(defn me [x]
(+ x 2))
(deftest test-me
(is (= 4 (me 2))))
@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]
@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)
{
"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"))