Skip to content

Instantly share code, notes, and snippets.

View taylorwood's full-sized avatar

Taylor Wood taylorwood

View GitHub Profile
@taylorwood
taylorwood / Exercise.fsx
Last active September 23, 2018 22:18
Relay Foods exercise
open System
// Data types //
/// Describes an Address, its physical location, and the times it will be occupied.
[<StructuredFormatDisplay("{Name}")>]
type Address = {
Name: string
Street: string
City: string
@taylorwood
taylorwood / Solutions.fs
Created September 14, 2016 21:56
Phone screen challenge
// A sequence of integers is defined as F(N) = F(N-1) * F(N-2),
// with F(0) = 1 and F(1) = 2.
// Write a function which, given N, prints N and F(N)
// f(2) = F(2-1) * F(2-2) = F(1) * F(0) = 2 * 1 = 2
// f(3) = f(3-1) * f(3-2) = f(2) * f(1) = 2 * 2 = 4
// f(4) = f(4-1) * f(4-2) = f(3) * f(2) = 4 * 2 = 8
// f(5) = f(5-1) * f(5-2) = f(4) * f(3) = 8 * 4 = 32
// f(6) = f(6-1) * f(6-2) = f(5) * f(4) = 32 * 8 = 256
// recursive
@taylorwood
taylorwood / infix-spec.clj
Created October 8, 2017 00:56
Recursive clojure.spec for grouped/nested infix expressions http://taylorwood.github.io/2017/10/04/clojure-spec-boolean.html
(ns playground.test
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as sgen]
[clojure.spec.test.alpha :as stest]
[clojure.string :as cs]
[clojure.walk :as walk]))
(def op-keys #{:and :or})
(s/def ::expression
@taylorwood
taylorwood / parts.clj
Created October 22, 2017 17:10
Recursive function for integer partitions
(defn parts
"Returns integer partitions for n."
([n]
(parts n n [] []))
([n m acc prefix]
(if (= 0 n)
(conj acc prefix)
(reduce
(fn [st i]
(into st (parts (- n i) i acc (conj prefix i))))
@taylorwood
taylorwood / emoji.clj
Last active December 15, 2017 17:02
Parse/render Unicode.org Emoji data https://www.unicode.org/reports/tr51/#Data_Files
(ns unicode.emoji
"Parses Unicode.org Emoji specifications."
(:require [clojure.java.io :as io]
[clojure.string :as cs]))
(defn slurp-lines
"Returns lines from unicode emoji file. Optionally reads file of same name
from resources."
[file & [resource?]]
(-> (if resource?
@taylorwood
taylorwood / stackoverflow-svg-icon-jekyll.html
Created January 5, 2018 17:46
Stack Overflow SVG icon for GitHub Pages Jekyll Footer
{% if site.stackoverflow_username %}
<li>
<a href="https://stackoverflow.com/story/{{ site.stackoverflow_username }}">
<span class="icon icon--stackoverflow">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 120 120" style="enable-background:new 0 0 120 120;" xml:space="preserve">
<polygon fill="#828282" points="84.4,93.8 84.4,70.6 92.1,70.6 92.1,101.5 22.6,101.5 22.6,70.6 30.3,70.6 30.3,93.8 "/>
<path fill="#828282" d="M38.8,68.4l37.8,7.9l1.6-7.6l-37.8-7.9L38.8,68.4z M43.8,50.4l35,16.3l3.2-7l-35-16.4L43.8,50.4z M53.5,33.2 l29.7,24.7l4.9-5.9L58.4,27.3L53.5,33.2z M72.7,14.9l-6.2,4.6l23,31l6.2-4.6L72.7,14.9z M38,86h38.6v-7.7H38V86z"/>
</svg>
</span>
@taylorwood
taylorwood / clj-jdbc-postgres.clj
Created February 6, 2018 22:39
Clojure JDBC Postgres streaming resultset
(defn query-lazy ;; adapted from https://stackoverflow.com/a/39775018
[[sql & params] & {:as opts}]
(jdbc/with-db-transaction [tx (db/connection)]
(jdbc/query tx
(into [(jdbc/prepare-statement (:connection tx) sql {:fetch-size 100})] params)
opts)))
@taylorwood
taylorwood / Player.clj
Created April 30, 2018 12:40
CodinGame Battle Royale Bot
(ns Player
(:require [clojure.pprint :refer [print-table pprint]]
[clojure.set :as set])
(:gen-class))
(defmacro debug [& body] `(binding [*out* *err*] (do ~@body)))
(def split-by (juxt filter remove))
(defn read-map [ks]
(let [vs (repeatedly (count ks) read)]
(zipmap ks vs)))
@taylorwood
taylorwood / instaparse.clj
Created May 14, 2018 20:06
Clojure Instaparse examples
;; "human" date/time format parsing
(def human-time-parser
(insta/parser
"S = H (':' M)? ' '? P? (' ' Z)?
H = #'[1-9]' | #'1[0-2]'
M = #'0[0-9]' | #'[1-5][0-9]'
P = AM | PM
AM = 'A' 'M'?
PM = 'P' 'M'?
@taylorwood
taylorwood / README.md
Last active May 6, 2020 00:19
clj.native-image CLI JSON-to-EDN example

GraalVM native image + deps.edn example

A CLI tool that reads JSON from stdin then writes it to stdout in EDN format.

Build

➜ clojure -A:native-image