Skip to content

Instantly share code, notes, and snippets.

View bmc's full-sized avatar

Brian Clapper bmc

View GitHub Profile
@bmc
bmc / stack.sh
Created October 28, 2011 21:04
A stack implementation, in bash
# A stack, using bash arrays.
# ---------------------------------------------------------------------------
# Create a new stack.
#
# Usage: stack_new name
#
# Example: stack_new x
function stack_new
{
@bmc
bmc / rfc1918.rb
Created May 19, 2012 01:06
Quick and dirty Ruby module to determine if IP address (as string) is RFC-1918 address
module RFC1918
def is_rfc1918_address(ip)
unless ip =~ /^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$/
raise "#{ip} is not an IP address"
end
octets = [$1, $2, $3, $4].map &:to_i
raise "#{ip} is a bad IP address" unless octets.all? {|o| o < 256}
# The Internet Assigned Numbers Authority (IANA) has reserved the
@bmc
bmc / StaticFile.scala
Last active September 3, 2021 20:08
I needed code to serve static files from an Akka HTTP server. I wanted to use fs2 to read the static file. Michael Pilquist recommended using streamz to convert from an fs2 Task to an Akka Source. This is what I came up with. (It does actually work.)
object StaticFile {
// Various necessary imports. Notes:
//
// 1. fs2 is necessary. See https://github.com/functional-streams-for-scala/fs2
// 2. streamz is necessary. See https://github.com/krasserm/streamz
// 3. Apache Tika is used to infer MIME types from file names, because it's more reliable and
// fully-featured than using java.nio.file.Files.probeContentType().
//
// If using SBT, you'll want these library dependencies and resolvers:
@bmc
bmc / Article.scala
Created September 25, 2012 19:21
One way to do transactions in Play with Anorm
case class Article(...);
object Article {
import DBUtil._
def delete(id: Long): Either[String, Boolean] = {
withTransaction { implicit connection =>
SQL("DELETE FROM comments WHERE article_id = {id}").on("id" -> id).executeUpdate()
SQL("DELETE FROM appusers WHERE id = {id}").on("id" -> id).executeUpdate( )
Right(true)
@bmc
bmc / FS2ReadInputStream.scala
Last active July 28, 2019 11:04
Read a CSV-like, pipe-delimited (jar) resource into Person records, via FS2
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.Date
import java.text.SimpleDateFormat
import fs2._
// id and gender should really be more strongly-typed, but
// this is just a demo...
case class Person(id: Int,
firstName: String,
@bmc
bmc / printSchemaAsCode.scala
Last active October 2, 2018 12:18
Patch a printSchemaAsCode() method into Spark DataFrame, in Python and Scala
import org.apache.spark.sql.types._
import org.apache.spark.sql._
object Implicits {
def schemaAsCode(schema: StructType, indentation: Int = 2): String = {
def prettyStruct(st: StructType, indentationLevel: Int): String = {
val indentSpaces = " " * (indentationLevel * indentation)
val prefix = s"${indentSpaces}StructType(List(\n"
val fieldIndentSpaces = " " * ((indentationLevel + 1) * indentation)
val fieldStrings: Seq[String] = for (field <- st.fields) yield {

Keybase proof

I hereby claim:

  • I am bmc on github.
  • I am bmc (https://keybase.io/bmc) on keybase.
  • I have a public key whose fingerprint is B57B 6B4B 451A F540 4D21 BEA4 1588 40B4 A8A7 FD98

To claim this, I am signing this object:

@bmc
bmc / fixepub.rb
Created March 7, 2011 19:57
Hack a Pandoc-generated EPUB to have a cover image.
#!/usr/bin/env ruby
#
# Fix an EPUB document generated by Pandoc to have a title page.
#
# Usage: fixepub epub_in epub_out cover_png
#
# Only supports PNG at the moment, but easily hacked to support JPEG.
#
# Requires these gems:
#
@bmc
bmc / event-hidden.coffee
Created December 6, 2012 02:25
jQuery: Create a "hidden" event handler, to trigger when an element is hidden
# Simulate a "hidden" event by overriding jQuery's hide() method.
oldHide = $.fn.hide
$.fn.hide = (speed, callback) ->
# This isn't a real event. Use "triggerHandler", to fire the jQuery handlers, without
# treating it as a real DOM event.
$(this).triggerHandler('hidden')
oldHide.apply(this, arguments)
@bmc
bmc / ContextualThing.scala
Last active March 27, 2017 00:40
Attempt to embed any type in a Contextual interpolator
package org.clapper
import scala.language.implicitConversions
import contextual._
object ThingTest1 {
case class Thing(s: String)