Skip to content

Instantly share code, notes, and snippets.

Last active November 28, 2015 20:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mmhelloworld/eac189d52cd1e2a91ad5 to your computer and use it in GitHub Desktop.
package helloworld;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import static java.lang.String.format;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static java.util.function.Function.identity;
import static;
* See
public class Main {
public static void main(String[] args) {
out.println(fzip(Main::mapIdentity, asList("one", "two", "three"), asList(1, 2, 3)));
out.println(fzip(Main::reverse, asList("one", "two", "three"), asList(1, 2, 3)));
public static <A, B> List<Tuple<A, B>> fzip(MyFunction f, List<A> xs, List<B> ys) {
return zip(f.apply(xs), f.apply(ys));
public static <A, B> List<Tuple<A, B>> zip(List<A> xs, List<B> ys) {
List<Tuple<A, B>> res = new ArrayList<>();
Iterator<B> ysItr = ys.iterator();
for (A x: xs) {
if (ysItr.hasNext()) {
final B y =;
res.add(new Tuple<>(x, y));
} else {
return res;
return res;
public static <A> List<A> mapIdentity(List<A> xs) {
public static <A> List<A> reverse(List<A> xs) {
List<A> ys = new ArrayList<>(xs);
return ys;
public interface MyFunction {
<A> List<A> apply(List<A> xs);
public static class Tuple<A, B> {
public final A first;
public final B second;
public Tuple(A first, B second) {
this.first = first;
this.second = second;
public String toString() {
return format("(%s, %s)", first, second);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment