Skip to content

Instantly share code, notes, and snippets.

View hussachai's full-sized avatar

Hussachai Puripunpinyo hussachai

View GitHub Profile
import scala.annotation.tailrec
import java.io.File
object FirstHomework extends App {
//homework sum 1 to n using recursion
def sumN(n: Int) = {
@tailrec
def sum(c: Int, acc: Int): Int = {
if(c == 0) acc
import scala.util.Try
object FirstWeek extends App {
lazy val lazy1 = 1 //evaluate once when needed
def lazy2 = 2 //evaluate when called
val eager = 3 //evaluate immediately
object Dog {
def name = {
var socket = null
function subscribe(channel) {
var subscribeUrl = jsRoutes.controllers.Application.subscribe().webSocketURL() + "?channel=" + channel
console.log("Subscribed to: " + subscribeUrl)
socket = new WebSocket(subscribeUrl)
socket.onmessage = function(event) {
$("#messages" ).prepend("<div>" + event.data + "</div>")
}
socket.onclose = function(event) {
console.log(event)
class SubscribeActor(redis: RedisClient, out: ActorRef,
channels: Seq[String] = Nil, patterns: Seq[String] = Nil) extends RedisSubscriberActor(
new InetSocketAddress(redis.host, redis.port), channels, patterns,
onConnectStatus = connected => { println(s"connected: $connected") }) {
def onMessage(message: Message) {
out ! message.data.decodeString("UTF-8")
}
def onPMessage(pmessage: PMessage) {}
class Application extends Controller {
...
implicit val akkaSystem = akka.actor.ActorSystem()
val redis = RedisClient()
def index = Action { implicit request =>
Ok(views.html.pubsub())
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@BenchmarkMode({Mode.Throughput})
╔══════╦══════╦════════╗
║ Col1 ║ Col2 ║ NumCol ║
╠══════╬══════╬════════╣
║ MMM ║ MMM ║ MMM ║
║ 111 ║ 111 ║ 111 ║
║ AAA ║ AAA ║ AAA ║
╚══════╩══════╩════════╝
Filter then sort then map then collect
+----------+-------------+---------+-------+
| Language | Benchmark | Score | Error |
+----------+-------------+---------+-------+
| Java | Collections | 13,615 | 255 |
| Java | Streams | 11,861 | 213 |
| Scala | Collections | 45,009 | 805 | <-- (3)
| Scala | Streams | 56,409 | 882 | <-- (2)
| Scala | Views | 139,639 | 2,875 | <-- (1)
+----------+-------------+---------+-------+
Find first
+----------+-------------+------------+-----------+
| Language | Benchmark | Score | Error |
+----------+-------------+------------+-----------+
| Java | Collections | 80,444,836 | 1,358,358 | <-- (1)
| Java | Streams | 26,209,187 | 469,668 |
| Scala | Collections | 65,145,791 | 2,699,582 | <-- (3)
| Scala | Streams | 6,923,533 | 179,960 |
| Scala | Views | 58,082,123 | 1,004,918 | <-- (2)
+----------+-------------+------------+-----------+
Map then reduce
+----------+-------------+------------+-------------+
| Language | Benchmark | Score | Error |
+----------+-------------+------------+-------------+
| Java | Collections | 10,989,113 | 319,941 | <-- (1)
| Java | Streams | 944,391 | 16,689 | <-- (2)
| Scala | Collections | 314,736 | 6,594 |
| Scala | Streams | 115,335 | 1,467 |
| Scala | Views | 476,832 | 9,358 | <-- (3)
+----------+-------------+------------+-------------+