This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)] |
OlderNewer