Skip to content

Instantly share code, notes, and snippets.

View cb372's full-sized avatar

Chris Birchall cb372

View GitHub Profile
@cb372
cb372 / joda.scala
Created May 4, 2016 15:27
Confusing default behaviour of Joda DateTime parsing
/*
* By default Joda will parse an ISO 8601 string into a DateTime
* with its timezone set to the _local_ timezone, not the one specified in the ISO string.
* You can fix this by calling `withOffsetParsed()` on your formatter.
*/
scala> import org.joda.time.format._
import org.joda.time.format._
scala> new DateTime("2016-04-06T15:22:01Z")
scala> def foo(a: Int): Future[Int] = memoize { Future { println("Computing..."); a + 1 } }
foo: (a: Int)scala.concurrent.Future[Int]
scala> foo(123)
res0: scala.concurrent.Future[Int] = List() // <-- WTF
scala> Computing...
/*
* Experiment into making ScalaCache generic in the serialization target.
*
* The idea is that different cache implementations can specify different serialization targets.
* Memcached/Redis would have Repr = Array[Byte],
* while in-memory caches (which don't need serialization) would have Repr = NoSerialization.
*
* The serialization target is encoded as a type parameter in `Cache` and `ScalaCache`.
* I tried it using a type member instead, but it turned into a bloodbath
* (you can see the first steps in https://gist.github.com/cb372/b53a0b51974f78212d3e06bdc682e977)
@cb372
cb372 / Demo.scala
Created April 8, 2016 11:50
Dependently typed implicit codecs for ScalaCache
/*
* Dependently typed implicit codecs for ScalaCache.
*
* Based on the encoding trick in https://gist.github.com/milessabin/cadd73b7756fe4097ca0
* to get around the need for multiple implicit parameter lists.
*
* The idea is that different cache implementations can specify different codec targets.
* Memcached/Redis would have CodecTarget = Array[Byte],
* while in-memory caches (which don't need serialization) would have CodecTarget = Dummy.
*
@cb372
cb372 / gist:5e442257f56b97271062
Created March 14, 2016 11:05
Desugaring in REPL in Scala 2.11.8
$ sbt ++2.11.8 consoleQuick
[info] Loading global plugins from /Users/cbirchall/ghar/github-cb372/.sbt/0.13/plugins
[info] Set current project to cbirchall (in build file:/Users/cbirchall/)
[info] Setting version to 2.11.8
[info] Reapplying settings...
[info] Set current project to cbirchall (in build file:/Users/cbirchall/)
[info] Updating {file:/Users/cbirchall/}cbirchall...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Starting scala interpreter...
@cb372
cb372 / query.json
Created March 9, 2016 18:40
Using the Elasticsearch scroll API
{
"sort": ["_doc"],
"size": 100,
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
...
import play.api.mvc.Security
object AuthAction extends Security.AuthenticatedBuilder[User](...)
type AuthReq = Security.AuthenticatedRequest[_, User]
object AuditLogAction extends ActionFunction[AuthReq, AuthReq] {
def invokeBlock[A](request: AuthReq[A], block: AuthReq[A] => Future[Result]): Future[Result] = {
/*
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
// Nameable is now a typeclass, not an interface
trait Nameable[P] {
def getName(p: P): String
@cb372
cb372 / Bar.scala
Created August 26, 2015 15:09
Example Thrift model and Scrooge-generated code
/**
* Generated by Scrooge
* version: 3.16.3
* rev: b8593c83072d94fc44feaa8d97940b9266d84ed0
* built at: 20140806-054445
*/
package com.mycorp
import com.twitter.scrooge.{
TFieldBlob, ThriftException, ThriftStruct, ThriftStructCodec3, ThriftStructFieldInfo, ThriftUtil}
[chris@ChracBookAir 1074 ~/tmp]
: cd foo
cd: no such file or directory: foo
[chris@ChracBookAir 1075 ~/tmp]
: fuck
mkdir -p foo && cd foo
[chris@ChracBookAir 1076 ~/tmp/foo]
: