Skip to content

Instantly share code, notes, and snippets.

View Kraks's full-sized avatar
🐈
no longer working from home

Guannan Wei Kraks

🐈
no longer working from home
View GitHub Profile
package thermo
/* Scala implementation of delimited continuations using "thermometer continuations" from
* Capturing the future by replaying the past (functional pearl)
* https://dl.acm.org/doi/10.1145/3236771
* Based on its Java artifact: https://github.com/jkoppel/thermometer-continuations
* Written and translate to Scala by Guannan Wei.
*/
trait Block[A] {
package duality.of.sorts
// A Duality of Sorts
// Ralf Hinze, Jose Pedro Magalhaes, Nicolas Wu
// in The Beauty of Functional Code, LNCS 8106, Springer
// This paper: makes the duality between folds and unfolds explicit,
// defines sorting algorithms as folds of unfolds,
// and as unfolds of folds.
;; Use Melpa
(require 'package)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes
(quote
@Kraks
Kraks / Tour.scala
Created December 3, 2019 19:23
CS502/Tour
object Basic {
trait Expr
case class Var(x: String) extends Expr
case class Lam(x: String, body: Expr) extends Expr
case class App(e1: Expr, e2: Expr) extends Expr
trait Value
case class FunV(f: Value Value) extends Value
type Env = Map[String, Value]
@Kraks
Kraks / Futamura.scala
Created May 10, 2019 00:45
Some types of Futamura projections.
trait Futamura1 {
// Roughly follow the idea from http://blog.sigfpe.com/2009/05/three-projections-of-doctor-futamura.html
type P[_] // program representation type
type M[_] // machine type
/* To run a machine: */
def run[A](ma: M[A]): A
/* The specializer: a machine that
takes a program representation of A => B,
@Kraks
Kraks / Shift.py
Last active September 12, 2018 04:33
class Shift:
def __init__(self, fun):
self.fun = fun
def map(self, f): return Shift(lambda k: self.fun(lambda x: k(f(x))))
def bind(self, f): return Shift(lambda k: self.fun(lambda x: f(x).fun(k)))
def bind2(self, f):
def aux(k):
#lang racket
(require rackunit)
#| Negation Normal Form |#
;; TODO: Add biconnection for NNF transformation
(define (nnf prop)
(match prop
@Kraks
Kraks / stage-fix.fsx
Created January 23, 2018 18:39 — forked from palladin/stage-fix.fsx
Staged Fixed-point combinator
open Microsoft.FSharp.Quotations
// <@ fun x -> (% <@ x @> ) @> ~ lambda (fun x -> x)
let lambda (f : Expr<'T> -> Expr<'R>) : Expr<'T -> 'R> =
let var = new Var("__temp__", typeof<'T>)
Expr.Cast<_>(Expr.Lambda(var, f (Expr.Cast<_>(Expr.Var var))))
// fixed-point combinator
let rec fix : (('Τ -> 'R) -> ('Τ -> 'R)) -> 'Τ -> 'R = fun f x ->
;; -*- mode: emacs-lisp -*-
;; This file is loaded by Spacemacs at startup.
;; It must be stored in your home directory.
(defun dotspacemacs/layers ()
"Configuration Layers declaration.
You should not put any user code in this function besides modifying the variable
values."
(setq-default
;; Base distribution to use. This is a layer contained in the directory

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