Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

import java.util.function.Function;

public class Main {
    public static <A> List<A> reverseWithFoldl(List<A> as) {
        return foldl(as, Collections.EMPTY_LIST, acc -> a -> cons(a, acc));
    }

    public static <A, X> A foldl(List<X> xs, A acc, Function<A, Function<X, A>> s) {
        A result = acc;
        for (X x : xs) {
            result = s.apply(result).apply(x);
        }
        return result;
    }

    public static <A> List<A> cons(A a, List<A> as) {
        List result = new ArrayList<>(as);
        result.add(0, a);
        return Collections.unmodifiableList(result);
    }
}