This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lshack | |
import scala.concurrent.{ ExecutionContext, Future } | |
import scala.languageFeature.higherKinds | |
import scala.util.{ Success, Try } | |
import lshack.Values._ | |
object Values { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Sanitizing { | |
case class NonEmptyString(s: String) { | |
require(s.nonEmpty) | |
} | |
type EmptiableStringMagnet = Either[String, Option[String]] | |
} | |
trait Sanitizing { | |
import Sanitizing._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 { | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer