Skip to content

Working with Primitives in Streams

johnmcclean-aol edited this page Dec 12, 2016 · 1 revision

Primitive operations

ReactiveSeq and LazyFutureStream have methods

  • ints
  • doubles
  • longs

Which convert the current Stream to a PrimitiveStream and allow a set of user defined operations to be executed on the PrimitiveStream before it is converted back to a ReactiveSeq / LazyFutureStream

ReactiveSeq.rangeLong(1, 1000)
           .longs(i->i,
                  s->s.map(i->i*2)
                      .filter(i->i<500))
           .size()

Cyclops uses primitive Spliterators where appropriate, if primite Spliterators are used during Stream creation they will passed into the primitive operator (ints, longs, doubles) of the same type if it is called immediately. Otherwise a conversion function will be used to convert the data to primitive form. The specialized primitive creation operators are range, rangeLong, ofInts, ofDoubles and ofLongs. Spliterator and fromStream can also be used to pass in Primitive Spliterators.

Primitive folds

ReactiveSeq and LazyFutureStream have methods

  • foldInt
  • foldLongs
  • foldDoubles

These methods allow users to chain together operations over primitive Streams that result in a single reduced value (via primitive Streams terminal operators).

ReactiveSeq.range(1, 1000)
           .foldInt(i->i,
                    s->s.map(i->i*2)
                        .filter(i->i<500)
                        .average()
                        .getAsDouble());
Clone this wiki locally