Skip to content

Instantly share code, notes, and snippets.

@jbgi
Created April 20, 2017 19:12
Show Gist options
  • Save jbgi/d6b677d084fafc641fe01f7ffd00591c to your computer and use it in GitHub Desktop.
Save jbgi/d6b677d084fafc641fe01f7ffd00591c to your computer and use it in GitHub Desktop.

Revisions

  1. jbgi created this gist Apr 20, 2017.
    44 changes: 44 additions & 0 deletions Label.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@

    import java.io.IOException;
    import org.derive4j.hkt.__;

    public abstract class Label<T> {
    private Label(){}
    public abstract T apply(String s);
    public abstract String unwrap(T lbl);
    public abstract <f> __<f, String> subst(__<f, T> fa);

    static Label<?> Label = new Label<String>() {
    @Override
    public String apply(String s) {
    return s;
    }

    @Override
    public String unwrap(String lbl) {
    return lbl;
    }

    @Override
    public <f> __<f, String> subst(__<f, String> fa) {
    return fa;
    }
    };

    public static void main(String[] args) throws IOException {
    program(Label).run();
    }

    // "Slight" inconvenience vs scala: you have to write your program inside method(s) parametrized by the Label(s):
    static <T> IO<Unit> program(Label<T> Label) {

    class Toto {
    private final T wrapped;
    Toto(T wrapped){this.wrapped = wrapped;}
    }

    Toto toto = new Toto(Label.apply("Hello World"));
    String unwrapped = Label.unwrap(toto.wrapped);
    return IO.effect(() -> System.out.println(unwrapped));
    }
    }