Skip to content

Instantly share code, notes, and snippets.

View lhohan's full-sized avatar

Hans L'Hoëst lhohan

View GitHub Profile
import a.Handler
import a.Handler.CONST
class HandlerImpl extends Handler {
def doSomethingWithConst: String = {
CONST
}
}
@lhohan
lhohan / reflect.scala
Created June 1, 2016 13:30
Set any private field on any Scala or Java class
object Reflect {
implicit class Reflect(ref: AnyRef) {
/**
* Set the value of a (private) field.
*/
def setV(name: String, value: Any): Unit = {
val declaredField = ref.getClass.getDeclaredField(name)
declaredField.setAccessible(true)
declaredField.set(ref, value)
@lhohan
lhohan / avg.scala
Created June 1, 2016 07:23
One pass over collection to calculate average
def sumLength(xs: List[Int]) = xs.foldLeft(None:Option[(Int,Int)]){
case (None, x) => Some(x, 1)
case (Some((s:Int, l:Int)), x) => Some((s + x, l + 1 ))
}
def avg(xs: List[Int]): Option[Int] = sumLength(xs).map{case (sum, length) => sum / length}
val avg1 = avg(List(1, 2, 3, 4, 6, 7, 8, 9))
val avg2 = avg(List(1, 2, 3, 4, 5, 6, 7, 8, 9))
val avg3 = avg(List.empty[Int])
// helper method inspired by haskell, cycle a list infinitely,
def cycle(xs: List[String]): Stream[String] = Stream.continually(xs).flatten
val numbers = Stream.from(1)
// a infinite cycle of "", "", "Fizz"
val fizzes = cycle(List("", "", "Fizz"))
// a infinite cycle of "", "", "", "", "Buzz"
val buzzes = cycle(List("", "", "", "", "Buzz"))
def fizzbuzz(s: Int): List[String] = {
// helper method inspired by haskell, cycle a list infinitely,
def cycle(xs: List[String]): Stream[String] = Stream.continually(xs).flatten
val numbers = Stream.from(1)
// a infinite cycle of "", "", "Fizz"
val fizzes = cycle(List("", "", "Fizz"))
// a infinite cycle of "", "", "", "", "Buzz"
@lhohan
lhohan / fizzbuzztest
Last active August 29, 2015 13:56
fizzbuzz scalacheck test
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalacheck.Prop.forAll
import org.scalatest.prop.Checkers
class FizzBuzzTest extends FunSuite with Checkers {
test("fizzbuzz") {
val fb = fizzbuzz(100)
val range = Gen.choose[Int](1, 100)
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
def fibonacciStream(n: Int) = fibFrom(1, 1).take(n).toList.last
def shouldThrow[E<:Throwable](f: =>Unit) { try { f fail("should have thrown an Exception") } catch { case e:E => () case e => throw e } }
package org.foo.policy;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.List;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.BundleException;import org.osgi.framework.ServiceReference;import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;import org.osgi.service.condpermadmin.ConditionalPermissionInfo;import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;public class Activator implements BundleActivator{    public void start(BundleContext context) throws Exception    {      File policyFile = getPolicyFile(context);                                   List<String> encodedInfos = readPolicyFile(policyFile);          encodedInfos.add(0, "ALLOW {"                                                  + "[org.osgi.service.condpermadmin.BundleLocationCondition \""       
import java.io.{File,FileInputStream,FileOutputStream}
val src = new File(args(0))
val dest = new File(args(1))
new FileOutputStream(dest) getChannel() transferFrom(
new FileInputStream(src) getChannel, 0, Long.MaxValue )