_map

trait _map[A]
class java.lang.Object
trait scala.Matchable
class Any
trait _build
object Stream

Def

inline def collect[B](f: scala.PartialFunction[A, B]): Stream[B]

Partial map

Partial map

Creates a new Stream by applying a partial function to all elements of current Stream on which the function is defined.

(0 <>> 26).stream.collect{
 case i if(i%2==0) => ('a' + i).toChar
}.tp

// Output
Stream(a, c, e, g, i, k, m, o, q, s, u, w, y)

Note:

  • collect is functionally similar to mapOpt, which is prefferable in most cases.
  • 'partialMap' would be a better name for this operation, but 'collect' is an established Scala convention.
Source
_map.scala
inline def FLAT_MAP[B](f: A => Stream[B])(using s: Specialized[B]): s.Stream

Heavy flat map

Heavy flat map

FLAT_MAP is functionally equivalent to flatMap, but is fully inlined. It makes compiled code larger, but guarantees the best possible performance on large streams.

Source
_map.scala
inline def flatMap[B](f: A => Stream[B])(using s: Specialized[B]): s.Stream

Flat map

Flat map

Creates a new Stream by applying given function to all elements of current Stream and concatenating the results

(1 <> 3).stream.flatMap(i => Stream(i, i*10, i*100)).tp

// Output
Stream(1, 10, 100, 2, 20, 200, 3, 30, 300)
Source
_map.scala
inline def flatten[B](using d: Any.Def.ToStream[A, B], s: Specialized[B]): s.Stream

Converts a stream of streams into a flat stream

Converts a stream of streams into a flat stream

The operation will only compile if stream elements are streams or stream convertible entities, like Able.Stream, Iterable, Iterator, etc.

val vs: Stream[Stream[Char]] = Stream(
  'a' <> 'd',
  Pack('x', 'y', 'z'),
  Vector('v', 'e', 'c', 't', 'o', 'r'))

vs.flatten.tp // Prints Stream(a, b, c, d, x, y, z, v, e, c, t, o, r)
Source
_map.scala
inline def map[B](f: A => B)(using s: Specialized[B]): s.Stream

Simple map

Simple map

Creates a new Stream where each element is a result of applying given function to current Stream elements

(0 <>> 26).stream.map(i => ('a' + i).toChar).tp

// Output
Stream(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
Source
_map.scala
inline def MAP[B](f: A => B)(using s: Specialized[B]): s.Stream

Heavy map

Heavy map

MAP is functionally equivalent to map, but is fully inlined. It makes compiled code larger, but guarantees the best possible performance on large streams.

Source
_map.scala
inline def MAP_OPT(f: A => OPT)(using o: Specialized.Opt[B, OPT], s: Specialized[B]): s.Stream

Heavy optional map

Heavy optional map

MAP_OPT is functionally equivalent to mapOpt, but is fully inlined. It makes compiled code larger, but guarantees the best possible performance on large streams.

Source
_map.scala
inline def mapIf(condition: A => Boolean, fun: A => A): Stream[A]

Conditional map

Conditional map

This is a synthetic oeration which is inlined as:

map(v => if(condition(v)) fun(v) else v)

In some cicumstances using "mapIf" does not make sense, in some it is really usefull.

Source
_map.scala
inline def mapOpt[B,OPT<:Any.Opt[B]](f: A => OPT)(using s: Specialized[B]): s.Stream

Optional map

Optional map

Creates a new Stream where each element is a result of applying given function to Stream elements. If the function returns void option, the element is dropped.

(1 <> 10).stream.mapOpt(i => if(i % 2 == 0) "Even_"+i else VOID).tp

// Output
Stream(Even_2, Even_4, Even_6, Even_8, Even_10)

Pattern matching can be used, but the last void case must always be provided explicitly:

(0 <>> 26).stream.mapOpt{
 case i if(i % 2 == 0) => ('a' + i).toChar
 case _                => VOID
}.tp

// Output
Stream(a, c, e, g, i, k, m, o, q, s, u, w, y)

Note:

  • All cases must return the same type, otherwise the operation will not compile.
  • mapOpt is functionally similar to collect, but is faster (PartialFunction in collect has to be evaluated twice)
Source
_map.scala