Skip to content

Instantly share code, notes, and snippets.

View plokhotnyuk's full-sized avatar

Andriy Plokhotnyuk plokhotnyuk

View GitHub Profile
@jrudolph
jrudolph / TestJsonClassHierarchyFormat.scala
Created June 2, 2014 12:21
spray-json: JsonFormat for class hierarchy
import spray.json._
object TestJsonClassHierarchyFormat extends App {
sealed trait Animal extends Product
case class Dog(name: String, age: Int) extends Animal
case class Cat(name: String, catchesBirds: Boolean)extends Animal
import DefaultJsonProtocol._
implicit val dogFormat = jsonFormat2(Dog)
object ScalaJSExample extends js.JSApp{
def main() = {
val xs = Seq(1, 2, 3)
println(xs.toString)
val ys = Seq(4, 5, 6)
println(ys.toString)
val zs = for{
x <- xs
y <- ys
} yield x * y
@bistaumanga
bistaumanga / BinaryHeap.scala
Last active May 20, 2022 04:20
Binary Heap implementation in Scala. It can be used for priority queue. In addition to standard Priority Queue available in Java or Scala, this supports delete and update on K-V pair(Priority is defined by Value) with addition pointer/map stored to the keys.
package util
import scala.collection.mutable.HashMap
import scala.collection.mutable.ArrayBuffer
/*
* Single Array Based Binary Min Heap
*/
class BinaryHeap[T]{
@mkrcah
mkrcah / gist:d1af804e03d20133a16d
Created December 3, 2014 09:33
Spray routing - Notes from Scala meet up

Spray routing (@agemooij)

  • Spray should be run standalone in spray-can, since you don't want to encapsulate high-performant actor inside a low-performant container
  • Spray natively supports Futures:
def getEmployees: Future[List[Employees]] = ???
route = get { complete(OK, getEmployees)}
@pathikrit
pathikrit / SudokuSolver.scala
Last active April 12, 2024 15:00
Sudoku Solver in Scala
val n = 9
val s = Math.sqrt(n).toInt
type Board = IndexedSeq[IndexedSeq[Int]]
def solve(board: Board, cell: Int = 0): Option[Board] = (cell%n, cell/n) match {
case (r, `n`) => Some(board)
case (r, c) if board(r)(c) > 0 => solve(board, cell + 1)
case (r, c) =>
def guess(x: Int) = solve(board.updated(r, board(r).updated(c, x)), cell + 1)
val used = board.indices.flatMap(i => Seq(board(r)(i), board(i)(c), board(s*(r/s) + i/s)(s*(c/s) + i%s)))

Actor Queue Notes

The first thing to understand is that the head field inside of scalaz.concurrent.Actor is not the "head" of the message queue in any traditional sense of the word. A better description would be "last". The there are no pointers to the head of the queue, which one of the very clever things about this implementation.

Empty Actor

Consider the case where the actor has no outstanding messages. This new message will go into the following code:

 def !(a: A): Unit = {
@runarorama
runarorama / gist:33986541f0f1ddf4a3c7
Created May 7, 2015 14:06
Higher-kinded types encoded as path-dependent types
trait λ {
type α
}
trait Functor extends λ {
type α <: λ
def map[A,B](x: α { type α = A })(f: A => B): α { type α = B }
}
@apangin
apangin / HotSpot JVM intrinsics
Last active May 11, 2023 18:32
HotSpot JVM intrinsics
_hashCode java/lang/Object.hashCode()I
_getClass java/lang/Object.getClass()Ljava/lang/Class;
_clone java/lang/Object.clone()Ljava/lang/Object;
_dabs java/lang/Math.abs(D)D
_dsin java/lang/Math.sin(D)D
_dcos java/lang/Math.cos(D)D
_dtan java/lang/Math.tan(D)D
_datan2 java/lang/Math.atan2(DD)D
_dsqrt java/lang/Math.sqrt(D)D
_dlog java/lang/Math.log(D)D
@xuwei-k
xuwei-k / PlayMini.scala
Last active February 20, 2016 13:14
came back play mini!
#!/usr/bin/env scalas
/***
scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.play" %% "play-netty-server" % "2.4.6"
*/
import play.core.server._
import play.api.routing.sird._
import play.api.mvc._
@paulp
paulp / functions.scala
Created September 20, 2015 12:34
Implicit classes translating scala lambdas into java.util.function classes.
package psp
package jtos
import java.{ util => ju }, ju.{ stream => jus, function => juf }, jus._
import scala.annotation.unchecked.{ uncheckedVariance => uV }
object generated {
implicit final class BiConsumer[-T1, -T2](f: (T1, T2) => Unit) extends juf.BiConsumer[T1 @uV, T2 @uV] { def accept(x1: T1, x2: T2): Unit = f(x1, x2) }
implicit final class BiFunction[-T1, -T2, +R](f: (T1, T2) => R) extends juf.BiFunction[T1 @uV, T2 @uV, R @uV] { def apply(x1: T1, x2: T2): R = f(x1, x2) }
implicit final class BinaryOperator[T](f: (T, T) => T) extends juf.BinaryOperator[T] { def apply(x1: T, x2: T): T = f(x1, x2) }