供应商< T>的反面在番石榴 [英] Inverse of Supplier<T> in Guava
问题描述
我正在寻找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:
- Java 8 introduces a
Consumer
interface which you can compose. - Xtend's standard library contains
Procedures
. - Scala has
Function*
; if a function's return type isUnit
, it is considered a side effect.
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.
这篇关于供应商< T>的反面在番石榴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!