Skip to content

Instantly share code, notes, and snippets.

View taylorwood's full-sized avatar

Taylor Wood taylorwood

View GitHub Profile
@taylorwood
taylorwood / core.clj
Last active November 3, 2021 13:47
GraalVM polyglot interop between Clojure and JavaScript https://blog.taylorwood.io/2018/11/26/graal-polyglot.html
(ns polydact.core
(:import (clojure.lang IFn)
(org.graalvm.polyglot Context Value)
(org.graalvm.polyglot.proxy ProxyArray ProxyExecutable ProxyObject)))
(set! *warn-on-reflection* true)
(comment
(do
(def context
@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
@taylorwood
taylorwood / hickory-css-selectors.clj
Last active November 16, 2019 13:14
CSS selectors for Hickory
(ns hickory-css-selectors
"Convert CSS selectors into Hickory selectors."
(:require [clojure.string :as cs]
[hickory.select :as s]
[instaparse.core :as p]
[instaparse.transform :as pt]))
(def css-selector-parser
"An incomplete and not very good parser for CSS selectors.
https://drafts.csswg.org/selectors-3/#selectors"
@taylorwood
taylorwood / google-redirect.md
Created June 21, 2019 19:35
Google Redirect (Beta)

Google Redirect (Beta)

Google has a https://www.google.com/url?q= endpoint that redirects to q, and it turns out you can nest these recursively and it'll 302 redirect you to each one in succession.

So here's the the most inefficient link to Google that will fit in a Tweet:

https://www.google.com/url?q=https%3A%2F%2Fwww.google.com%2Furl%3Fq%3Dhttps%253A%252F%252Fwww.google.com%252Furl%253Fq%253Dhttps%25253A%25252F%25252Fwww.google.com%25252Furl%25253Fq%25253Dhttps%2525253A%2525252F%2525252Fwww.google.com

Here's some Clojure I used to make the link:

@taylorwood
taylorwood / README.md
Created April 25, 2019 14:38
Scala Slick compile error: query with group by

What happened?

This compilation error was generated by a Scala Slick query that's essentially a SQL SELECT with two JOINs and a GROUP BY:

slick.SlickTreeException: Cannot convert node to SQL Comprehension
GroupBy t3 : Vector[(t3<(Option[String'], Option[String'])>, Vector[t4<{s5: Option[scala.math.BigDecimal'], s6: SOption[OptionDisc/Int], s7: Option[String'], s8: SOption[OptionDisc/Int], s9: Option[String']}>])]
  from s2: Bind : Vector[t4<{s5: Option[scala.math.BigDecimal'], s6: SOption[OptionDisc/Int], s7: Option[String'], s8: SOption[OptionDisc/Int], s9: Option[String']}>]
    from s10: Filter s11 : Vector[t12<{s13: String', s14: String', s15: String', s16: String', s17: String', s18: Option[String'], s19: Int', s20: Option[String'], s21: String', s22: scala.math.BigDecimal', s23: String', s24: String', s25: String', s26: String', s27: String', s28: Boolean', s29: scala.math.BigDecimal', s30: String', s31: String', s32: Boolean', s33: Option[String'], s34: String', s35: Option[String'], s36:
@taylorwood
taylorwood / transducers.clj
Created September 23, 2018 23:44
Clojure transducers
(defn zip
"Returns a transducer that zips cs with inputs."
[& cs]
(fn [rf]
(let [cs (volatile! cs)]
(fn
([] (rf))
([result] (rf result))
([result item]
(if (every? seq @cs)
@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 / 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 / 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