Skip to content

Instantly share code, notes, and snippets.

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.

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