Skip to content

Instantly share code, notes, and snippets.

View barambani's full-sized avatar

Filippo Mariotti barambani

View GitHub Profile
@barambani
barambani / MyVimrc
Last active June 29, 2020 18:55
My current .vimrc file
" Initialize plugin system
call plug#begin('~/.vim/plugged')
Plug 'rizzatti/dash.vim'
Plug 'derekwyatt/vim-scala'
Plug 'vim-syntastic/syntastic'
Plug 'kien/ctrlp.vim'
Plug 'scrooloose/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'ervandew/supertab'

Advanced Functional Programming with Scala - Notes

Copyright © 2016-2017 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
@barambani
barambani / Day1.scala
Last active December 31, 2019 22:33
Applied Functional Programming In Scala Day 1
object Functions {
/**
* Functions
* - Total (every element in the domain must have a representation in the co-domain)
* - Deterministic (give always the same result for the same input)
* - Not side effecting (the only result of the function is the mapped value)
*/
/**
@barambani
barambani / kinds.md
Created June 11, 2018 15:05 — forked from 5310/kinds.md
Kind Polymorphism in Haskell #article

So we have all these types like Int, Float, String, Map String Int, [Maybe String], etc. What do all these types have in common? Well, essentially, they're "normal" types where we can make a value for them. Just as values have types, types have "kinds". In this case, just as 4 : Int, Int : *, and just as "Hello" : String, String : *. That is to say, all these normal types (the types of plain old values) have kind *.

Now, in OCaml, you have type parametricity, but only over types of kind *. Let's give an example of a type that's not of kind *.

Map : * -> * -> *

We apply this "type constructor" (which is a kind of "type function", just like constructors are a kind of function) to the type Int : *.

Map Int : * -> *

@barambani
barambani / fpmax.scala
Created July 15, 2018 13:29 — forked from jdegoes/fpmax.scala
FP to the Max — Code Examples
package fpmax
import scala.util.Try
import scala.io.StdIn.readLine
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@barambani
barambani / typelevelcps.scala
Created August 27, 2018 12:45 — forked from milessabin/typelevelcps.scala
Using type level continuation passing style to rewrite a whitebox macro (which relies on fundep materialization) as a blackbox macro
import scala.language.higherKinds
// Whitebox ...
trait Schema[T, R] {
def conv(t: T): R
}
object Schema {
// Whitebox macro: R is computed from T
implicit def mkSchema[T, R]: Schema[T, R] = ??? // macro ...
@barambani
barambani / LocalTests.scala
Created October 6, 2018 18:26 — forked from milessabin/LocalTests.scala
Setup for local integration (ie. compile pos/neg/run) tests for lampepfl/dotty
// local/localtests/test/localtests/LocalTests.scala
package localtests
import java.nio.file._
import java.util.stream.{ Stream => JStream }
import org.junit.{ AfterClass, Test }
import scala.collection.JavaConverters._
import scala.concurrent.duration._
@barambani
barambani / fs2.scala
Created October 14, 2018 23:17 — forked from smarter/fs2.scala
trait Stream[+F[_], +A]
object Stream {
// Make scalac get over its oudenophobia.
type Nothing2[X] <: Nothing
def emits[F[x] >: Nothing2[x], A](as: List[A]): Stream[F, A] = new Stream[F, A] {}
implicit class InvariantOps[F[x] >: Nothing2[x], A](private val self: Stream[F, A]) extends AnyVal {
def ethrough[B](p: Stream[F, A] => Stream[F, B]): Stream[F, B] = p(self)
@barambani
barambani / GADTs.scala
Created December 1, 2018 15:22 — forked from pchiusano/GADTs.scala
GADT support in Scala
/** GADTs in Scala and their limitations */
/** Background: what is an algebraic data type (ADT) ?
* ADT: (possibly) recursive datatype with sums and products
* In scala - a trait with case classes (case class is product, subtyping is sum)
*/
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */
sealed trait Expr {
def apply(other: Expr) = Ap(this, other)
@barambani
barambani / listt.scala
Created January 9, 2019 21:31 — forked from tpolecat/listt.scala
A demonstration in Scala of failed associativity of ListT. Example taken from https://wiki.haskell.org/ListT_done_right
// addCompilerPlugin("com.milessabin" % "si2712fix-plugin" % "1.2.0" cross CrossVersion.full)
import scalaz._, Scalaz._
implicit def f2k[F[_], A, B](f: A => F[B]) = Kleisli(f)
val a: Int => ListT[List, Int] = {
case 0 => ListT(List(List(0, 1)))
case 1 => ListT(List(List(0), List(1)))
}