Skip to content

Instantly share code, notes, and snippets.

@kevinwright
kevinwright / gist:8505936
Last active January 3, 2016 18:59
ScalaOnIos.scala
implicit class IosInt(i: Int) extends AnyVal {
def s: Int = i + 1
}
val test: String = "I have an iPhone " + 4.s
block"""
| implicit def fnFromProduct${N}[A,..Z]: Aux[(A::..Z) => Res, (A,..Z) => Res] = new FnFromProduct[(A::..Z) => Res] {
| type Out = (A,..Z) => Res
| def apply(hf : (A::..Z) => Res): Out = (a:A,..z:Z) => hf(a::..z)
| }
"""
block"""
| implicit def fnFromProduct«N»[«A..Z»]: Aux[(«A::Z») => Res, («A..Z») => Res] = new FnFromProduct[(«A::Z») => Res] {
| type Out = («A..Z») => Res
| def apply(hf : («A::Z») => Res): Out = («a:A..z:Z») => hf(«a::z»)
| }
"""
@kevinwright
kevinwright / gist:8268045
Created January 5, 2014 13:16
string interpolation with "creatively" named values
// intended for boilerplate.scala in shapeless...
val arity = 3
val N = arity
val typeVars = (0 until arity) map (n => (n+'A').toChar)
// = Vector(A, B, C)
val `A..Z` = typeVars.mkString(", ")
// = "A, B, C"
@kevinwright
kevinwright / arch-firstboot.md
Last active October 25, 2023 08:28
Arch first-boot configuration. Setup the first user, sudo, vm tools, and enough of a dev env to support AUR+yaourt

OpenSSH daemon

pacman -S openssh
systemctl start sshd
systemctl enable sshd.socket

Sudoers

@kevinwright
kevinwright / arch-install-non-efi.md
Last active December 24, 2015 23:49
Arch install script for a minimal BIOS-based (as opposed to EFI) system. Gets as far as the first boot with network configured, and no users or packages installed beyond those necessary to complete the installation.

Phase 0: Enable ssh service

systemctl start sshd
passwd
ip addr

From this point on, SSH into the (dhcp-assigned) IP - it makes it easier to copy/paste

Phase 1: Prepare System

@kevinwright
kevinwright / ObjectToExpr.scala
Created March 21, 2013 15:29
Universal lifting macro - courtesy of Dmitry Grigoriev
object ObjectToExpr {
def objectToExpr[O <: AnyRef](c1: Context, o: O): c1.Expr[O] = macro objectToExprMacro[O]
def objectToExprMacro[O <: AnyRef : c.WeakTypeTag](c: Context)(c1: c.Expr[Context], o: c.Expr[O]) = c.Expr[Nothing] {
import c.universe._
// println("*** objectToExprMacro() started")
val mh = new MacroHelper[c.type](c)
def isConstant(t: Type) = t =:= typeOf[Boolean] || t =:= typeOf[String] || t =:= typeOf[Int]
def isNonAbstractCaseClass(t: Type) = {
@kevinwright
kevinwright / gist:5188757
Created March 18, 2013 16:55
URL template extractor using scalatra routes
implicit class PagePatternHelper(val sc: StringContext) extends AnyVal {
def page(args: Any*): PagePattern = {
val template = sc.standardInterpolator(treatEscapes, args)
val templateUrl = rl.Uri(template)
val path = templateUrl.path
val routeMatcher = new SinatraRouteMatcher(path)
val queryMultiParams = rl.MapQueryString.parseString(templateUrl.query.rawValue)
val queryParams = queryMultiParams.mapValues(_.head)
@kevinwright
kevinwright / Signal.scala
Last active December 14, 2015 06:59
Signal and FocussedSignal
import akka.agent.Agent
import akka.util.Timeout
import akka.actor.ActorSystem
import concurrent.{Future, Promise}
import concurrent.duration.FiniteDuration
import shapeless.Lens
object Signal {
@kevinwright
kevinwright / FocusedAgent.scala
Created February 26, 2013 14:14
FocusedAgent
import akka.agent.Agent
import shapeless.Lens
import concurrent.Future
import akka.util.Timeout
import concurrent.ExecutionContext
object FocusedAgent {
implicit class EnrichedAgent[A](val agent: Agent[A]) extends AnyVal {
def focusOn[B](lens: Lens[A,B]): FocusedAgent[A,B] = new FocusedAgent[A,B](agent, lens)
}