Skip to content

Instantly share code, notes, and snippets.

@joescii
joescii / IfBrowserIs
Created May 8, 2014 18:53
IE9 awareness in Lift
object IfBrowserIs {
def ie9 = if(S.request.map(_.isIE9).openOr(false)) PassThru else ClearNodes
}
@japgolly
japgolly / DependencyLib.scala
Last active September 24, 2015 18:16
SBT JVM/JS DependencyLib
import sbt._
import scala.languageFeature._
import org.scalajs.sbtplugin.ScalaJSPlugin
import ScalaJSPlugin.autoImport._
object DependencyLib {
sealed trait HasDialect
sealed trait HasJvm extends HasDialect
sealed trait HasJs extends HasDialect
// Define the following traits and companion object
// It's in Rapture Core (https://github.com/propensive/rapture-core) if you don't want to
trait LowPriorityDefaultsTo { implicit def fallback[T, S]: DefaultsTo[T, S] = null }
object DefaultsTo extends LowPriorityDefaultsTo { implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null }
trait DefaultsTo[T, S]
// Then, assuming we want to specify a default for a type class like `Namer`,
case class Namer[T](name: String)
@milessabin
milessabin / gist:25b7b669b5a9ac051a71
Created June 5, 2015 14:32
A safe ADT+shapeless drop-in replacement for the unsafe standard Scala Enumeration ...
// An ADT+shapeless as a drop-in replacement for a standard Scala Enumeration.
//
// First the unsafe standard Scala Enumeration ...
//
object ScalaEnumDemo extends App {
// Example from scala.Enumeration scaladoc. Terse ...
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
@noelmarkham
noelmarkham / imports.md
Last active January 5, 2017 18:00
Cats imports cheat sheet
import... What it imports
cats.std.type Typeclass instances for standard library type (think List, Option)
cats.syntax.type “Enhanced” methods for type (.toRightXor etc)
cats.data.type Imports a type not part of the standard library (think Xor, Kleisli) and its typeclass instances
import net.liftweb.common._
import net.liftweb.http.LiftRules._
import net.liftweb.http.rest.RestHelper
import net.liftweb.http.{S, JsonResponse, LiftResponse, Req}
import net.liftweb.json.JsonDSL._
import scala.concurrent.duration.Duration
object RateLimit {
@milessabin
milessabin / gist:aae285025a32fac0f5c1
Last active August 26, 2017 10:28
Trivial type safe heterogenous map using only dependent method types, singleton-typed String literal keys and implicits.
scala> trait Assoc[K] { type V ; val value: V }
defined trait Assoc
scala> def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } =
| new Assoc[k.type] { type V = V0 ; val value = v }
mkAssoc: [V0](k: String, v: V0)Assoc[k.type]{type V = V0}
scala> implicit def nameAssoc = mkAssoc("Name", "Mary")
nameAssoc: Assoc[String("Name")]{type V = String}
@janlay
janlay / update-repos.sh
Last active April 16, 2019 13:08
Updates all Git repos in directory, defaults to Vim bundles.
#!/bin/bash
# author: janlay@gmail.com
WORKDING_DIR="${1-$HOME/.vim/bundle}"
GIT_DIR_ROOT="$HOME/Workspace/.gitrepo"
echo "Working on $WORKDING_DIR"
for i in `find "$WORKDING_DIR" -mindepth 1 -maxdepth 1 -type d`; do
REPO_NAME="${i##*/}"
export GIT_WORK_TREE="$WORKDING_DIR/$REPO_NAME"
// Alternative to sealed abstract case class pattern for Scala 2.12.2+
// Benefits:
// - 1 final class instead of 1 sealed class + anonymous subclass
// - portable to Scala 3 regardless of opaque types
// - less boilerplate
final case class Angle private (toDegrees: Int) {
// Define our own `copy` method to suppress synthetic one
// Add private to prevent it from being used
def copy(degrees: Int = toDegrees): Angle = Angle.fromDegrees(degrees)
@noelwelsh
noelwelsh / Nullable.scala
Created April 17, 2015 10:38
Nullable types in Scala
object Nullable {
sealed trait NullableTag
type Nullable[A] = A with NullableTag
def nullAs[B]: Nullable[B] =
null.asInstanceOf[Nullable[B]]
implicit class ToNullable[A](val a: A) extends AnyVal {
def `?`: Nullable[A] = a.asInstanceOf[Nullable[A]]
}