Skip to content

Instantly share code, notes, and snippets.

View cb372's full-sized avatar

Chris Birchall cb372

View GitHub Profile
@cb372
cb372 / Server.scala
Created May 17, 2012 11:59
Redis server in Finagle
package com.twitter.finagle.redis
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder}
import java.net.InetSocketAddress
import protocol._
import collection.mutable.{ Map => MMap }
import java.util.concurrent.Executors
import com.twitter.util.{FuturePool, Future}
@cb372
cb372 / Foo.java
Created July 6, 2012 04:08
Grepping for Java casts
public class Foo {
public A a = new B();
public B b = (B) a;
}
class A {
}
class B extends A {
}
@cb372
cb372 / compile_error.java
Created July 16, 2012 15:05
Project Lombok bug 399 - @ExtensionMethod fails unless extensions class is in root package
package com.github.cb372.lombok; // <-- Note: not the root package
import lombok.experimental.ExtensionMethod;
@ExtensionMethod({ Extensions.class })
public class Main {
public static void main(String[] args) {
System.out.println("Hello, %s".format("World.")); // => "Hello, World."
}
}
def add1(arr: Array[Int], value: Int, n: Int): Array[Int] = {
n match {
case 0 => loop(arr, value, 0, 1, {_ < arr.length}, arr.length)
case m if m > 0 => loop(arr, value, 0, 1, {_ < arr.length}, m)
case m if m < 0 => loop(arr, value, arr.length - 1, -1, {_ > 0}, -m)
}
arr
}
private def loop(arr: Array[Int], value: Int, startIndex: Int, step: Int, continueCond: Int => Boolean, maxIncr: Int) {
@cb372
cb372 / GuavaServiceTest.java
Created August 15, 2012 00:36
Can I use Guava's AbstractExecutionThreadService for services that need to be interrupted?
package chris;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import com.google.common.util.concurrent.Service;
import org.junit.Test;
import java.util.concurrent.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@cb372
cb372 / index.html
Created November 25, 2012 05:09
Call for Speakers English translation
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Scala Conference in Japan 2013</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="日本で初のScala言語のカンファレンスが開催されることになりました。">
<meta name="author" content="日本 Scala ユーザーズグループ">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="./bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
@cb372
cb372 / Fedis.md
Created December 15, 2012 07:43
Finagle Advent Calendar 2012 (http://connpass.com/event/1524/) - Day 15 - Fedis
@cb372
cb372 / MyFormatter.java
Created January 17, 2013 10:01
Example of using Flume HDFS sink with a custom SeqFileFormatter.
package com.foo;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.sink.hdfs.SeqFileFormatter;
import org.apache.flume.sink.hdfs.SeqFileFormatter.Record;
public class MyFormatter implements SeqFileFormatter {
@Override
@cb372
cb372 / init-gradle.sh
Last active December 15, 2015 20:00
Gradle boilerplate generator
#!/bin/sh
content=$(cat <<EOF
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'eu.appsatori:gradle-fatjar-plugin:0.2-rc1'
@cb372
cb372 / x-in-xs.txt
Last active December 18, 2015 16:39
Python-style "x in xs" testing in Scala using implicit conversion and duck typing. Sinful but convenient.
scala> implicit class WithInMethod(obj: Any) {
| def in(a: {def contains(e: Any): Boolean}): Boolean = a.contains(obj)
| }
warning: there were 1 feature warnings; re-run with -feature for details
defined class WithInMethod
scala> "foo" in List("1", "2")
res3: Boolean = false
scala> "foo" in List("foo", "bar")