简体   繁体   English

功能性Java将arity-2功能(F2)绑定到选项

[英]Functional Java bind arity-2 Function (F2) to options

I understand the basic bind syntax for Option with functional java as 我了解功能Java的Option的基本绑定语法为

Option.some(2).bind(new F<Integer,Option<Integer>>(){
    public Option<Integer>f(Integer i){
        return Option.some(i/2);
    }
};

That works well for single input functions but I can't figure out how to use multiple input functions (like F2, F3, etc). 这对于单个输入函数很好用,但是我不知道如何使用多个输入函数(例如F2,F3等)。

ie: 即:

new F2<Integer,Integer,Option<Integer>>(){
    public Option<Integer>f(Integer a,Integer b){
        return Option.some(a/b);
    }
} 

I know I'm missing something, but documentation is a bit sparse. 我知道我遗漏了一些东西,但是文档很少。 Ideas? 有想法吗?

Your solution works, using a tuple (single parameter) instead of two parameters and working with them. 您的解决方案有效,使用元组(单个参数)而不是两个参数并使用它们。 An alternative, maybe better, approach would be (I second Diego on this one) to work with partial functions and curry. 另一种可能更好的方法是(我第二次在迭戈上讲)处理部分功能和咖喱。 For instance you could do something like: 例如,您可以执行以下操作:

public final F2<Integer, Integer, Option<Integer>> sumInteger() {
    return new F2<Integer,Integer,Option<Integer>>() {
        @Override
        public Option<Integer> f(Integer a, Integer b) {
            /* you logic here */
        }
    }; 
}

public final F<Integer, Option<Integer>> partialSumInteger(final F2<Integer, Integer, Option<Integer>> f2, final Integer fixed) {
    return Function.partialApply2(f2.curry(), fixed);
}

And then pass your now 1-arity function to bind. 然后传递您现在的1-arity函数进行绑定。 Have a look at the Functional Java's class fj.Function , it contains functions for currying and partially applying your n-arity functions. 看一下Functional Java的fj.Function类,它包含用于fj.Function和部分应用n-arity函数的函数。 I agree the documentation is not the best. 我同意文档不是最好的。

I'm not sure what you're asking, but if you're trying to bind a two-arity function into the Option , you should partially apply the two-arity function before binding it. 我不确定您要问的是什么,但是如果您试图将两个arity函数绑定到Option ,则应在绑定它之前部分应用两个arity函数。 Thus, that'll result into a unary function with a fixed first parameter: 因此,这将导致具有固定的第一个参数的一元函数:

(new F2<...>{ ... }).f(5)

(bind the first parameter to 5 , returning a new unary function). (将第一个参数绑定到5 ,返回一个新的一元函数)。

I've searched and haven't found a partial applicator for the second parameter. 我已经搜索过,但是没有找到第二个参数的部分涂抹器。 This is weird. 真奇怪

Breakthrough, 突破,

The trick is you need to uncurry the arity function and bind it to the product of your options. 诀窍是您需要取消使用arity函数并将其绑定到您选择的产品。

So in example: 因此在示例中:

arity-5 implementation Arity-5实施

import fj.F;
import fj.F5;
import fj.P5;

public abstract class F5Optional<At, Bt, Ct, Dt, Et, Ft> extends F5<At, Bt, Ct, Dt, Et, Ft> {

    public final F<P5<At, Bt, Ct, Dt, Et>, Ft> tupleize() {
        return new F<P5<At, Bt, Ct, Dt, Et>, Ft>() {

            @Override
            public Ft f(final P5<At, Bt, Ct, Dt, Et> p) {
                return F5Optional.this.f(p._1(), p._2(), p._3(), p._4(), p._5());
            }
        };
    }

}

usage: 用法:

    F5Optional<Integer, Integer, Integer, Integer, Integer, Option<Integer>> f5 = new F5Optional<Integer, Integer, Integer, Integer, Integer, Option<Integer>>() {

        @Override
        public Option<Integer> f(Integer a, Integer b, Integer c, Integer d, Integer e) {
            return Option.some(a + b + c + d + e);
        }
    };
    Option<Integer> test2 = b.bindProduct(Option.some(1), Option.some(1), Option.some(1), Option.some(1)).bind(f5.tupleize());
    Assert.assertTrue(new Integer(8).equals(test2.toNull()));

    Option<Integer> nullInteger = Option.none();
    Option<Integer> test3 = b.bindProduct(nullInteger, Option.some(1), Option.some(1), Option.some(1)).bind(f5.tupleize());
    Assert.assertTrue(test3.isNone());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM