Skip to content

Instantly share code, notes, and snippets.

@pkukielka
pkukielka / factorial.c
Created August 12, 2012 10:26
Factorial in C with trampolines
#include <stdio.h>
typedef struct _trampoline_data {
void(*callback)(struct _trampoline_data*);
void* parameters;
} trampoline_data;
void trampoline(trampoline_data* data) {
while(data->callback != NULL)
data->callback(data);
@pkukielka
pkukielka / Factorial.scala
Created May 31, 2012 13:23
Factorial in scala with trampolines
sealed trait Bounce[A]
case class Done[A](result: A) extends Bounce[A]
case class Call[A](thunk: () => Bounce[A]) extends Bounce[A]
def trampoline[A](bounce: Bounce[A]): A = bounce match {
case Call(thunk) => trampoline(thunk())
case Done(x) => x
}
def factorial(n: Int, sum: BigInt): Bounce[BigInt] {
@pkukielka
pkukielka / Factorial.java
Created May 31, 2012 10:25
Factorial in java with trampolines
import java.math.BigInteger;
class Trampoline<T>
{
public T get() { return null; }
public Trampoline<T> run() { return null; }
T execute() {
Trampoline<T> trampoline = this;