Skip to content

Instantly share code, notes, and snippets.

(ns rules-to-pred)
(def products
[{:id 1
:department "bedding"
:category "pillow"
:color "white"}
{:id 2
:department "cloth"
:category "shirt"
@visibletrap
visibletrap / stack-trace.clj
Created October 29, 2015 08:32
A convenient function to peek Clojure's stack trace at a certain position in code
(defn stack-traces []
(->> (Thread/currentThread)
(.getStackTrace)
(map (juxt #(.getClassName %) #(.getMethodName %) #(.getLineNumber %)))
(map #(clojure.string/join ":" %))))
(ns correct-tags.use-parser
(:require [instaparse.core :as insta]))
; Additional dependencies
; [instaparse "1.4.10"]]
; https://github.com/Engelberg/instaparse
(def annot-parser
(insta/parser
"LINE = ('|' GROUP)+ '|'
@visibletrap
visibletrap / light.clj
Last active March 12, 2019 15:30
deps.edn + ring + compojure as a single executable shell script file. Inspired by https://gist.github.com/ericnormand/6bb4562c4bc578ef223182e3bb1e72c5
#!/bin/sh
#_(
DEPS='
{:deps {org.clojure/clojure {:mvn/version "1.10.0"}
ring/ring-core {:mvn/version "1.7.1"}
ring/ring-jetty-adapter {:mvn/version "1.7.1"}
compojure {:mvn/version "1.6.1"}}}
'
@visibletrap
visibletrap / deps.edn
Created March 12, 2019 14:56
deps.edn + ring + compojure
{:deps {org.clojure/clojure {:mvn/version "1.10.0"}
ring/ring-core {:mvn/version "1.7.1"}
ring/ring-jetty-adapter {:mvn/version "1.7.1"}
compojure {:mvn/version "1.6.1"}}}
(require '[cognitect.transcriptor :refer (check!)])
(require '[hangman.reactive-hangman :refer :all])
(comment "win case")
(-> (init-game-data "bigbear" 7)
(prefill-letters ["a"])
(handle-new-event [:guess "b"])
(handle-new-event [:guess "o"])
(defn hangman [secret-words letters]
(->> secret-words
(map str)
(every? (set letters))))
@visibletrap
visibletrap / group_address.clj
Last active May 7, 2018 08:29
Input: [{:province :a, :district :b, :amphoe :c}, {:province :a, :district :k, :amphoe :g}], Output: {:a {:c [:b], :g [:k]}}
(defn group-amphoes [locations]
(->> locations
(group-by :amphoe)
(map (juxt first (comp #(mapv :district %) second)))
(into {})))
(defn group-provinces [locations]
(->> locations
(group-by :province)
(map (juxt first (comp group-amphoes second)))
@visibletrap
visibletrap / swap-group.clj
Last active May 3, 2018 06:06
Given input: {1 [:a :b], 2 [:a], 3 [:b], 4 [:b :c], 5 [:a], 6 [:a :c :e]}. Expected output: {:a [1 2 5 6], :b [1 3 4], :c [4 6], :e [6]}
(->> {1 [:a :b], 2 [:a], 3 [:b], 4 [:b :c], 5 [:a], 6 [:a :c :e]}
(map (fn [[k v]] (zipmap v (repeat [k]))))
(apply merge-with into))
;=> {:a [1 2 5 6], :b [1 3 4], :c [4 6], :e [6]}
(ns playground.transducer)
(def f1 (comp #(map str %) #(filter even? %) #(map inc %))) ; right -> left
(def nums [4 7 8 9 5 3])
(f1 nums)
(defn f2 [x]
(map str (filter even? (map inc x))))