Skip to content

Instantly share code, notes, and snippets.

View markehammons's full-sized avatar

Mark Hammons markehammons

View GitHub Profile
package com.example.myapp
import org.scalatra._
import javax.servlet.ServletContext
/**
* This is the Scalatra bootstrap file. You can use it to mount servlets or
* filters. It's also a good place to put initialization code which needs to
* run at application start (e.g. database configurations), and init params.
*/
@markehammons
markehammons / gist:5805162
Created June 18, 2013 13:03
Classes with cyclical references cannot be pickled or unpickled. They generate out of heap space errrors.
import scala.pickling._
import json._
object Test extends App {
val x = (new X).pickle
println(x)
x.unpickle[X]
}
@markehammons
markehammons / gist:5896043
Last active December 19, 2015 04:18
An example of a memoization function and a timing function.
def memoize[T,U](fn: (T, T => U) => U) = {
var map = HashMap.empty[T,U]
def memoedFn(t: T): U = { map.getOrElseUpdate(t, fn(t, memoedFn)) }
memoedFn _
}
val memoedFib = memoize((i: Long, rec: Long => Long) => i match { case x @ (0|1) => x; case x => rec(x-1) + rec(x-2)})
def time(fn: => Unit) = {
val start = System.currentTimeMillis()
@markehammons
markehammons / modulediving.scala
Created September 21, 2018 17:10
Getting rt.jar stuff in java 9
import java.net.URI
import java.nio.file.{Files, Path, Paths}
val modPath = Paths.get(URI.create("jrt:/modules"))
def dig(p: Path): Unit = {
if(Files.isDirectory(p)) {
Files.list(p).forEach(dig)
} else {
println(p)
@markehammons
markehammons / build.sbt
Created December 19, 2018 04:22
Packaging your application with a minimized runtime courtesy of jlink
import java.io.{ByteArrayOutputStream, PrintWriter}
import java.util.spi.ToolProvider
enablePlugins(JavaAppPackaging)
//this allows us to run tools like jdeps and jlink from within the JVM
def runTool(name: String, arguments: Seq[String]): Either[String,String] = {
val maybeTool: Option[ToolProvider] = {
val _tool = ToolProvider.findFirst(name)
if(_tool.isPresent) {
import zio._
import zio.stream._
import scala.scalanative.unsafe._
import scala.scalanative.posix.unistd.{
fork,
vfork,
pipe,
close,
dup2,
STDIN_FILENO,
@markehammons
markehammons / Option.scala
Created June 12, 2021 13:14
Opaque Option type
opaque type Option[T] = T | Null
object Option:
def apply[T](t: T): Option[T] = t
def None: Option[Nothing] = null
extension [T](t: Option[T])
def map[U](fn: T => U): Option[U] =
if t == null then None else fn(t.asInstanceOf[T])
def flatMap[U](fn: T => Option[U]): Option[U] = if t == null then None else fn(t.asInstanceOf[T])
@markehammons
markehammons / AddCoverage.scala
Last active June 4, 2023 21:39
ScoverageReporting for bleep
package fr.hammons.slinc.scripts
import bleep.rewrites.BuildRewrite
import bleep.model.BuildRewriteName
import bleep.model.Build
import bleep.model.CrossProjectName
import bleep.model.Project
import bleep.model.Options
import bleep.PathOps
import java.nio.file.Paths