streams - generate()

Another approach to creating a separate factory class:

// streams/Bubble.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.function.*;

public class Bubble {
  public final int i;

  public Bubble(int n) {
    i = n;
  }

  @Override
  public String toString() {
    return "Bubble(" + i + ")";
  }

  private static int count = 0;

  public static Bubble bubbler() {
    return new Bubble(count++);
  }
}
// streams/Bubbles.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.stream.*;

public class Bubbles {
  public static void main(String[] args) {
    Stream.generate(Bubble::bubbler).limit(6).forEach(System.out::println); // method reference.
  }
}
/* My Output:
Bubble(0)
Bubble(1)
Bubble(2)
Bubble(3)
Bubble(4)
Bubble(5)
*/
@FunctionalInterface
public interface Consumer<T>

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

This is a functional interface whose functional method is accept(Object).

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Bubble.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Bubbles.java

4. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-

5. https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help with that! First, let me explain the reasoning behind the definition of the `exp-series` stream. The series expansion for `e^x` is given by: ``` e^x = 1 + x + (x^2)/2! + (x^3)/3! + (x^4)/4! + ... ``` This can be rewritten as: ``` e^x = 1 + x*(1 + x/2*(1 + x/3*(1 + x/4*(...)))) ``` We can use this recursive definition to generate the stream of coefficients for the series. The `stream-cons` function is used to define the stream, and the `integrate-series-tail` function is used to integrate the series to generate the next term. The `integrate-series-tail` function takes a stream and integrates it term-by-term. In this case, we integrate the `exp-series` stream to generate the next term in the series. The first term in the stream is 1, since `e^0 = 1`. Here's the code to define the `exp-series` stream: ``` (define exp-series (stream-cons 1 (integrate-series-tail exp-series))) ``` Now, let's move on to generating the series for sine and cosine. The series expansions for sine and cosine are given by: ``` sin(x) = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ... cos(x) = 1 - (x^2)/2! + (x^4)/4! - (x^6)/6! + ... ``` We can use similar recursive definitions to generate the streams of coefficients for these series. We'll define two streams, `sin-series` and `cos-series`, and we'll use mutual recursion to define them. The `sin-series` stream will start with `x`, since `sin(0) = 0`, and the `cos-series` stream will start with `1`, since `cos(0) = 1`. We'll use the same `integrate-series-tail` function to integrate the series and generate the next term. Here's the code to define the `sin-series` and `cos-series` streams: ``` (define sin-series (stream-cons 0 (integrate-series-tail cos-series))) (define cos-series (stream-cons 1 (integrate-series-tail (stream-map - sin-series)))) ``` In the definition of `cos-series`, we use `stream-map` to negate the terms in `sin-series`, since the coefficient of `x` in the expansion for `cos(x)` is `0`. We can then use these streams to generate approximations of `sin(x)` and `cos(x)` for any value of `x`. I hope that helps! Let me know if you have any other questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值