Skip to content

Instantly share code, notes, and snippets.

View defndaines's full-sized avatar

Michael S. Daines defndaines

View GitHub Profile
@defndaines
defndaines / title_case.ex
Last active July 26, 2023 03:30
Functions for Title Case
defmodule Title do
@moduledoc """
Build up a title-case function for ensuring that a line is properly cased.
"""
# Includes articles as well
@prepositions ~w(a aboard about above abreast absent across after against ago
along aloft alongside amid among an and apropos around as aslant astride at atop
before behind below beneath beside besides between beyond but by circa cum
despite during except for from in including inside into less like next
@defndaines
defndaines / transitive_clojure_pg.sql
Last active June 7, 2022 16:12
Transitive Closure of Acyclic Graphs [PostgreSQL]
-- Maintaining Transitive Closure of Graphs in SQL (1999)
-- https://corescholar.libraries.wright.edu/knoesis/399/
-- Set up
DROP TABLE IF EXISTS graph;
DROP TABLE IF EXISTS transitive_closures;
CREATE TABLE graph (
x VARCHAR(5) NOT NULL,
@defndaines
defndaines / transitive_closures_1999.sql
Last active June 6, 2022 18:55
Transitive Closure of Acyclic Graphs [Original]
-- Maintaining Transitive Closure of Graphs in SQL (1999)
-- https://corescholar.libraries.wright.edu/knoesis/399/
-- 2) Transitive Closure of Acyclic Graphs
-- Maintenance Under Insertion
-- Insertion of edge (a, b).
SELECT *
@defndaines
defndaines / snake-kebab.clj
Created March 2, 2018 17:25
Converting snake_case to kebab-case and back again
(defn kebab-snake
"Convert a kebab-case keyword to snake_case."
[kw]
(-> kw name (clojure.string/replace #"-" "_") keyword))
(defn snake-kebab
"Convert a snake_case keyword to kebab-case."
[kw]
(-> kw name (clojure.string/replace #"_" "-") keyword))
@defndaines
defndaines / deps.edn
Created February 27, 2018 20:53
Example using Jsoup in Clojure to parse a webpage
{:deps
{org.jsoup/jsoup {:mvn/version "1.11.2"}}}
@defndaines
defndaines / chrono.clj
Created February 19, 2018 15:05
Set up #inst and #date for Clojure to use java.time libraries
(defmethod print-method java.time.LocalDate
[v ^java.io.Writer w]
(.write w "#date \"")
(.write w (.format v java.time.format.DateTimeFormatter/ISO_LOCAL_DATE))
(.write w "\""))
(java.time.LocalDate/now)
(defn format-instant
[v ^java.io.Writer w]
@defndaines
defndaines / move-namespace.clj
Created February 7, 2018 21:32
Example of using tools-namespace to move a Clojure namespace
(require '[clojure.tools.namespace.move :as move])
(move/move-ns
'proj.old.loc
'proj.new.shiny.loc
"src"
["src" "test"])
@defndaines
defndaines / sqlite.cljs
Last active March 19, 2024 22:35
Attempt to Use SQLite3 from ClojureScript
;; Attempting to access an SQLite DB from Loom.
;; SQLite3 is installed and npm installed.
(def sqlite (js/require "sqlite3"))
(defn on-error [err]
(when err
(js/console.error err.message)))
@defndaines
defndaines / KafkaSink.scala
Created February 8, 2017 16:31
Kafka façade for use in a Spark job (adapted liberally from elsewhere)
import java.util.concurrent.Future
import java.util.concurrent.atomic.AtomicReference
import org.apache.kafka.clients.producer.{Callback, KafkaProducer, ProducerRecord, RecordMetadata}
import scala.collection.JavaConversions._
/** This construct allows for a KafkaPublisher to be reused in an executor.
*
* Establishing communication to the Kafka cluster is expensive, so this should
@defndaines
defndaines / AvroCodec.scala
Created January 26, 2017 18:42
Scala Avro encoding and decoding to bytes without using additional libraries (From Confluent or Twitter)
import java.io.ByteArrayOutputStream
import org.apache.avro.Schema
import org.apache.avro.io.{BinaryDecoder, BinaryEncoder, DatumReader, DatumWriter, DecoderFactory, EncoderFactory}
import org.apache.avro.reflect.{ReflectDatumReader, ReflectDatumWriter}
import org.apache.avro.specific.SpecificRecordBase
class AvroCodec[A <: SpecificRecordBase](schema: Schema) {
val decoderFactory: DecoderFactory = DecoderFactory.get()