This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import a.Handler | |
| import a.Handler.CONST | |
| class HandlerImpl extends Handler { | |
| def doSomethingWithConst: String = { | |
| CONST | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b) | |
| def fibonacciStream(n: Int) = fibFrom(1, 1).take(n).toList.last | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def shouldThrow[E<:Throwable](f: =>Unit) { try { f fail("should have thrown an Exception") } catch { case e:E => () case e => throw e } } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 \"" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ) |
NewerOlder