Skip to content

Instantly share code, notes, and snippets.

View featheredtoast's full-sized avatar
🏠
Working from home

Jeff Wong featheredtoast

🏠
Working from home
View GitHub Profile
@featheredtoast
featheredtoast / knots-part2.clj
Last active December 12, 2017 07:31
advent day 10
(defn tie-knot-round
[current-list knot-lengths skip-size offset]
(if (empty? knot-lengths)
{:result current-list :skip-size skip-size :offset offset}
(let [cycle-list (cycle current-list)
knot-length (first knot-lengths)
non-knot-length (- (count current-list) knot-length)
new-offset (mod (+ knot-length skip-size offset) (count current-list))
reversed-list (reverse (take knot-length cycle-list))
rest-of-list (take non-knot-length (drop knot-length cycle-list))
@featheredtoast
featheredtoast / marshalling.cljs
Last active November 11, 2017 00:08
some js cljs fun for data manip
(defn ^:export marshall-object [perms]
(->>
(js->clj perms)
(map (juxt (fn [p] (get-in p ["modules" 0])) (fn [p] [(identity p)])))
(map (partial apply hash-map))
(apply merge-with into)
(clj->js)))
@featheredtoast
featheredtoast / index.html
Created November 9, 2017 22:52
get started with react and jsx without node, babel tasks, or webpack.
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="app">
</div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
@featheredtoast
featheredtoast / mouse.clj
Created September 13, 2017 18:14
4clojure problem 117, for science
(defn mouse
([board]
(let [raw-board
(for
[x (range (-> board first count))
y (range (count board))
:let [mouse? (= \M (get-in board [y x]))
cheese? (= \C (get-in board [y x]))]]
{:x x :y y :mouse mouse? :cheese cheese?})
[cheese-location mouse-location] (->>
@featheredtoast
featheredtoast / matching-substring-integer-counts.clj
Created September 11, 2017 17:08
Find the largest substring in which the number of integer elements is equal to number of non integer elements.
(defn find-subcoll-of-equal-int-nonint [coll]
(let [partition-even (if (even? (count coll)) (count coll) (dec (count coll)))
coll-sizes (range partition-even 0 -2)
candidates (reduce concat (map #(partition % 1 coll) coll-sizes))
candidates-group (map (juxt identity (partial group-by integer?)) candidates)
match (some
(fn [[candidate count-map]]
(and (= (count (count-map false)) (count (count-map true))) candidate)) candidates-group)]
match))
@featheredtoast
featheredtoast / watcher.clj
Created August 11, 2017 00:51
sample watcher component for restarting yourself under the reloaded.repl workflow. Attemping to call (reset) directly is interesting.. see http://justabloginthepark.com/2017/06/18/clojure-and-the-esoteric-mysteries-of-namespaces/
(ns testnut.components.watcher
(:require [com.stuartsierra.component :as component]
[suspendable.core :refer [Suspendable]]
[hawk.core :as hawk]
[clojure.java.shell :as shell]))
(defrecord Watcher [watcher]
component/Lifecycle
(start [component]
(let [watch
@featheredtoast
featheredtoast / file-watcher.sh
Created August 11, 2017 00:19
file watcher to automatically issue reset commands via inotifywait
#!/bin/bash
while inotifywait --format '%f' --exclude '.git' --exclude ".#" -r -q -e modify -e move -e create -e delete .; do
echo '(reset)' | lein repl :connect `cat .nrepl-port`
done
(defrecord FigwheelSystem [system]
component/Lifecycle
(start [this]
(if-not (:system-running this)
(do
(swap! system component/start)
(assoc this :system-running true))
this))
(stop [this]
(if (:system-running this)
@featheredtoast
featheredtoast / test.html
Created June 14, 2017 22:19
quick test for async/await syntax in es7 vs pure promises
<html>
<head>
<script>
var args = {
headers: new Headers({
'Content-Type': 'text/plain'
})};
async function useAwaits() {
try {
let resp = await fetch("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search=stack&limit=10", args);
@featheredtoast
featheredtoast / hiccup-zip.clj
Created January 13, 2017 23:55
creating a zipper for hiccup
(defn hiccup-zip
"Returns a zipper for hiccup vectors"
[root]
(clojure.zip/zipper #(or (vector? %) (seq? %))
seq
(fn [node children]
(if (keyword? (first children))
(with-meta (vec children) (meta node))
(with-meta children (meta node))))
root))