Skip to content

Instantly share code, notes, and snippets.

View hurryabit's full-sized avatar

Martin Huschenbett hurryabit

  • Zurich, Switzerland
View GitHub Profile
@hurryabit
hurryabit / stack_safe.js
Last active June 14, 2022 09:34
Stack-safety for free?
// This gist contains the JavaScript version of the code from the blog post
// https://hurryabit.github.io/blog/stack-safety-for-free/
function triangular(n) {
if (n == 0) {
return 0;
} else {
return n + triangular(n - 1);
}
}
@hurryabit
hurryabit / stack_safe.py
Last active January 8, 2022 17:16
Stack-safety for free?
# This gist contains the Python version of the code from the blog post
# https://hurryabit.github.io/blog/stack-safety-for-free/
import sys
from typing import Callable, Generator, TypeVar
Arg = TypeVar("Arg")
Res = TypeVar("Res")
def triangular(n: int) -> int:
@hurryabit
hurryabit / stack_safe.rs
Last active December 25, 2021 17:43
Stack-safety for free?
// This gist contains the code from the blog post
// https://hurryabit.github.io/blog/stack-safety-for-free/
// We need Rust nightly to run the code.
#![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
fn triangular(n: u64) -> u64 {
if n == 0 {
0
@hurryabit
hurryabit / recurse.py
Last active November 13, 2021 10:07
Easily eliminate recursion in Python using generators
import sys
def recurse(f):
"""
Decorator that turns a "yield transformed" recursive function into a
function that computes the same value as the actual recursive function but
does not blow the stack, even for large inputs. The "yield transformed"
version of a recursive function `f(x)` is obtained by replacing all
recursive calls `f(x')` into `(yield x')`.
@hurryabit
hurryabit / merge-speedscope-json.js
Last active March 14, 2023 17:06
Merge multiple profiles in Speedscope's JSON format into one file
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const SPEEDSCOPE_JSON_SCHEMA = "https://www.speedscope.app/file-format-schema.json";
const [node, prog, ...options] = process.argv;
if (options[options.length - 2] !== "-o") {
let nil = { isEmpty = true } in
let cons = fun hd tl -> { isEmpty = false; head = hd; tail = tl } in
let rec foldrList = fun f z xs ->
if xs.isEmpty then
z
else
f xs.head (foldrList f z xs.tail)
in
let rec foldlList = fun f z xs ->
if xs.isEmpty then
@hurryabit
hurryabit / BikeShop.daml
Last active August 29, 2019 16:10
The DAML example used in the HaskellerZ meeting on August 29th, 2019.
daml 1.2
module BikeShop where
import DA.Date
import DA.Time
template Template t => Proposal t with
proposer: Party
receiver: Party
-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
daml 1.2
module PaintHouse where
template PaintHouse with
owner : Party
painter : Party
cleaner : Party
-- Copyright (c) 2019, Digital Asset (Switzerland) GmbH and/or its affiliates.
-- All rights reserved.
daml 1.2
module Celebration where
import DA.Date qualified as Date
import DA.List
template Celebration with