Created
May 24, 2013 14:00
-
-
Save romanoid/5643736 to your computer and use it in GitHub Desktop.
code dump from presentation @ JEEConf 2013
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 templates | |
| import templates.FancyCode | |
| object Demo extends App { | |
| Console.println(Range(0, 12)) | |
| implicit class RangeBuilder(val start: Int) { | |
| def till(end: Int) = { | |
| Range(start, end + 1) | |
| } | |
| } | |
| Console.println(0 till 12) | |
| // implicit class FactorialCounter(val source:Int) { | |
| // def ! = { | |
| // (2 to source).foldLeft(BigInt(1))( (a:BigInt, b:Int) => a*b) | |
| // } | |
| // } | |
| //implicit class FactorialCounter(val source:Int) { | |
| // def ! = { | |
| // this | |
| // } | |
| // | |
| // lazy val compute = { | |
| // (2 to source).foldLeft(BigInt(1))( (a:BigInt, b:Int) => a*b) | |
| // } | |
| //} | |
| //implicit def computeFac(fac: FactorialCounter):BigInt = { | |
| // fac.compute | |
| //} | |
| class LazyFactorial(val numerators: Vector[Int], val denominators: Vector[Int]) { | |
| def ! = this | |
| def this(singleFac: Int) = this(Vector(singleFac), Vector.empty) | |
| def *(that: LazyFactorial) = { | |
| new LazyFactorial(numerators ++ that.numerators, denominators ++ that.denominators) | |
| } | |
| def /(that: LazyFactorial) = { | |
| new LazyFactorial(numerators ++ that.denominators, denominators ++ that.numerators) | |
| } | |
| lazy val compute = FancyCode.computeFactorialCombination(numerators, denominators) | |
| } | |
| object LazyFactorial { | |
| def apply(singleFac: Int) = { | |
| new LazyFactorial(singleFac) | |
| } | |
| def apply(numerators: Vector[Int], denominators: Vector[Int]) = { | |
| new LazyFactorial(numerators, denominators) | |
| } | |
| } | |
| implicit def factorialConv(source: Int) = { | |
| LazyFactorial(source) | |
| } | |
| implicit def computeFac(fac: LazyFactorial): BigInt = { | |
| fac.compute | |
| } | |
| def binomialCoificient(n: Int, k: Int): BigInt = { | |
| n.! / (k.! * (n - k).!) | |
| } | |
| val i = System.currentTimeMillis() | |
| Console.println(binomialCoificient(5, 3)) | |
| println( binomialCoificient(Int.MaxValue, 1) ) | |
| println(System.currentTimeMillis()-i) | |
| } |
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 templates | |
| import scala.annotation.tailrec | |
| object FancyCode { | |
| def computeFactorialCombination(numerators: List[Int], denominators: List[Int]): BigInt = | |
| computeFactorialCombination(numerators.toVector, denominators.toVector) | |
| def computeFactorialCombination(numerators: Vector[Int], denominators: Vector[Int]): BigInt = { | |
| val num = numerators.sortBy(-_).toList | |
| val denom = denominators.sortBy(-_).toList | |
| @tailrec | |
| def compute(accnum: List[Range], accdenom: List[Range], numleft: List[Int], denomleft: List[Int]): (List[Range], List[Range]) = { | |
| if (numleft.isEmpty || denomleft.isEmpty) { | |
| (numleft.map(1 to _) ::: accnum, denomleft.map(1 to _) ::: accdenom) | |
| } else if (numleft.head > denomleft.head) { | |
| compute((denomleft.head + 1 to numleft.head) :: accnum, accdenom, numleft.tail, denomleft.tail) | |
| } else { | |
| compute(accnum, (numleft.head + 1 to denomleft.head) :: accdenom, numleft.tail, denomleft.tail) | |
| } | |
| } | |
| def multiplyAll(source: List[Range]) = { | |
| source.map(_.foldLeft(BigInt(1))((a, b) => a * b)).fold(BigInt(1))((a, b) => a * b) | |
| } | |
| val computed = compute(List.empty, List.empty, num, denom) | |
| multiplyAll(computed._1) / multiplyAll(computed._2) | |
| } | |
| } |
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 templates | |
| object ParametersDemo extends App{ | |
| trait CacheFriendly { | |
| def size:Int | |
| } | |
| trait SizeCounter[T] { | |
| def sizeOf (measured: T): Int | |
| } | |
| case class Person(fistName: String, secondName : String, sex: Char) { | |
| } | |
| class OurSmartCache { | |
| def get(key: String ):Object = { | |
| null | |
| } | |
| def add(key:String, input: Any, priority:Int){ | |
| //doStuff | |
| println(s"${input} added with priority=${priority}") | |
| } | |
| def add(key: String, input: CacheFriendly) { | |
| add(key, input, input.size) | |
| } | |
| def add[T](key:String, input: T)(implicit sizeCounter: SizeCounter[T]){ | |
| add(key, input, sizeCounter.sizeOf(input)) | |
| } | |
| } | |
| implicit val humanPrioritizer: SizeCounter[Person] = new SizeCounter[Person]() { | |
| def sizeOf(input: Person): Int = input.fistName.length + input.secondName.length + 1 | |
| } | |
| implicit val stringSc: SizeCounter[String] = new SizeCounter[String] { | |
| def sizeOf(input: String): Int = input.length | |
| } | |
| implicit def convertToCounter[T](conv: T => Int): SizeCounter[T] = { | |
| new SizeCounter[T] { | |
| def sizeOf(measured: T): Int = conv(measured) | |
| } | |
| } | |
| val cache = new OurSmartCache() | |
| cache.add("key" , new Person("John", "SmartAss", 'M')) | |
| cache.add("key2", " this is just a string") | |
| cache.add("key3", 3)((x: Int) => x) | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by the way, in last example function "convertToCounter" is 'how not to do'-example