Skip to content

Instantly share code, notes, and snippets.

@jnape
jnape / Scheduling.hs
Created March 13, 2021 22:43
Cooperative multitasking in haskell
{-# LANGUAGE RankNTypes #-}
module Scheduling where
newtype Fiber f a = Fiber (f (Maybe (Maybe a, Fiber f a)))
noop :: Applicative f => Fiber f a
noop = Fiber $ pure Nothing
singleton :: Applicative f => f (Maybe a) -> Fiber f a
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <optional>
#include <variant>
#include <any>
template<template<typename, typename> typename CP, typename A, typename B, typename R, template<typename> typename F> concept Coproduct2 =
requires (CP<A,B> cp, F<R(A)> f, F<R(B)> g) {
@jnape
jnape / Convolution.java
Created July 23, 2020 18:52
parallel / sequential convolution generalized to any algebraic ring
static interface CommutativeSemigroup<A> extends Semigroup<A> {
}
static interface CommutativeMonoid<A> extends CommutativeSemigroup<A>, Monoid<A> {
}
static interface Abelian<A> extends CommutativeMonoid<A> {
A inverse(A a);
}
static interface Ring<A> {
Abelian<A> addition();
Monoid<A> multiplication();
#include <thread>
#include <iostream>
#include <functional>
#include <algorithm>
#include <numeric>
#include <any>
namespace {
struct __ {};
struct ___ {};
#include <thread>
#include <iostream>
#include <functional>
template<typename T> using Id = T&;
template<typename A, typename B>
struct Phi {
template<typename R>
R match(std::function<R(A)>, std::function<R(B)>) {
@jnape
jnape / pom.xml
Created April 16, 2019 21:24
mnichols test pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mnichols</groupId>
<artifactId>lambda-spike</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>lambda-spike</name>
<description>examples</description>
@jnape
jnape / HListMapper.java
Created February 25, 2018 22:01
Mapping the slots of an HList
package com.jnape.palatable.lambda.adt.hlist;
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
import com.jnape.palatable.lambda.adt.hlist.HList.HNil;
import java.util.function.Function;
import static com.jnape.palatable.lambda.adt.hlist.HList.cons;
import static com.jnape.palatable.lambda.adt.hlist.HList.nil;
import static com.jnape.palatable.lambda.adt.hlist.HList.singletonHList;
@jnape
jnape / SizedIterable.java
Created January 24, 2018 23:10
Dependently-typed Iterable
package example;
import com.jnape.palatable.lambda.functions.builtin.fn2.Cons;
import com.jnape.palatable.lambda.functions.builtin.fn2.Snoc;
import java.util.Iterator;
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
import static example.SizedIterable.Nat.s;
import static example.SizedIterable.Nat.stringify;
@jnape
jnape / Spike.java
Created December 27, 2017 20:15
Type-level encoding and optimization of tail recursive functions in Java with Lambda
package spike;
import com.jnape.palatable.lambda.adt.choice.Choice2;
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.functions.builtin.fn2.Cons;
import com.jnape.palatable.lambda.functor.Bifunctor;
import java.util.Iterator;
public static class SetIsNotAFullyFaithfulFunctor {
public static void main(String[] args) {
class Mod {
private final int mod;
private final int value;
Mod(int mod, int value) {
this.mod = mod;
this.value = value;
}