Skip to content

Instantly share code, notes, and snippets.

Avatar

Hao Ren invkrh

View GitHub Profile
@invkrh
invkrh / bet.scala
Last active Nov 11, 2019
Useful code for calculate bet size
View bet.scala
def kellyFactor(pWin: Double, odd: Double) = {
(pWin * odd - 1) / (odd - 1)
}
def betSize(
bankroll: Double,
minOdd: Double,
odd: Double) = {
val pWin = 1.02 / minOdd
val kf = kellyFactor(pWin, odd)
@invkrh
invkrh / ScalaTraining.scala
Created Oct 29, 2019
Note of Scala training session at Criteo
View ScalaTraining.scala
trait Name {
def it: String
}
/** no need for explicit import */
object Name {
implicit val criteo = new Name { val it = "asdf" }
}
object Live extends App {
View mutliple-inheritance.py
class Type(type):
def __repr__(cls):
return cls.__name__
class O(object, metaclass=Type): pass
class A(O): pass
class B(O): pass
class C(O): pass
class D(O): pass
class E(O): pass
class K1(A, B, C): pass
View multiple-inheritance.scala
class Animal { def a = 1 }
trait Furry extends Animal {
override def a = 2
}
trait HasLegs extends Animal {
override def a = super.a
}
trait FourLegged extends HasLegs
class Cat extends Animal with Furry with FourLegged
@invkrh
invkrh / count.scala
Last active Jul 10, 2019
Spark Puzzles
View count.scala
val rdd1 = sc.makeRDD(Seq((1,2),(1,2),(1,2),(1,2))).cache
rdd1.count
val rdd2 = rdd1.map(_._1 + 1).cache
rdd2.count
rdd1.unpersist() // rdd2 in storage
val df1 = Seq((1,2),(1,2),(1,2),(1,2)).toDF("key", "value").cache
df1.count
val df2 = df1.select('key, 'value + 1 as "inc").cache
df2.count
View immutableMap.scala
case class Product(id: Int, tags: Set[String])
val productList = Array(Product(0, Set("a")), Product(1, Set("a", "b")))
val tagDict1: Map[String, mutable.HashSet[Product]] = productList.
flatMap(p => p.tags.map(_ -> p)).
groupBy(_._1).
mapValues(v => mutable.HashSet() ++ v.map(_._2))
val tagDict2 = Map[String, mutable.HashSet[Product]](
@invkrh
invkrh / pipe.scala
Created Apr 16, 2019
Implement spark.rdd.pipe
View pipe.scala
import scala.io.Source
import java.io.PrintWriter
val command = "/tmp/cmd.sh"
val proc = Runtime.getRuntime.exec(Array(command))
new Thread("stderr reader for " + command) {
override def run() {
for(line <- Source.fromInputStream(proc.getErrorStream).getLines)
System.err.println(line)
}
@invkrh
invkrh / utf8.scala
Created Jan 2, 2019
Function convert string to UTF-8 binary string
View utf8.scala
import java.nio.charset.StandardCharsets
def encode(s: String): Array[String] = {
StandardCharsets.UTF_8.encode(s).array.filter(_ != 0).map(b => "%08d".format((b & 0xFF).toBinaryString.toInt))
}
View leetcode_3sum.txt
[82597,-9243,62390,83030,-97960,-26521,-61011,83390,-38677,12333,75987,46091,83794,19355,-71037,-6242,-28801,324,1202,-90885,-2989,-95597,-34333,35528,5680,89093,-90606,50360,-29393,-27012,53313,65213,99818,-82405,-41661,-3333,-51952,72135,-1523,26377,74685,96992,92263,15929,5467,-99555,-43348,-41689,-60383,-3990,32165,65265,-72973,-58372,12741,-48568,-46596,72419,-1859,34153,62937,81310,-61823,-96770,-54944,8845,-91184,24208,-29078,31495,65258,14198,85395,70506,-40908,56740,-12228,-40072,32429,93001,68445,-73927,25731,-91859,-24150,10093,-60271,-81683,-18126,51055,48189,-6468,25057,81194,-58628,74042,66158,-14452,-49851,-43667,11092,39189,-17025,-79173,13606,83172,92647,-59741,19343,-26644,-57607,82908,-20655,1637,80060,98994,39331,-31274,-61523,91225,-72953,13211,-75116,-98421,-41571,-69074,99587,39345,42151,-2460,98236,15690,-52507,-95803,-48935,-46492,-45606,-79254,-99851,52533,73486,39948,-7240,71815,-585,-96252,90990,-93815,93340,-71848,58733,-14859,-83082,-75794,-82082,-24871,-15206,91207,-56469,-93618
View callByName.scala
class A {
def say() = println(this.hashCode())
}
def fun(a: => A) = {
a.say()
a.say()
}
fun(new A)
You can’t perform that action at this time.