es6 写util函数_为什么我喜欢Java util函数

es6 写util函数

One of the greatest things that happened to Java8 is FunctionalInterface . It’s amazing. It makes java developer’s life so functional that you will love it. By name, we get some idea that it is an interface and it’s functional too. Ohh…As far as I know, an interface can not have implementation and a function is nothing but implementation only.

Java8发生的最伟大的事情之一是FunctionalInterface 。 太奇妙了。 它使Java开发人员的生活变得如此实用,以至于您会爱上它。 顾名思义,我们知道它是一个接口,并且也具有功能。 哦……据我所知,接口不能有实现,而功能仅是实现而已。

Should we say that java creators have skimped while finding an appropriate name to this feature???

我们是否应该说Java创建者在为此功能找到合适的名称时就ski了?

I would say, they were not skimping instead they were very clever to give it a name as FunctionalInterface . As we know, the interface provides you a way to implement your own version of the prototype. So they decided that they will give some prototype(here a predefined function(not a method)) to users and let them implement their own version and use it as they want… hurrrrrrah. But it might not have been possible if there were no generic concepts.

我要说的是,他们并没有精打细算,而是非常聪明地为其命名为FunctionalInterface 。 众所周知,该接口为您提供了一种实现您自己的原型版本的方法。 因此,他们决定向用户提供一些原型(这里是预定义的函数(不是方法)),并让他们实现自己的版本并根据需要使用它……hurrrrrrah。 但是,如果没有通用概念,这可能是不可能的。

Ohh God, they are very clever, they know how to use the existing features massively. So here comes all the masterpieces of java-8-functional-interfaces. I call them base functional interfaces.

哦,天哪,他们非常聪明,他们知道如何大量使用现有功能。 因此,这里介绍了java-8-functional-interfaces的所有杰作。 我称它们为基本功能接口。

  1. Function

    功能
  2. Predicate

    谓词
  3. Supplier

    供应商
  4. Consumer

    消费者

We will not be seeing any code here, but we will just try to understand it by imagining it.

我们在这里看不到任何代码,但是我们将通过想象来尝试理解它。

So what is Function ?

那么什么是Function

I am asking about the real-life bro? What is a function in your real life?

我问的是现实生活中的兄弟? 您现实生活中的功能是什么?

A function is some sort of event where some people come and do bla bla and then go back. > yeah???

功能是某种事件,某些人来来去去,然后回去。 >是的???

This Function is also very similar to real-life functions, which takes some input and do some bla bla then returns back. So now let’s give it a proper(in normal language) definition: — It takes one type of input and returns one type of output. Here, the type can be any concrete substance(Object).

Function也非常类似于现实生活中的函数,后者需要一些输入并做一些bla bla bla然后返回。 现在让我们给它一个适当的定义(用正常语言):—它接受一种输入,并返回一种输出。 在此,类型可以是任何具体物质(对象)。

Now let’s understand Consumerfirst: — We can think of it as a real-life consumer who takes one type of input and do some process but does not return anything. Let’s understand this: — I want someone to do some tasks, but I don’t want to know whether that person completed that task or not. I will just ask him to do it that’s it.

现在让我们首先了解“ Consumer :—我们可以将其视为现实生活中的消费者,他接受一种类型的输入并执行某些处理,但不返回任何东西。 让我们了解一下:—我希望某人执行某些任务,但是我不想知道该人是否完成了该任务。 我只问他就是这样。

Supplier : does not take any input but returns one type of output. You can think of a supplier as — bro I want this, you bring it from anywhere I don’t care.

Supplier :不接受任何输入,但返回一种输出。 您可以将供应商视为-我想要这个,您可以从我不在意的任何地方带走它。

Predicate : as the word itself says: — something which is affirmed or denied concerning an argument of a proposition. Basically a Predicate functional interface gives you a prototype to assert something you pass it to.

Predicate :正如该词本身所说的:—关于命题论证的肯定或否定的东西。 基本上, Predicate功能接口为您提供原型,以声明将其传递给它的内容。

Now, I hope you understand the literal meaning of all above mentioned functional interfaces. If not, please let me know in the comment section below!

现在,我希望您理解上述所有功能接口的字面含义。 如果没有,请在下面的评论部分中告诉我!

There are much more functional interfaces, in which some are enhanced form of base functional interfaces or some are in customized forms. So Let’s look into enhanced form first: —

功能接口更多,其中一些是基本功能接口的增强形式,或者一些是定制形式的。 因此,让我们先看看增强形式:

  1. BiFunction: Takes two types of input and perform some task then returns one type of output.

    BiFunction :接受两种类型的输入并执行某些任务,然后返回一种类型的输出。

  2. BiConsumer: Takes two types of input and perform some task.

    BiConsumer :接受两种类型的输入并执行某些任务。

  3. BiPredicate: Assert on two types of input.

    BiPredicate :断言两种类型的输入。

  4. BinaryOperator: Takes two same type of input and returns the same type of output as the input’s type.

    BinaryOperator:接受两种相同类型的输入,并返回与输入类型相同的输出类型。
  5. UnaryOperator: Takes one type of input and returns the same type of output as the input’s type.

    UnaryOperator:接受一种输入类型,并返回与输入类型相同的输出类型。

Now Let’s focus on all customized forms: —

现在,让我们关注所有自定义表单:

  1. IntFunction: Takes Integers as input and performs some operations and returns any type of output.

    IntFunction:将Integers作为输入并执行一些操作并返回任何类型的输出。
  2. ToIntFunction: Takes any type of input and performs some operations and returns Integer.

    ToIntFunction:接受任何类型的输入并执行一些操作并返回Integer。
  3. LongFunction: Takes Long data type as input and performs some operations and returns any type of output.

    LongFunction:将Long数据类型作为输入并执行一些操作并返回任何类型的输出。
  4. ToLongFunction: Takes any type of input and performs some operations and returns a long data type.

    ToLongFunction:接受任何类型的输入并执行一些操作并返回long数据类型。
  5. DoubleFunction: Takes Double data type as input and performs some operations and returns any type of output.

    DoubleFunction:将Double数据类型作为输入并执行一些操作并返回任何类型的输出。
  6. ToDoubleFunction: Takes any type of input and performs some operations and returns a double data type.

    ToDoubleFunction:接受任何类型的输入并执行一些操作并返回double数据类型。
  7. DoubleToIntFunction: Takes Double data type of input and performs some operations and returns an Integer data type.

    DoubleToIntFunction:接受输入的Double数据类型并执行一些操作,然后返回Integer数据类型。
  8. DoubleToLongFunction: Takes Double data type of input and performs some operations and returns a Long data type.

    DoubleToLongFunction:接受Double数据类型的输入并执行一些操作,然后返回Long数据类型。
  9. IntToLongFunction: Takes Integer data type of input and performs some operations and returns a Long data type.

    IntToLongFunction:接受输入的Integer数据类型并执行一些操作,然后返回Long数据类型。
  10. IntToDoubleFunction: Takes Integer data type of input and performs some operations and returns a Double data type.

    IntToDoubleFunction:接受输入的Integer数据类型并执行一些操作,然后返回Double数据类型。
  11. LongToIntFunction: Takes a Long data type of input and performs some operations and returns an Integer data type.

    LongToIntFunction:接受输入的Long数据类型并执行一些操作,然后返回Integer数据类型。
  12. LongToDoubleFunction: Takes a Long data type of input and performs some operations and returns a Double data type.

    LongToDoubleFunction:接受输入的Long数据类型并执行一些操作,然后返回Double数据类型。
  13. IntConsumer: Just takes an integer and performs some operations.

    IntConsumer:只需要一个整数并执行一些操作。
  14. LongConsumer: Just takes a long data type value and performs some operations.

    LongConsumer:只需要一个长数据类型值并执行一些操作。
  15. DoubleConsumer: Just takes a double data type value and performs some operations.

    DoubleConsumer:只是获取一个double数据类型值并执行一些操作。
  16. BooleanSupplier: Just returns a boolean value.

    BooleanSupplier:只返回一个布尔值。
  17. IntSupplier: Just returns an integer value.

    IntSupplier :仅返回一个整数值。

  18. LongSupplier: Just returns a long value.

    LongSupplier :只返回一个长值。

  19. DoubleSupplier: Just returns a double value.

    DoubleSupplier :仅返回一个双精度值。

  20. IntPredicate: Assert on an integer value.

    IntPredicate :声明一个整数值。

  21. LongPredicate: Assert on a long value.

    LongPredicate :声明一个long值。

  22. DoublePredicate: Assert on a double value.

    DoublePredicate :声明一个双精度值。

There are some enhanced customized forms also: —

还有一些增强的自定义表单:

  1. ToIntBiFunction: Takes two types of input and performs some operations then returns an Integer data type.

    ToIntBiFunction:接受两种类型的输入并执行一些操作,然后返回Integer数据类型。
  2. ToLongBiFunction: Takes two types of input and performs some operations then returns a Long data type.

    ToLongBiFunction:接受两种类型的输入并执行一些操作,然后返回Long数据类型。
  3. ToDoubleBiFunction: Takes two types of input and performs some operations then returns a Double data type.

    ToDoubleBiFunction:接受两种类型的输入并执行一些操作,然后返回Double数据类型。
  4. IntUnaryOperator: Takes an integer and perform some operations and returns an integer.

    IntUnaryOperator:取一个整数并执行一些操作并返回一个整数。
  5. LongUnaryOperator: Takes a long and perform some operations and returns a long date type.

    LongUnaryOperator:需要很长时间并执行一些操作,并返回一个长日期类型。
  6. DoubleUnaryOperator: Takes a double and perform some operations and returns a double.

    DoubleUnaryOperator:进行双精度处理并执行一些操作,然后返回双精度计算。
  7. IntBinaryOperator: Takes two integers and performs some operations and returns an integer.

    IntBinaryOperator:接受两个整数并执行一些操作并返回一个整数。
  8. LongBinaryOperator: Takes two long data types and performs some operations and returns a long data type.

    LongBinaryOperator:接受两个long数据类型并执行一些操作并返回long数据类型。
  9. DoubleBinaryOperator: Takes two doubles and performs some operations and returns a double.

    DoubleBinaryOperator:进行两个双精度处理,并执行一些操作,然后返回一个双精度计算。
  10. ObjIntConsumer: Special type of consumer which takes two inputs in which the second input is always an integer.

    ObjIntConsumer :特殊类型的使用者,它接受两个输入,其中第二个输入始终是整数。

  11. ObjLongConsumer: Special type of consumer which takes two inputs in which the second input is always a long value.

    ObjLongConsumer :特殊类型的使用者,它接受两个输入,其中第二个输入始终是long值。

  12. ObjDoubleConsumer: Special type of consumer which takes two inputs in which the second input is always a double value.

    ObjDoubleConsumer :特殊类型的使用者,具有两个输入,其中第二个输入始终是双精度值。

Hey man! Can you help me to write a function to add two integers???

嗨,老兄! 您能帮我写一个将两个整数相加的函数吗???

Hey bro! Can you help me to write a function to find if a String starts with ‘D’???

嘿哥们儿! 您能帮我写一个函数来查找字符串是否以'D'开头吗????

Hey buddy! I will give you data to put into the cache. Can you do it for me?

你好朋友! 我会给你数据放入缓存。 你可以帮我做这件事吗?

Hey, Can you return me a failure message?

嘿,您能给我发失败消息吗?

They(Java creators) have given us many functions to implement and use it. Isn’t it beautiful? Once you start using these functions your code readability will improve so much.

他们(Java创建者)为我们提供了许多实现和使用它的功能。 不漂亮吗一旦开始使用这些功能,您的代码可读性将大大提高。

That’s all for this blog. Let me know if you found this interesting.

这就是本博客的全部内容。 如果您觉得这很有趣,请告诉我。

You can reach me on twitter.

您可以在Twitter上与我联系。

Thanks.

谢谢。

翻译自: https://medium.com/@govinda_raj/why-i-love-java-util-function-8e131a36fd8d

es6 写util函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值