Skip to content

Instantly share code, notes, and snippets.

@jbgi
Created October 15, 2017 06:54
Show Gist options
  • Select an option

  • Save jbgi/1a3697d380f2071eb8c2fd3ad259c011 to your computer and use it in GitHub Desktop.

Select an option

Save jbgi/1a3697d380f2071eb8c2fd3ad259c011 to your computer and use it in GitHub Desktop.

Revisions

  1. jbgi created this gist Oct 15, 2017.
    35 changes: 35 additions & 0 deletions Program.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;

    interface Person {
    int age();
    }

    interface People<T> {
    T fromList(List<Person> ps);
    List<Integer> getAges(T people);
    People<?> module = new People<List<Person>>() {
    public List<Person> fromList(List<Person> ps) {
    return ps;
    }
    public List<Integer> getAges(List<Person> people) {
    return people.stream().map(Person::age).collect(Collectors.toList());
    }
    };
    }

    // run with:
    // javac Program.java
    // java -cp . Program
    public class Program<T> {
    People<T> P;
    Program(People<T> P) {this.P = P;}
    void run() {
    T people = P.fromList(Arrays.asList(() -> 21, () -> 37));
    System.out.println(P.getAges(people)); // print "[21, 37]"
    }
    public static void main(String[] args) {
    new Program<>(People.module).run();
    }
    }