Skip to content

Instantly share code, notes, and snippets.

View stanch's full-sized avatar

Nick stanch

  • Product Manager at @snowplow
  • Lisbon, Portugal
View GitHub Profile
val l2d = Map('i' -> 1, 'v' -> 5, 'x' -> 10, 'l' -> 50, 'c' -> 100)
def compute(ds: Seq[Int]) = ds.foldRight((0, 0)) {
case (d, (acc, last)) => if (d < last) (acc - d, last) else (acc + d, d)
}
def roman(numeral: String) = compute(numeral map l2d)._1
@stanch
stanch / SealedTest.scala
Created January 24, 2016 14:39
Extending Quicklens (https://github.com/adamw/quicklens) to support sealed traits
case class Grand(parent: Parent)
sealed trait Parent { def x: Int }
case class Child1(x: Int) extends Parent
case class Child2(x: Int) extends Parent
val grand = Grand(Child1(3))
it should "modify a field in a sealed trait" in {
modify(grand)(_.parent.x).using(_ + 1) should be (Grand(Child1(4)))
}
@stanch
stanch / AkkaHttp.scala
Last active January 21, 2016 18:40
Demo fragments for my talk “What can we learn from composable DSLs?”
object Utils {
val disableCache = respondWithHeaders(
`Cache-Control`(`no-cache`, `max-age`(0), `must-revalidate`, `no-store`),
`Expires`(DateTime.MinValue)
)
}
object BooksRoutes {
val routes = path("books" / Segment) { id =>
complete(id)
@stanch
stanch / ScalaJSExample.scala
Last active January 20, 2016 01:50
Scala.js template for scala-js-fiddle
import scalatags.JsDom.all._
object ScalaJSExample extends js.JSApp {
def main() = {
}
}
@stanch
stanch / config.fish
Created January 18, 2016 23:20
My entire fish shell configuration
# In ~/.config/fish/config.fish
set fish_greeting
set __fish_git_prompt_showdirtystate 'yes'
@stanch
stanch / classdiag.py
Last active January 14, 2016 17:52
A quick-n-dirty way to convert Scala case class definitions into http://yuml.me/diagram/plain/class/draw class diagrams
#!/usr/bin/env python3
import os
import re
import sys
import fnmatch
entities = {}
relations = {}
// some other android imports here
...
import android.support.v4.app.{ Fragment, FragmentManager, FragmentPagerAdapter }
import android.support.v4.view.ViewPager
import com.viewpagerindicator.TitlePageIndicator
import macroid._
import macroid.FullDsl._
import macroid.contrib._
import macroid.contrib.Layouts._
@active
class A {
// all non-@active locals
// will be made private
var y = 0
def bar(x: Int) = x + 5
@active // should return Unit
def foo(x: Int) = {
y = bar(x)
import android.content.Context
import android.util.Log
import android.view.{View, ViewGroup}
import macroid.AppContext
import macroid.MediaQueries._
object Spec {
def apply(size: Int, mode: Int) =
View.MeasureSpec.makeMeasureSpec(size, mode)
// -- in the activity:
var navSlot = slot[ListView]
var drawerSlot = slot[DrawerLayout]
// -- in onCreate:
// ListView tweaks
def checkItem(pos: Int) = Tweak[ListView](_.setItemChecked(pos, true))
val singleNoDivider = Tweak[ListView] { lv ⇒