Skip to content

Instantly share code, notes, and snippets.

View lhohan's full-sized avatar

Hans L'Hoëst lhohan

View GitHub Profile
@lhohan
lhohan / new_gist_file
Created September 2, 2013 08:39
File copy using Java7 & Scala
Java 7 is now out and you have another option: java.nio.file.Files.copy. The probably easiest solution (And with Scalas superior import even easier). Provided that from and to are strings as in your question:
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files.copy
import java.nio.file.Paths.get
implicit def toPath (filename: String) = get(filename)
copy (from, to, REPLACE_EXISTING)
@lhohan
lhohan / new_gist_file
Created September 2, 2013 10:52
helper "use" method using currying for automatic resource management From http://stackoverflow.com/questions/2225214/scala-script-to-copy-files
If you really want to do it yourself instead of using a library like commons-io, you can do the following in version 2.8. Create a helper method "use". It will give you a form of automatic resource management.
def use[T <: { def close(): Unit }](closable: T)(block: T => Unit) {
try {
block(closable)
}
finally {
closable.close()
}
}
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 )
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 \""       
def shouldThrow[E<:Throwable](f: =>Unit) { try { f fail("should have thrown an Exception") } catch { case e:E => () case e => throw e } }
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
def fibonacciStream(n: Int) = fibFrom(1, 1).take(n).toList.last
@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 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"
// 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"))
@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])