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 / Downloader.scala
Last active September 17, 2020 19:59
Akka Stream and HTTP download resource examples
import java.io.File
import akka.http.javadsl.model.headers.ContentDisposition
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.{HttpResponse, Uri}
import akka.stream.scaladsl._
import akka.stream.{Graph, Materializer, SinkShape}
import akka.util.ByteString
@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 / OneToOneOnDemandSink.scala
Last active February 27, 2021 18:51
A custom graph stage that materializes a new sink for every element
import akka.NotUsed
import akka.stream.{Attributes, Inlet, SinkShape}
import akka.stream.scaladsl.{Sink, Source}
import akka.stream.stage._
class OneToOneOnDemandSink[T, +M](sink: T => Sink[T, M]) extends GraphStage[SinkShape[T]] {
val in: Inlet[T] = Inlet("OneToOneOnDemandSink.in")
override val shape = SinkShape(in)
@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)