如何将复杂的Java lambda转换为Kotlin?

7xllpg7q  于 2023-02-13  发布在  Kotlin
关注(0)|答案(1)|浏览(149)

我正在尝试将下面的Java代码转换为Kotlin(为了可读性,省略了接口实现):

public class Test {
    public static void main(String[] args) {
        validate("some text", extractor -> s -> check(s, extractor));
    }

    private static void validate(String s, Function<Extractor, Validator> factory) {
        final Extractor extractor = all -> all; // use some more meaningful extractor here
        factory.apply(extractor).validate(s);
    }

    private static boolean check(String s, Extractor extractor) {
        return s.equals(extractor.extract(s));
    }

    interface Extractor {
        String extract(String all);
    }

    interface Validator {
        boolean validate(String s);
    }
}

这包含了一个非常紧凑的lambda表达式,我想将其转换为Kotlin(关于Java lambda的一个更易读的替代方案,请参见[1])。我目前的解决方案保留了interfaces,但这似乎导致了一个相当复杂的调用(比较main中的validate-invocations):

object Test {
    fun main(args: Array<String>) {
        validate("test", object : Function<Extractor, Validator> {
            override fun apply(extractor: Extractor): Validator {
                return object : Validator {
                    override fun validate(s: String): Boolean {
                        return check(s, extractor)
                    }
                }
            }
        })
    }

    private fun validate(s: String, factory: Function<Extractor, Validator>) {
        val extractor : Extractor = object : Extractor {  // use some more meaningful extractor here
            override fun extract(all: String): String {
                return all
            }
        }
        factory.apply(extractor).validate(s)
    }

    private fun check(s: String, extractor: Extractor): Boolean {
        return s == extractor.extract(s)
    }

    internal interface Extractor {
        fun extract(all: String): String
    }

    internal interface Validator {
        fun validate(s: String): Boolean
    }
}
    • 问题**:在保持interfaces不变的情况下,是否存在更有效的validate调用?
    • 后续问题**:这个逻辑需要一个可配置的验证(Validator)和复杂的check-使用可配置提取(Extractor)的方法。

[1]紧凑的lambda对应于以下可读性更强的代码:

public static void main(String[] args) {
        validate("some text", extractor -> {
            return s -> {
                return check(s, extractor);
            };
        });
    }
soat7uwm

soat7uwm1#

Function已经可以转换为lambda。接下来,将ExtractorValidator声明为fun interface。然后代码最终可以重写为:

object Test {
    fun main(args: Array<String>) {
        validate("test") { extractor -> Validator { s -> check(s, extractor) } }
    }

    private fun validate(s: String, factory: Function<Extractor, Validator>) {
        val extractor = Extractor { all -> all }   // use some more meaningful extractor here
        factory.apply(extractor).validate(s)
    }

    private fun check(s: String, extractor: Extractor): Boolean {
        return s == extractor.extract(s)
    }

    internal fun interface Extractor {
        fun extract(all: String): String
    }

    internal fun interface Validator {
        fun validate(s: String): Boolean
    }
}

相关问题