Skip to content

Instantly share code, notes, and snippets.

View steinybot's full-sized avatar

Jason Pickens steinybot

View GitHub Profile
@steinybot
steinybot / StringOpsMonad.scala
Created December 22, 2015 22:32
My attempt at learning Monads in Scala
/**
* I wrote this while trying to figure out what a monad was and how you would write one. No guarantees that this is
* actually a monad or good code at all.
*
* I wanted this monad to encapsulate side effects and apply them lazily. One interesting observation that I found
* from all the println statements was that in order for the bind (flatMap) to not evaluate the side effect was to use
* by-name parameters. Not sure if this is really a unit function anymore...
*/
object StringOpsMonad extends App {
println("-----")
@steinybot
steinybot / CaseClassMacros.scala
Last active May 8, 2016 23:37
Class for providing variable substitution in a command at runtime.
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
/**
* Macro bundle for case class macros.
*/
class CaseClassMacrosImpl(val c: blackbox.Context) {
import c.universe._
@steinybot
steinybot / LoggerOutputStream.scala
Last active September 13, 2017 03:25
Convert a SBT Logger to an OutputStream
private object LoggerOutputStream {
def info(logger: Logger, charset: Charset = StandardCharsets.UTF_8, maxMessageSize: Int = 8192): OutputStream =
apply(logger, Level.Info, charset, maxMessageSize)
def error(logger: Logger, charset: Charset = StandardCharsets.UTF_8, maxMessageSize: Int = 8192): OutputStream =
apply(logger, Level.Error, charset, maxMessageSize)
def apply(logger: Logger, level: Level.Value, charset: Charset = StandardCharsets.UTF_8, maxMessageSize: Int = 8192): OutputStream =
new LoggerOutputStream(logger, level, charset, maxMessageSize)
@steinybot
steinybot / Test.scala
Last active October 10, 2017 01:36
Surprising implicit resolutions with package objects
package com.example
class Test {
// This works
(_: Int).fooImplicit
}
@steinybot
steinybot / FixingVarianceErrors.scala
Created October 11, 2017 19:50
How to convince the compile that the variance is safe
object FixingVarianceErrors {
trait InvariantThing[T]
trait CovariantThing[+T] {
// Fails to compile with error:
// Error:(12, 9) covariant type T occurs in invariant position in
// type => FixingVarianceErrors.InvariantThing[T] of method thing1
def thing1: InvariantThing[T]
@steinybot
steinybot / git-inserts-deletes.sh
Last active January 25, 2018 19:58
Git insertions and deletions
git log --shortstat --format="%an" | sed '/^$/d' | awk '
BEGIN { FS = ", " }
{
if (match($1, /^ [0-9]+ files? changed/)) {
split($2, a, " ")
inserts = (a[1] == "")? 0 : a[1]
split($3, b, " ")
deletes = (b[1] == "")? 0 : b[1]
total = (inserts - deletes)
print name " " inserts " " deletes " " total
@steinybot
steinybot / Entities.scala
Last active February 2, 2018 00:00
Understanding Slick Projections
case class Inner(a: String, b: Int)
case class Outer(a: String, i: Inner)
@steinybot
steinybot / CoproductImplicits.scala
Last active March 7, 2018 04:42
Play Json Reads and Writes for Coproducts
package com.geneious.nucleus.service.util.play.json
import play.api.libs.json._
import shapeless._
trait CoproductFormats {
implicit val cNilReads: Reads[CNil] = Reads(_ => JsError())
implicit val cNilWrites: Writes[CNil] = Writes(_ => JsNull)
@steinybot
steinybot / Bob.txt
Created March 30, 2018 00:58
This is a markdown test
Bob
@steinybot
steinybot / Scratch.java
Created March 28, 2019 05:19
Check if the operating system is 64-bit
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Kernel32Util;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
class Scratch {
public static void main(final String[] args) {
System.out.println("Is 64-bit OS? " + is64BitOS());