供应商< T>的反面在番石榴 [英] Inverse of Supplier<T> in Guava

查看:93
本文介绍了供应商< T>的反面在番石榴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Guava中供应商< T> 的倒数。我希望它被称为 Consumer - nope - 或 Sink - 存在,但是用于原始值。



它隐藏在什么地方,我错过了它吗?



我想看到它与供应商的原因很有用。诚然,使用不太常见,但是很多供应商的静态方法可以以类似的方式应用,并且用一行表示一行比如在这个迭代器中发送这个供应商的每个值。



同时, Predicate 函数< T,Void> 是丑陋的解决方法。您可以选择




  • Java 8 引入了 撰写。

  • Xtend 的标准库包含 程序

  • Scala has < a href =http://www.scala-lang.org/api/current/index.html#scala.Function1 =nofollow noreferrer> 功能* ;如果函数的返回类型是 Unit ,这被认为是一种副作用。



在所有这些语言中,您都可以使用功能界面方便,所以你也可以使用例如功能性Java ' 效果

否则,您最好依靠现有的语言结构来执行副作用,例如内置的用于循环。 Java< 8在使用lambda时造成巨大的语法开销。请参阅此问题 this discussion


I'm looking for the inverse of Supplier<T> in Guava. I hoped it would be called Consumer – nope – or Sink – exists, but is for primitive values.

Is it hidden somewhere and I'm missing it?

I'd like to see it for the same kinds of reasons that Supplier is useful. Admittedly, uses are less common, but many of the static methods of Suppliers, for example, would apply in an analogous way, and it would be useful to express in one line things like "send this supplier every value in this iterable".

In the meantime, Predicate and Function<T,Void> are ugly workarounds.

解决方案

Your alternatives are:

In all of these languages, you can use functional interfaces conveniently, so you could also use e.g. Functional Java's Effect.

Otherwise, you better rely on existing language constructs for performing side effects, e.g. the built-in for loop. Java < 8 inflicts tremendous syntactic overhead when using lambdas. See this question and this discussion.

这篇关于供应商&lt; T&gt;的反面在番石榴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆