这是indexloc提供的服务,不要输入任何密码
Skip to content

Add Kotlin style Sequence Generators #616

@johnmcclean

Description

@johnmcclean

#e.g. with suspend and yield operators - note providing a BooleanSupplier indicates the suspended block should be executed until false (in this case infinitely).

int i = 100;
ReactiveSeq.generate(suspend(infinitely(),s->s.yield(i++)))
           .take(6)
           .printOut();

/**
100
101§
102
103
104
105
106
**/

Execute the suspended block just once (itself meaningless unless we can also sequence actions)

int i = 100;
ReactiveSeq.generate(suspend(s->s.yield(i++)))
           .take(6)
           .printOut();

/**
100
**/

Yield a sequence of values

ReactiveSeq.generate(suspend(times(10),s-> {
            System.out.println("Top level - should repeat after sequence completes!");
            return s.yield(1,
                           () -> s.yield(2),
                           () -> s.yield(3),
                           () -> s.yield(4));
                       }))
           .take(6)
           .printOut();    

Support method references

Generator<Integer> generator = suspendRef(times(10),this::next);
generator.stream()
         .forEach(System.out::println);

List<Integer> list = generator.to()
                              .linkedListX(LAZY)
                              .type(VavrTypes.list())
                              .map(this::process)          
                              .to(VavrConverters::LIST);
public Integer next(){
        return i++;
}
int i = 100;

Method references directly during yielding

ReactiveSeq.generate(suspend(times(10),s-> {
                    System.out.println("Top level - should repeat after sequence completes!");
                    return s.yieldRef(1,
                                      Generator::next,
                                      Generator::next,
                                      Generator::next,
                                      Generator::next);
                }
        )).take(6)
          .printOut();
public Integer next(){
        return i++;
}

More managable / complex mutable state via Inner Classes (if really needed)

ReactiveSeq.<Integer>generate(suspend(times(10),new ContFunction<Integer>() {
                    int runningTotal =0;

                   @Override
                   public Generator<Integer> apply(Suspended<Integer> s) {
                       System.out.println("Top level - should repeat after sequence completes!");
                       return s.yield(1,
                               () -> {
                                    runningTotal = runningTotal +5;
                                    return s.yield(runningTotal+2);
                               },
                               () -> s.yield(runningTotal+3),
                               () -> s.yield(runningTotal+6));

                   }
               }

        )).take(6)
          .printOut();

Control looping based on current value and support safe (s.maybe()) and unsafe (s.current()) to the current value.

ReactiveSeq.generate(suspend((Integer i)->i==4,s-> {
                    System.out.println("Top level - repeat infinetely after sequence (because we return 4!)");
                    return s.yield(1,
                            () -> s.yield(s.maybe()
                                            .map(o->o+5)
                                            .orElse(10)),
                            () -> s.yield(s.current()),
                            () -> s.yield(4));
                }
        )).take(120)
          .printOut();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions