Skip to content

Instantly share code, notes, and snippets.

View pirita's full-sized avatar
📟

Ignacio Navarro Martin pirita

📟
  • Spain
View GitHub Profile
@MarcRiera
MarcRiera / dengo_controller_documentation.md
Last active February 19, 2021 08:44
Densha de GO! controller documentation
@milessabin
milessabin / no-effect.scala
Created March 20, 2018 10:38
Side-effects are bad, right?
// What output does this yield? What should it? (try with either 2.12.x or 2.13.x)
// (spoiler: https://github.com/scala/bug/issues/10788)
object Test {
class Box[T](t: T) {
def foo: T = {
println("effect")
t
}
}

Quick Tips for Fast Code on the JVM

I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So… here's my word of mouth.

This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea

@mpilquist
mpilquist / example.md
Last active May 17, 2021 13:17
Properly scheduling effect evaluation in FS2

TL;DR - Use fs2.time.sleep_[Task](delay) ++ Stream.eval(effect) instead of Stream.eval(effect.schedule(delay)).

FS2 never interrupts evaluation of an effect. This can lead to surprising behavior when using the schedule method on Task. Consider this test driver:

def testInterruption[A](effect: Stream[Task, A]): Stream[Task, A] = {
  val logStart = Stream.eval_(Task.delay(println("Started: " + System.currentTimeMillis)))
  val logFinished = Stream.eval_(Task.delay(println("Finished: " + System.currentTimeMillis)))
  val interruptSoonAfterStart =
 Stream.eval(async.signalOf[Task,Boolean](false)).flatMap { cancellationSignal =>

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@paulp
paulp / unsoundagain.scala
Created June 3, 2016 23:19
Another in the endless parade.
package p
/** Super basic map/flatMap fusion ADT.
*/
sealed trait View[A] {
def map[B](f: A => B): View[B] = CView(this, Mapped(f))
def flatMap[B](f: A => View[B]): View[B] = CView(this, FlatMap(f))
def foreach(f: A => Unit): Unit = this match {
case IdView(xs) => xs foreach f
case class Refrigerated[T](thing: T) // this is just something holding things that have been refrigerated.
trait Refrigeratable[T] { // this is the typeclass
def refrigerate(thing: T): Refrigerated[T]
}
object Refrigerable {
@inline // this is just an annotation telling the JVM to inline this, reducing the amount of indirection occuring.
def apply[T](refrigerateMethod: T => Refrigerated[T]): Refrigerable[T] =
new Refrigerable {
def refrigerate(thing: T): Refrigerated[T] = refrigerateMethod(thing)
@davegurnell
davegurnell / TypeclassDemo.scala
Created October 6, 2015 14:53
Example of the type class pattern in Scala
object TypeclasseDemo {
// The parts of the type class pattern are:
//
// 1. the "type class" itself -- a trait with a single type parameter;
//
// 2. type class "instances" for each type we care about,
// each marked with the `implicit` keyword;
//
// 3. an "interface" to the type class -- one or more methods
@richard-gibson
richard-gibson / MagnetExample.scala
Created June 14, 2015 23:40
Simple magnet example
object MagnetExample extends App {
sealed trait AdditionMagnet {
type Result
def apply():Result
}
object AdditionMagnet {
@propensive
propensive / alloc.scala
Created March 21, 2015 22:34
`alloc`, an alternative to `new`, with better syntax and type inference
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.core._
import rapture.core._
scala> val str: String = alloc() // new String()
str: String = ""