Skip to content

Instantly share code, notes, and snippets.

// 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)
import monix.eval.Task
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
/** Request limiter for APIs that have quotas per second, minute, hour, etc.
*
* {{{
* // Rate-limits to 100 requests per second
* val limiter = TaskLimiter(TimeUnit.SECONDS, limit = 100)
*
@almost
almost / proposal.md
Last active September 12, 2019 09:07
Reactive 2016 Lightning Talk Proposal: Get Flow

This is a proposal for a lightning talk at the Reactive 2016 conference.

NOTE: If you like this, star ⭐ the Gist - the amount of stars decides whether it makes the cut! You could also Retweet if you want :)

Get Flow

Type checking JavaScript with Flow

JavaScript is a dynamic language, and there's nothing wrong with that. It allows quick iteration and lowers barriers. However, sometimes some compile-time type checking is just what you need to keep your code in line and give yourself the confidence to build bigger and faster. Flow gives the best of both worlds. You can have normal JavaScript but you can also add types where they're helpful, and it adds zero cost at runtime. In this talk I'll show Flow as it applies to a Redux & React codebase.

@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
@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"
@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
@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
}
@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]]
}
// 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)
@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
}