Stream.apex

  • Docs
  • Tutorials
Docs Menu
  • Streams
  • Methods
    • Creation Methods
    • Functions
    • Subscription Methods
    • Operation Methods
  • Subject

Operation Methods Guide

Operation Methods

concatOther

Concatenate the other streams

Stream.of(1)
    .concatOther(new List{ Stream.with(2, 3) })
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// 3
// Completed
MethodDescription
concatOther(List)Concat list of streams
concatOther(Stream)Concat one stream
concatOther(Stream, Stream)Concat two streams
concatOther(Stream, Stream, Stream)Concat three streams

mapBy

Map the Func over the stream elements

Stream.with(1, 2, 3)
    .mapBy(R.inc)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 2
// 3
// 4
// Completed

flatMap

Map the Func and flatten the stream

Stream.with(1, 2, 3)
    .flatMap(Stream.Funcs.ofData.apply(4))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 4
// 4
// 4
// Completed

mapTo

Map the element to the value

Stream.with(1, 2, 3)
    .mapTo(4)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 4
// 4
// 4
// Completed

filter

Filter the stream

Stream.with(1, 2, 3)
    .filter(R.equals.apply(2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 2
// Completed

scan

Similar to 'reduce', but emit the intermediate results

Stream.with(1, 2, 3)
    .scan(R.add, 0)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 3
// 6
// Completed

reduce

Reduce over the stream elements

Stream.with(1, 2, 3)
    .reduce(R.add, 0)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 6
// Completed

catchError

Catch the error and return the recovered value

Stream.throwError('error')
    .catchError(R.constant.apply('success'))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// success
// Completed

count

Get the count of the stream items

Stream.with(1, 2, 3)
    .count()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 3
// Completed
MethodDescription
count()Count all
count(Func)Count all that matches the predicate

defaultIfEmpty

Set the default value if the stream is empty

Stream.empty()
    .defaultIfEmpty(1)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// Completed

distinct

Get a stream of distinct values, according to the key calculated by the Func

Stream.with(1, 2, 3)
    .distinct(R.mod.apply(R.placeholder, 2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// Completed
MethodDescription
distinct()Get a stream of distinct values
distinct(Func)Get distinct values based on the compare Func that returns Boolean

distinctUntilChanged

Get a stream of values, distinct from the last one

Stream.with(1, 2, 1)
    .distinctUntilChanged(R.equals, R.identity)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// 1
// Completed
MethodDescription
distinctUntilChanged(Func, Func)Get distinct values based on compare Func and selector Func
distinctUntilChanged(Func)Get distinct values based on selector Func
distinctUntilChanged()Get distinct values

distinctUntilKeyChanged

Get a stream of values, distinct from the last one by key value

Stream.with(new Map{ 'name' => 'a' }, new Map{ 'name' => 'a' })
    .distinctUntilKeyChanged('name', R.equals)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// {name=a}
// Completed
MethodDescription
distinctUntilKeyChanged(String, Func)Get distinct values based on key and compare Func
distinctUntilKeyChanged(String)Get distinct values based on key

elementAt

Get the Nth element, return default value if not found

Stream.with(1, 2, 3)
    .elementAt(3, 4)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 4
// Completed
MethodDescription
elementAt(Integer, Object)Get element at the index with default value
elementAt(Integer)Get element at the index

every

Check if every element matches the predicate

Stream.with(1, 2, 3)
    .every(R.lt.apply(R.placeholder, 4))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// true
// Completed

join

Flatten nested Streams

Stream.with(1, Stream.of(2), 3)
    .join()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// 3
// Completed

finalize

Register a Func that will be called either after completion or error

Stream.with(1, 2, 3)
    .finalize(R.debug.apply(4))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// 3
// 4
// Completed

find

Find the first element that matches the predicate

Stream.with(1, 2, 3)
    .find(R.equals.apply(2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 2
// Completed

findIndex

Find the index of the first element that matches the predicate

Stream.with(1, 2, 3)
    .findIndex(R.equals.apply(2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// Completed

first

Find the first element that matches the predicate, otherwise return the default value

Stream.with(1, 2, 3)
    .first(R.equals.apply(4), 4)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 4
// Completed
MethodDescription
first(Func, Object)Find the first matching with default value
first(Func)Find the first matching

last

Find the last element that matches the predicate, otherwise return the default value

Stream.with(1, 2, 3)
    .last(R.equals.apply(4), 4)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 4
// Completed
MethodDescription
last(Func, Object)Find the last matching with default value
last(Func)Find the last matching

groupBy

Group the elements into a map

Stream.with(1, 2, 3)
    .groupBy(R.mod.apply(R.placeholder, 2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// {0=(2), 1=(1, 3)}
// Completed

ignoreElements

Create a stream that ignores all elements

Stream.with(1, 2, 3)
    .ignoreElements()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// Completed

isEmpty

Check if the stream is empty

Stream.with(1, 2, 3)
    .isEmpty()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// false
// Completed

max

Get the max element according to the comparator Func that returns an Integer

Stream.with(1, 2, 3)
    .max(R.compare)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 3
// Completed
MethodDescription
max(Func)Get max value with the comparator Func returning Integer
max()Get the max value

min

Get the min element according to the comparator Func that returns an Integer

Stream.with(1, 2, 3)
    .min(R.compare)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 3
// Completed
MethodDescription
min(Func)Get min value with the comparator Func returning Integer
min()Get the min value

onErrorResumeNext

Like 'concat', but continue to the next stream only when the first stream has errors

Stream.throwError('error')
    .onErrorResumeNext(new List{ Stream.of('success') })
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// success
// Completed

pairwise

Make pairs from adjacent stream items

Stream.with(1, 2, 3)
    .pairwise()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// Pair:[fst=1, snd=2]
// Pair:[fst=2, snd=3]
// Completed

pluck

Pluck the value from the stream elements

Stream.with(new Map{ 'name' => 'a' })
    .pluck('name')
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// a
// Completed

repeat

Repeat the stream for N times

Stream.with(1, 2, 3)
    .repeat(2)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// 3
// 1
// 2
// 3
// Completed

single

Get the single element from the stream that matches the predicate

Stream.with(1, 2, 3)
    .single(R.equals.apply(2))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 2
// Completed

skip

Skip N elements from the stream

Stream.with(1, 2, 3)
    .skip(2)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 3
// Completed

skipLast

Skip N elements from the last of the stream

Stream.with(1, 2, 3)
    .skipLast(2)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// Completed

skipWhile

Skip elements while the predicate is true

Stream.with(1, 2, 3)
    .skipWhile(R.lt.apply(R.placeholder, 3))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 3
// Completed

take

Take the first N elements from the stream

Stream.with(1, 2, 3)
    .take(2)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// Completed

takeLast

Take the last N elements from the stream

Stream.with(1, 2, 3)
    .takeLast(2)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 2
// 3
// Completed

takeWhile

Take elements while the predicate is true

Stream.with(1, 2, 3)
    .takeWhile(R.lt.apply(R.placeholder, 3))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 2
// Completed

startWith

Append the other stream to the stream

Stream.with(1, 2, 3)
    .startWith(Stream.of(0))
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 0
// 1
// 2
// 3
// Completed

tap

Pass the value through the Func

Stream.with(1, 2, 3)
    .tap(R.debug)
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// 1
// 1
// 2
// 2
// 3
// 3
// Completed

toArray

Convert the strema elements to a list

Stream.with(1, 2, 3)
    .toArray()
    .subscribe(R.debug, R.debug.apply('Error: '), R.debug.apply('Completed'));
// (1, 2, 3)
// Completed

Contribute on Github! Edit this section.