Java 文件夹:折叠方法

Java 文件夹:折叠方法,java,algorithm,generics,Java,Algorithm,Generics,我在Java中遇到了一个问题,我有以下接口 public interface Function2<T, U, R> { R apply(T t, U u); } public interface Folder<T, U> { U fold(U u, Queue<T> list, Function2<T,U,U> function); } 公共接口功能2 { R应用(T,U); } 公用接口文件夹 { U折叠(U U,队列列表,函

我在Java中遇到了一个问题,我有以下接口

public interface Function2<T, U, R>
{
    R apply(T t, U u);
}

public interface Folder<T, U>
{
    U fold(U u, Queue<T> list, Function2<T,U,U> function);
}
公共接口功能2
{
R应用(T,U);
}
公用接口文件夹
{
U折叠(U U,队列列表,函数2函数);
}
该问题要求开发商实施:

public class MyFolder<T, U> implements Folder<T, U>
{
    public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
    {
        if(u == null || ts == null || function == null)
            throw new IllegalArgumentException();

        if (ts.isEmpty()) {
            return u;
        }

        // The recursive implementation will overflow the stack for
        // any data set of real size, your job is to implement a
        // non-recursive solution
        //return fold(function.apply(ts.poll(), u), ts, function);
        return null;
    }
}
public类MyFolder实现文件夹
{
公共U形折叠(U形,队列ts,功能2功能)
{
if(u==null | | ts==null | |函数==null)
抛出新的IllegalArgumentException();
if(ts.isEmpty()){
返回u;
}
//递归实现将使堆栈溢出
//任何实际大小的数据集,您的工作都是实现
//非递归解
//返回折叠(function.apply(ts.poll(),u),ts,function);
返回null;
}
}
有人能给我解释一下折叠函数的作用吗?我似乎在网上找不到例子。我已经读过这个方法的作用,但它没有给出任何具体的例子。

作为一个例子,
折叠(0,{1,2,3,4,5},+)
应该给出结果
0+1+2+3+4+5=15
作为一个例子,
折叠(0,{1,2,3,4,5},+)
应该给出结果
0+1+2+3+4+5=15

来自维基百科全书:

在函数式编程中,fold(也称为reduce、accumulate、aggregate、compress或inject)指的是一系列高阶函数,它们分析递归数据结构,并通过使用给定的组合操作重新组合递归处理其组成部分的结果,建立返回值。通常,折叠显示有组合函数、数据结构的顶部节点,以及可能在某些条件下使用的一些默认值。然后,折叠继续组合数据结构层次结构的元素,以系统的方式使用函数

哦,注释中实现的非递归等价物是:

do {
    u = function.apply(ts.poll(), u);
} while (!ts.isEmpty());
return u;
来自维基百科:

在函数式编程中,fold(也称为reduce、accumulate、aggregate、compress或inject)指的是一系列高阶函数,它们分析递归数据结构,并通过使用给定的组合操作重新组合递归处理其组成部分的结果,建立返回值。通常,折叠显示有组合函数、数据结构的顶部节点,以及可能在某些条件下使用的一些默认值。然后,折叠继续组合数据结构层次结构的元素,以系统的方式使用函数

哦,注释中实现的非递归等价物是:

do {
    u = function.apply(ts.poll(), u);
} while (!ts.isEmpty());
return u;
递归地将函数应用于每个元素,给定的代码如下所示:

public interface Function2<T, U, R>
{
    R apply(T t, U u);
}

public interface Folder<T, U>
{
    U fold(U u, Queue<T> list, Function2<T,U,U> function);
}
public static class MyFolder<T, U> implements Folder<T, U>
{
    public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
    {
        if(u == null || ts == null || function == null)
            throw new IllegalArgumentException();

        if (ts.isEmpty()) {
            return u;
        }

        return fold(function.apply(ts.poll(), u), ts, function);
    }
}

public static void main(String[] args) {
    Folder<Integer,Integer> fold = new MyFolder<Integer, Integer>();
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);

    Integer result = fold.fold(0, queue, new Function2<Integer, Integer, Integer>() {
        public Integer apply(Integer a, Integer b) {
            return a + b;
        }
    });
    System.out.println(result);
}
public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
{
    if(u == null || ts == null || function == null)
        throw new IllegalArgumentException();

    if (ts.isEmpty()) {
        return u;
    }

    T item = null;

    while ((item = ts.poll()) != null) {
        u = function.apply(item, u);
    }
    return u;
}
迭代解决方案应如下所示:

public interface Function2<T, U, R>
{
    R apply(T t, U u);
}

public interface Folder<T, U>
{
    U fold(U u, Queue<T> list, Function2<T,U,U> function);
}
public static class MyFolder<T, U> implements Folder<T, U>
{
    public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
    {
        if(u == null || ts == null || function == null)
            throw new IllegalArgumentException();

        if (ts.isEmpty()) {
            return u;
        }

        return fold(function.apply(ts.poll(), u), ts, function);
    }
}

public static void main(String[] args) {
    Folder<Integer,Integer> fold = new MyFolder<Integer, Integer>();
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);

    Integer result = fold.fold(0, queue, new Function2<Integer, Integer, Integer>() {
        public Integer apply(Integer a, Integer b) {
            return a + b;
        }
    });
    System.out.println(result);
}
public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
{
    if(u == null || ts == null || function == null)
        throw new IllegalArgumentException();

    if (ts.isEmpty()) {
        return u;
    }

    T item = null;

    while ((item = ts.poll()) != null) {
        u = function.apply(item, u);
    }
    return u;
}
public U折叠(U U,队列ts,函数2函数)
{
if(u==null | | ts==null | |函数==null)
抛出新的IllegalArgumentException();
if(ts.isEmpty()){
返回u;
}
T项=null;
而((item=ts.poll())!=null){
u=功能。应用(项目,u);
}
返回u;
}
递归地将函数应用于每个元素,给定的代码如下所示:

public interface Function2<T, U, R>
{
    R apply(T t, U u);
}

public interface Folder<T, U>
{
    U fold(U u, Queue<T> list, Function2<T,U,U> function);
}
public static class MyFolder<T, U> implements Folder<T, U>
{
    public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
    {
        if(u == null || ts == null || function == null)
            throw new IllegalArgumentException();

        if (ts.isEmpty()) {
            return u;
        }

        return fold(function.apply(ts.poll(), u), ts, function);
    }
}

public static void main(String[] args) {
    Folder<Integer,Integer> fold = new MyFolder<Integer, Integer>();
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);

    Integer result = fold.fold(0, queue, new Function2<Integer, Integer, Integer>() {
        public Integer apply(Integer a, Integer b) {
            return a + b;
        }
    });
    System.out.println(result);
}
public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
{
    if(u == null || ts == null || function == null)
        throw new IllegalArgumentException();

    if (ts.isEmpty()) {
        return u;
    }

    T item = null;

    while ((item = ts.poll()) != null) {
        u = function.apply(item, u);
    }
    return u;
}
迭代解决方案应如下所示:

public interface Function2<T, U, R>
{
    R apply(T t, U u);
}

public interface Folder<T, U>
{
    U fold(U u, Queue<T> list, Function2<T,U,U> function);
}
public static class MyFolder<T, U> implements Folder<T, U>
{
    public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
    {
        if(u == null || ts == null || function == null)
            throw new IllegalArgumentException();

        if (ts.isEmpty()) {
            return u;
        }

        return fold(function.apply(ts.poll(), u), ts, function);
    }
}

public static void main(String[] args) {
    Folder<Integer,Integer> fold = new MyFolder<Integer, Integer>();
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);

    Integer result = fold.fold(0, queue, new Function2<Integer, Integer, Integer>() {
        public Integer apply(Integer a, Integer b) {
            return a + b;
        }
    });
    System.out.println(result);
}
public U fold(U u, Queue<T> ts, Function2<T, U, U> function)
{
    if(u == null || ts == null || function == null)
        throw new IllegalArgumentException();

    if (ts.isEmpty()) {
        return u;
    }

    T item = null;

    while ((item = ts.poll()) != null) {
        u = function.apply(item, u);
    }
    return u;
}
public U折叠(U U,队列ts,函数2函数)
{
if(u==null | | ts==null | |函数==null)
抛出新的IllegalArgumentException();
if(ts.isEmpty()){
返回u;
}
T项=null;
而((item=ts.poll())!=null){
u=功能。应用(项目,u);
}
返回u;
}

旁注:在
null
参数上,通常会抛出
NullPointerException
旁注:在
null
参数上,通常会抛出
NullPointerException