Skip to content

Instantly share code, notes, and snippets.

@NightRa
NightRa / Math.scala
Last active December 19, 2015 05:19
A nice combinatoric problem.
object Math extends App {
case class ResultSet(n1: Int, n2: Int) {
lazy val result = n1 * n2
override def equals(obj: Any): Boolean = obj match {
case r: ResultSet => (n1 == r.n1 && n2 == r.n2) || (n1 == r.n2 && n2 == r.n1)
case _ => false
}
@NightRa
NightRa / ModularPatternMatches.scala
Last active December 23, 2015 12:29
Modular pattern matches turned into an extractor.
object Operator {
type op = PartialFunction[Char, (Int, Int) => Int]
val plus: op = {
case '+' => _ + _
}
val minus: op = {
case '-' => _ - _
}
val multi: op = {
@NightRa
NightRa / DequeTest.scala
Created September 22, 2013 07:54
Some very exhaustive tests for Deque
//Created By Ilan Godik
package elementaryDataStructures
import org.scalatest.{GivenWhenThen, Matchers, FlatSpec}
class DequeTest extends FlatSpec with Matchers with GivenWhenThen {
"A Deque" should "throw an NullPointerException when calling addFirst with null" in {
intercept[NullPointerException] {
val model = new Deque[String]
model.addFirst(null)
@NightRa
NightRa / gist:8284147
Last active January 2, 2016 09:38 — forked from qharlie/gist:8284056
class ModelCombinator extends JavaTokenParsers {
def mod: Parser[Any] = "model" ~ ident ^^ {
case x => println("Hoping for string value of ident " + x.toString())
}
def model: Parser[Any] = "model" ~ ident ~ "{" ~ rep(member) ~ "}"
def member: Parser[Any] = ident ~ ":" ~ ident ~ "@" ~ ("eager" | "lazy") ~ relation.? ~ ";"
@NightRa
NightRa / gist:8924667
Last active August 29, 2015 13:56 — forked from nuttycom/gist:6690987
trait Functor {
type M <: { type T }
def fmap[A, B](fa: M { type T = A })(f: A => B): M { type T = B }
}
implicit class EitherRightFunctor extends Functor { self =>
type L
type M = Either { type A = self.L ; type T = B } //doesn't this specify a subtype of Either, rather than Either itself?
def fmap[A0, B0](fa: M { type A = self.L ; type B = A0 })(f: A0 => B0): Either { type A = self.L ; type B = B0 } =
trait Decorable[A, B] {
def decorate(obj: A, dec: B): Either[String, A]
}
object Decorator {
def decorate[A, B](implicit d: Decorable[A,B]) = d
}
object TagsDecorator {
@NightRa
NightRa / DataStructuresCheatSheet
Created April 8, 2014 19:42
Data Structures cheat sheet
Pre-Oreder:
root-->left-->right
In-Oreder:
left-->root-->right
Post-Oreder:
left-->right-->root
==================================================
Sorts:
Max-Sort:
@NightRa
NightRa / Task.scala
Last active November 20, 2015 09:35
Task - Reader[ExecutionContext, IO[Future[A]]]
package nightra.mashovNotificator.network
import scala.concurrent.{ExecutionContext, Future}
import scalaz.effect.IO
import scalaz.effect.IO._
import scalaz.Reader
import scala.util.{Success, Failure, Try}
case class Task[A](task: Reader[ExecutionContext, IO[Future[A]]]) {
def map[B](f: A => B): Task[B] = Task(Reader(implicit executor => task(executor).map(_.map(f))))
@NightRa
NightRa / gist:173adb9c1ad15d45852f
Last active August 29, 2015 14:00
Scalaz concurrency issue.
Wanted behavior:
[info] Started 1
[info] Started 2
[info] Started 3
[info] Started 4
[info] Started 5
[info] Finished 5 in 215
[info] Finished 4 in 615
[info] Finished 3 in 825
[info] Finished 2 in 1109
מה עשית?
[21:35:30] Ilan: ואו, הרבה
[21:35:36] Ilan: היו תחנות
[21:35:41] Ilan: תחנה 1:
[21:36:00] Ilan: סימולציה שאתה עובד כלשהו ויש לקוח לא מרוצה
[21:36:48] Ilan: אני הייתי מוכר הכרטיסים בקולנוע ובאה אחת שהזמינה 2 כרטיסים בשורה 10 ונתנו לה כרטיס בשורה 1 ובשורה 5
[21:36:52 | 21:36:54 נערך] Ilan: והיא רוצה לשבת עם חברה שלה
[21:37:01] Ilan: ואי אפשר לתת החזר כספי
[21:37:23] Ilan: ואי אפשר יום אחר כי זה היום הולדת של חברה שלה
[21:37:50] Ilan: וניסיתי לעזור לה שאולי תשאל אנשים באולם אם אפשר להחליף