Skip to content

Instantly share code, notes, and snippets.

@jbgi
jbgi / Term.java
Last active November 6, 2024 12:48
Generalized Algebraic Data Types (GADT) in Java
import static java.lang.System.*;
import java.util.function.BiFunction;
import java.util.function.Function;
// Implementation of a pseudo-GADT in Java, translating the examples from
// http://www.cs.ox.ac.uk/ralf.hinze/publications/With.pdf
// The technique presented below is, in fact, just an encoding of a normal Algebraic Data Type
// using a variation of the visitor pattern + the application of the Yoneda lemma to make it
// isomorphic to the targeted 'GADT'.
@jbgi
jbgi / Expressions.java
Last active December 13, 2023 10:20
Generated code for @DaTa Expression
package org.derive4j.exemple;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public final class Expressions {
@jbgi
jbgi / Lists.java
Last active January 25, 2016 13:36
Lists.java genrated by Derive4J for the List data type (see exemples)
package org.derive4j.exemple;
import java.lang.Object;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
@jbgi
jbgi / Instantiations.scala
Created January 25, 2016 22:07
scato Benchmarks
package scato
package benchmarks
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Mode}
import scato.Prelude._
import scato.benchmarks.Data._
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
class Instantiations {
package org.derive4j.exemple;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.Optional;
import java.util.function.Function;
public class Example {
private int value;
public int totallyComplexForSure(int x) {
final int result;
if (x <= 0) {
result = 0;
} else if (x == 10) {
result = -value;
import java.util.function.Function;
import org.derive4j.Data;
class Y{}
class Z{}
@Data
public abstract class X<YZ> {
X(){}
@jbgi
jbgi / Elements.java
Last active May 15, 2016 13:11
Elements, generated by derive4j at compile time from the Element#match method definition.
package org.mfusco.fromgoftolambda.examples.visitor;
import java.lang.Object;
import java.lang.Override;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
final class Elements {
private static final TotalMatchBuilderSquareElement totalMatchBuilderSquareElement = new TotalMatchBuilderSquareElement();
@jbgi
jbgi / BoolExpr.java
Created June 8, 2016 15:12
OCaml mimicry in Java translated to derive4j
// The file BoolExprs is generated by derive4j via the @Data annotation.
import static BoolExprs.*;
import java.util.function.Function;
import org.derive4j.Data;
@Data
interface BoolExpr<E> {
@jbgi
jbgi / AdtEncodingsBench.scala
Last active September 23, 2016 18:57
Pattern matching is overrated! All we need is a catamorphism!
package scalaz
import org.openjdk.jmh.annotations._
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
class AdtEncodingsBench {
import AdtEncodingsBench._