Skip to content

Instantly share code, notes, and snippets.

View viebel's full-sized avatar

Yehonathan Sharvit viebel

View GitHub Profile
@viebel
viebel / README.md
Last active July 30, 2020 17:42 — forked from mfikes/README.md

Self-hosted:

$ plk
ClojureScript 1.10.597
cljs.user=> (require '[foo.core :refer [defnmy]])
nil
cljs.user=> (defnmy FOO clojure.string/lower-case [x]
       #_=>   (inc x))
#'cljs.user/foo
(ns simple.core
(:require [reagent.core :as r]
[re-frame.core :as rf]))
(rf/reg-event-db
:initialize
(fn [_ _]
{:loading? true}))
(rf/reg-event-db
@viebel
viebel / spec_example.cljs
Last active April 27, 2017 01:00 — forked from bhb/spec-example.clj
Recursive spec with clojure.spec
(ns viebel.gist-57d9ebad3381e727c2f0a373e1bd4eec.raw.spec-example
(:require [cljs.spec :as s]))
(s/def ::tag (s/cat :type #{:div}
:attrs map?
:children (s/spec (s/* ::tag))))
(s/explain ::tag [:div {} [[:div {} []]]])
;; In: [2 0] val: [:div {} []] fails spec: _ at: [:children :type] predicate: #{:div}
(require
'[clojure.spec :as s]
'[clojure.spec.test :as test])
(defn naive-english-explain
"Copy and paste this into your app. Figure out what it does by
trying it in production."
([] (naive-english-explain (ex-data *e)))
([spec-explain-data]
(let [p1 (-> spec-explain-data ::s/problems first)]
@viebel
viebel / set-game.clj
Last active February 22, 2017 05:03 — forked from cgrand/set-game.clj
the SET game in clojure.spec
; the SET game in clojure.spec
;; inspired by https://github.com/jgrodziski/set-game
(require '[clojure.spec :as s])
(require 'clojure.test.check.generators)
(require '[clojure.spec.impl.gen :as gen])
(s/def ::shape #{:oval :diamond :squiggle})
(s/def ::color #{:red :purple :green})
(s/def ::value #{1 2 3})
@viebel
viebel / spectest.cljs
Created February 11, 2017 21:02 — forked from Risto-Stevcev/spectest.cljs
Some functions to help integrate clojure.spec.test/check with cljs.test/deftest
(ns foo-test
(:require [cljs.spec :as s]
[cljs.spec.test :as stest]
[clojure.pprint :as pprint]
[cljs.test :refer [run-tests deftest is]]))
;; Sample function and a function spec
(defn fooo [i] (+ i 20))
(s/fdef fooo
(ns tic.tac.toe
(:require [reagent.core :as r]))
(enable-console-print!)
(defn vanilla-state []
(r/atom {:squares (vec (repeat 9 nil))
:x-is-next true
:winner nil}))
@viebel
viebel / core.clj
Last active January 5, 2017 09:30 — forked from Engelberg/core.clj
Twenty four using partitions
;; Live demo with klipse - http://app.klipse.tech/?cljs_in.gist=viebel/54a9699398205a3ed41fc881a4232e08&eval_only=1
(ns twentyfour.core
(:require [clojure.math.combinatorics :refer [partitions]]))
(def ops ['+ '- '* '/])
(def commutative #{'+ '*})
;; We can generate all the possible expressions efficiently with combinatorics' partitions
;; partitions automatically handles duplicates for us, which keeps the process efficient
@viebel
viebel / limitEval.js
Created November 1, 2016 06:02 — forked from westc/limitEval.js
Use web workers to limit the amount of time that an eval can run.
function limitEval(code, fnOnStop, opt_timeoutInMS) {
var id = Math.random() + 1,
blob = new Blob(
['onmessage=function(a){a=a.data;postMessage({i:a.i+1});postMessage({r:eval.call(this,a.c),i:a.i})};'],
{ type:'text/javascript' }
),
myWorker = new Worker(URL.createObjectURL(blob));
function onDone() {
URL.revokeObjectURL(blob);
@viebel
viebel / sets.clj
Last active October 27, 2016 06:49 — forked from cemerick/sets.clj
Name that set operation!
; I'm hoping to find a common name for a particular operation over sets that I've
; been thinking of as a "join" of sorts.
;
; It's _not_ a relational join -- the values involved aren't relations (for me
; at the moment, they're actually graphs essentially representing
; disjoint recursive sets, but I think that's probably irrelevant
; detail) -- but it feels like it's in the spirit of a full outer join
;
; Given N sets, recursively produce all unions of those sets that are disjoint.