Skip to content

Instantly share code, notes, and snippets.

Hao Ren invkrh

Block or report user

Report or block invkrh

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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 / 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
val rdd2 = + 1).cache
rdd1.unpersist() // rdd2 in storage
val df1 = Seq((1,2),(1,2),(1,2),(1,2)).toDF("key", "value").cache
val df2 ='key, 'value + 1 as "inc").cache
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)).
mapValues(v => mutable.HashSet() ++
val tagDict2 = Map[String, mutable.HashSet[Product]](
invkrh / pipe.scala
Created Apr 16, 2019
Implement spark.rdd.pipe
View pipe.scala
val command = "/tmp/"
val proc = Runtime.getRuntime.exec(Array(command))
new Thread("stderr reader for " + command) {
override def run() {
for(line <- Source.fromInputStream(proc.getErrorStream).getLines)
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
View callByName.scala
class A {
def say() = println(this.hashCode())
def fun(a: => A) = {
fun(new A)
View poisson.scala
import scala.util.Random
object Bootstrap {
def generate[T](seed: T, numBootStraps: Int): Iterator[Int] = {
val rng = new Random(seed.hashCode())
(0 until numBootStraps)
.map(_ => samplePoisson(rng, 1.0))
View ActroMessage.scala
package me.invkrh.raft.core
import{Actor, ActorSystem, Props}
case class Holder(arr: Array[Int])
class AddrChecker extends Actor {
override def receive: Receive = {
case h: Holder =>
println("In: " + h.toString)
You can’t perform that action at this time.