Skip to content

Instantly share code, notes, and snippets.

View p3t0r's full-sized avatar

Peter Maas p3t0r

  • Adevinta
  • Hilversum, the Netherlands
View GitHub Profile
import scala.util.control.Breaks._
breakable {
for(i <- 1 to 10) if (i==3) break else println(i)
}
Dir.glob("**/*.java").map{|it| it.split('/').last[0..-6]}.map{|it| [it, it.length]}.sort{|a, b| b[1] <=> a[1]}[0..5]
@p3t0r
p3t0r / scrabble
Created October 9, 2010 15:43
scrable scorer in scala
#!/usr/bin/env scala
!#
import io.Source
if(args.length < 1) { println("usage: ./scrabble <filename>"); System.exit(-1)}
Source.fromFile(args(0)).getLines.foreach(w => printf("%s,%d%n", w,score(w)))
def score(word:String) = word.foldLeft(0) (_ + scoreChar(_))
package com.log4p;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Scorer {
public static void main(String args[]) {
FileInputStream fis = null;
package com.log4p;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Scorer {
abstract class Repository[T] {
def withConnection(f: (Connection => Option[T])): Option[T]
}
class BigFailRepository extends Repository[String] {
override def withConnection(f: (Connection => Option[String])): Option[String] = {
throw new UnsupportedOperationException("BAM")
}
}
class SmallFailRepository extends Repository[String] {
var numFails = 0
override def withConnection(f: (Connection => Option[String])): Option[String] = {
trait Retrying[T] extends Repository[T] {
abstract override def withConnection(f: (Connection => Option[T])): Option[T] = retry(f)
private def retry(f: (Connection => Option[T]), times: Int = 3): Option[T] = {
try {
println("number of tries left: %d".format(times))
super.withConnection(f)
} catch {
case _ if times == 0 => None
case _ => retry(f, times - 1)
val bfr = new BigFailRepository with Retrying[String]
println("failed result: " + bfr.withConnection {conn => Option("failure result")})
val sfr = new SmallFailRepository with Retrying[String]
println("success result: " + sfr.withConnection {conn => Option("should fail first, succeed later")})
val nfr = new NoFailRepository with Retrying[String]
println("success result: " + nfr.withConnection {conn => Option("should not fail result")})
@p3t0r
p3t0r / ParameterInMethodNameExample.scala
Created April 28, 2011 19:25
example of encoding parameters in the methodname using the Dynamic trait
package com.log4.methodmissing
import java.lang.Integer
class ParameterInMethodNameExample extends Dynamic {
def find(by:String, value:String) = "select from users where %s = '%s'".format(by, value)
def find(by:String, value:Integer) = "select from users where %s = %s".format(by, value)
def delete(by:String, value:String) = "delete from users where %s = '%s'".format(by, value)
def delete(by:String, value:Integer) = "delete from users where %s = %s".format(by, value)