Skip to content

Instantly share code, notes, and snippets.

@mandubian
mandubian / kind_polymorphic.scala
Last active November 28, 2016 20:33
PoC for Kind Polymorphism in Scala
// That compiles for real in a patched version of Scala just introducing the KindPolymorphic syntax
object Test {
// Basic Kind polymorphism sample
trait Foo[T <: KindPolymorphic] { type Out ; def id(t: Out): Out = t }
object Foo {
implicit def foo0[T] = new Foo[T] { type Out = T }
implicit def foo1[T[_]] = new Foo[T] { type Out = T[Any] }
implicit def foo2[T[_, _]] = new Foo[T] { type Out = T[Any, Any] }
}
sealed trait Task[A] {
import Task._
final def flatMap[B](f: A => Task[B]): Task[B] = Suspend(this, f)
final def map[B](f: A => B): Task[B] = this match {
case Suspend(inner, suspension) =>
Suspend(inner, suspension andThen { _ map f })
case Async(body) =>

Getting Started in Scala

This is my attempt to give Scala newcomers a quick-and-easy rundown to the prerequisite steps they need to a) try Scala, and b) get a standard project up and running on their machine. I'm not going to talk about the language at all; there are plenty of better resources a google search away. This is just focused on the prerequisite tooling and machine setup. I will not be assuming you have any background in JVM languages. So if you're coming from Python, Ruby, JavaScript, Haskell, or anywhere…  I hope to present the information you need without assuming anything.

Disclaimer It has been over a decade since I was new to Scala, and when I was new to Scala, I was coming from a Java and Ruby background. This has probably caused me to unknowingly make some assumptions. Please feel free to call me out in comments/tweets!

One assumption I'm knowingly making is that you're on a Unix-like platform. Sorry, Windows users.

Getting the JVM

class TC[A](val x: String)
object TC {
def getX[A: TC](a: A): String = implicitly[TC[A]].x
implicit def anyTC[A]: TC[A] = new TC[A]("*")
implicit val stringTC: TC[String] = new TC[String]("String")
}
object Example {
@cb372
cb372 / 0_PrettyPrinting.scala
Created October 7, 2016 11:27
Prettier AST trees for debugging macros
import scala.reflect.macros.blackbox
object PrettyPrinting {
/*
* Print a raw AST, nicely indented.
* Pretty fragile, e.g. will get confused by string literals containing commas or parentheses.
*/
def prettyTree(raw: String): String = {
var level = 0
@larsrh
larsrh / tut-cli.sh
Created October 1, 2016 07:48
Running tut on the command line with coursier
# Uses Alex Archambault's coursier launcher
# Download as follows:
# $ curl -L -o coursier https://git.io/vgvpD && chmod +x coursier
# ... or refer to instructions here: <https://github.com/alexarchambault/coursier>
# list any dependencies here
COURSIER_CLASSPATH="$(coursier fetch -p com.chuusai:shapeless_2.11:2.3.1)"
# this downloads and launches tut in a seperate step
# the 'classpath' argument is being interpreted by scalac
@jbgi
jbgi / AdtEncodingsBench.scala
Last active September 23, 2016 18:57
Pattern matching is overrated! All we need is a catamorphism!
package scalaz
import org.openjdk.jmh.annotations._
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
class AdtEncodingsBench {
import AdtEncodingsBench._

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 / demo.sh
Last active June 8, 2018 09:16
Enabling sbt plugins from the command line in any sbt project
% sbtx dependencyGraph
... blah blah ...
[info] *** Welcome to the sbt build definition for Scala! ***
[info] Check README.md for more information.
[error] Not a valid command: dependencyGraph
[error] Not a valid project ID: dependencyGraph
% sbtx -Dplugins=graph dependencyGraph
... blah blah ...
@raulraja
raulraja / ecpitfalls.scala
Last active September 10, 2016 08:07
Wrong Execution Context passed to all ops in the same way via flatMap
import scala.concurrent.ExecutionContext.global
for {
a <- nonBlocking
b <- simpleDbLookups
c <- expensiveDbLookups
d <- dbWriteOperations
e <- expensiveCpuOperations
} yield result