Skip to content

Instantly share code, notes, and snippets.

View viebel's full-sized avatar

Yehonathan Sharvit viebel

View GitHub Profile
@viebel
viebel / regex_as_a_function.cljs
Last active June 10, 2016 07:51 — forked from alandipert/reinvoke.cljs
Make a regex behave like a function
; Make a regex behave like a function
; Inspired by: http://blog.klipse.tech/clojure/2016/04/07/ifn.html
(extend-type js/RegExp
IFn
(-invoke ([this s] (re-find this s))))
(#"clojure" "clojurescript")
@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}
@viebel
viebel / spec.cljs
Last active June 8, 2016 14:00 — forked from swannodette/spec.cljs
om.next query spec
(ns om.next.spec
(:require [cljs.spec :as s]))
(s/def ::ident (s/and vector? (s/cat :ident keyword? :value #(not (coll? %)))))
(s/def ::join-key (s/or :prop keyword? :ident ::ident))
(s/def ::join (s/and (s/map-of ::join-key ::query) #(= (count %) 1)))
(s/def ::union (s/and (s/map-of keyword? ::query) #(> (count %) 1)))
(s/def ::param-expr
(s/cat :query-expr ::query-expr
@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.
@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 / 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
(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 / 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
@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})
(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)]