Skip to content

Instantly share code, notes, and snippets.

View kolemannix's full-sized avatar

Koleman Nix kolemannix

  • patterndata.ai
  • Richmond, VA
View GitHub Profile
@kolemannix
kolemannix / TaggedPlayJsonExample.scala
Last active November 15, 2016 13:58
Dynamically sanitizing Strings (XSS concerns) based on type tags
import scalaz.{ @@, Tag }
import org.apache.commons.lang.StringEscapeUtils
import org.joda.time.DateTime
import play.api.libs.json._
trait DomRenderable
def RenderableString(s: String): String @@ DomRenderable = Tag[String, DomRenderable](s)
@kolemannix
kolemannix / CoordinateGen.scala
Last active March 9, 2017 18:43
ScalaCheck Polymorphic Generators for Latitude and Longitude
import org.scalacheck.Gen._
def latitudeGen[T](implicit num: Numeric[T], c: Choose[T]): Gen[T] = {
import num._
val min = fromInt(-90)
val max = fromInt(90)
Gen.sized(n => c.choose(min, max))
}
def longitudeGen[T](implicit num: Numeric[T], c: Choose[T]): Gen[T] = {
@kolemannix
kolemannix / ImplicitIsos.scala
Created April 11, 2017 15:38
Implicit isomorphisms with ScalaZ
import scalaz.Isomorphism.<=>
/** Our example Isomorphism */
trait StringIntIso {
implicit object StringIntIso extends (String <=> Int) {
override val to: (String) => Int = _.toInt
override val from: (Int) => String = _.toString
}
@kolemannix
kolemannix / phantom-and-f-bound.scala
Last active August 3, 2017 21:33
Phantom Types, higher kinds, and F-Bounded polymorphism PoC (abstraction over en/decryption
package lshack
import scala.concurrent.{ ExecutionContext, Future }
import scala.languageFeature.higherKinds
import scala.util.{ Success, Try }
import lshack.Values._
object Values {
@kolemannix
kolemannix / supertrim.scala
Created August 29, 2017 21:50
NonEmpty String trim example (Scala)
object Sanitizing {
case class NonEmptyString(s: String) {
require(s.nonEmpty)
}
type EmptiableStringMagnet = Either[String, Option[String]]
}
trait Sanitizing {
import Sanitizing._
@kolemannix
kolemannix / wrapperjsonformat.scala
Created September 25, 2017 16:00
Wrapper-type Play JSON format automatic derivation using shapeless
import play.api.libs.json._
import shapeless.ops.hlist.IsHCons
import shapeless.{ Generic, HList, HNil }
/**
* Typeclass for automatically deriving a [play.api.libs.json.Format] instance for a type `A`.
* Instances will exist if A is a single-member case class containing a Formattable field
*/
trait WrapperJsonFormat[A] {
def jsonFormat: Format[A]
@kolemannix
kolemannix / safeser_full.sc
Last active November 21, 2018 18:38
Code from post "A practical type class example: SafeSerializable"
#!/usr/bin/env amm
@scala.annotation.implicitNotFound("No Serializer provided for ${A} in scope.")
trait SafeSerializable[A] {
def serialize(a: A): Array[Byte]
def deserialize(bytes: Array[Byte]): A
}
object SafeSerializable {
/**
@kolemannix
kolemannix / intro_typeclasses.sc
Created November 21, 2018 17:23
Code from: "An introduction to typeclasses in Scala"
#!/usr/bin/env amm
// Snippet 1
@scala.annotation.implicitNotFound("No Showable[A] in scope for A = ${A}. Try importing or defining one.")
trait Showable[A] {
def show(a: A): String
}
// Snippet 2
object Showable {
def make[A](showFn: A => String): Showable[A] = new Showable[A] { override def show(a: A): String = showFn(a) }
@kolemannix
kolemannix / ID.scala
Created February 23, 2023 17:10
Timestamp-based 64-bit IDs with sharding and random elements
import scala.util.Random
object IdGeneration {
private[id] val OUR_EPOCH = 1546344000000L // 2019-01-01T12:00:00Z
private[id] val FIFTEEN_BIT_MAX: Int = 0x8000
private def randomComponent15Bit(): Int = {
Random.nextInt(FIFTEEN_BIT_MAX)
}
@kolemannix
kolemannix / Aoc_2022_day6_simple.scala
Created August 8, 2023 19:55
Aoc_2022_day6_simple
def func(s: String, n: Int) = {
val index = (0 until s.length).find { i =>
s.slice(i, i + n).distinct.length == n
}
index.map(_ + n)
}
def part1(s: String): Option[Int] = func(s, 4)
def part2(s: String): Option[Int] = func(s, 14)