_map

trait _map
class java.lang.Object
trait scala.Matchable
class Any
trait _build
object ~

Def

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

Partial map

Partial map

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

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

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

Note:

  • collect is functionally similar to map_?, 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[A, T, STM <: ~~.AnyType[T]](inline f: A => ~[B])(using inline s: Given.StreamTag[B, STM]): STM

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[A, T, STM <: ~~.AnyType[T]](inline f: A => ~[B])(using inline s: Given.StreamTag[B, STM]): STM

Flat map

Flat map

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

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

// Output
~(1, 10, 100, 2, 20, 200, 3, 30, 300)
Source
_map.scala
inline def flatten[A, T, STM <: ~~.AnyType[T]](using inline f: A => Able.~[T], inline s: Given.StreamTag[T, ~~._build._map.STM]): STM

Converts a stream of streams into a flat stream

Converts a stream of streams into a flat stream

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

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

Simple map

Simple map

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

(0 <>> 26).~.map(i => ('a' + i).Char).tp

// Output
~(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[A, T, STM <: ~~.AnyType[T]](inline f: A => B)(using inline s: Given.StreamTag[B, STM]): STM

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
@scala.annotation.targetName("map_Opt")
inline def map_?[A, T, STM <: ~~.AnyType[T]](inline f: A => OPT)(using inline s: Given.StreamTag.Opt[T, OPT, STM]): STM

Optional map

Optional map

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

(1 <> 10).~.map_?[Opt[String]](i => if(i>5) "Str_"+i else \/).tp

// Output
~(Str_6, Str_7, Str_8, Str_9, Str_10)

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

(0 <>> 26).~.map_?[Char.Opt]{
 case i if(i%2==0) => ('a' + i).Char
 case _            => \/
}.tp

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

Note:

  • map_? is functionally similar to collect, but is faster (PartialFunction has to be evaluated twice)
  • map_? can return specialized stream result, but boxing might happen during mapping
Source
_map.scala
@scala.annotation.targetName("MAP_Opt")
inline def MAP_?[A, T, STM <: ~~.AnyType[T]](inline f: A => OPT)(using inline s: Given.StreamTag.Opt[T, OPT, STM]): STM

Heavy optional map

Heavy optional 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