_mutate

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

Def

def enablePreview[A]: ~[A] & ~~.Preview[A]

Enables preview capabilities

Enables preview capabilities

Returns ~~.Preview, which allows to pre-load and inspect elements, even before they are read

  def strm : ~[String] = ???

  if(strm.enablePreview.previewSize > 1000) "Stream is over 1K".TP
Source
_mutate.scala
def enableSize[A]: ~[A] & Able.Size

Adds sizing information

Adds sizing information

If Stream already has sizing, this method is a simple cast, otherwise, the elements might be buffered and counted.

Source
_mutate.scala
def hideSizeData[A]: ~[A]

Loose size information

Loose size information

Many streams return ''sizeLong_?'', knowing their current size

hideSizeData drops sizing information, so some optimizations will not be available

This is primarily for testing and debugging

Source
_mutate.scala
def load[A]: ~[A] & Able.Size

Preload all

Preload all

Immediately loads all stream elements into memory, so they are no longer dependent on underlying sources.

  def strm : ~[String] = ???

  strm.load

  // is functionally same as

  strm.toBuffer.~
Source
_mutate.scala
@scala.annotation.targetName("nonEmpty_Opt")
def nonEmpty_?[A]: Opt[~[A]]

Non empty option Optionally returns stream, if it is not empty.

Non empty option Optionally returns stream, if it is not empty.

Note: If stream does not have sizing information, this method will immediately preload at lease the first element.

Source
_mutate.scala
inline def raw[A, STM <: ~~.RawType[A]](using inline s: Given.StreamTag.Raw[A, ~~._build._mutate.STM]): STM

Specialized stream

Specialized stream

Converts current stream into specialized on underlying primitive data. If stream is already specialized, the conversion is a simple cas t.

   val s  : ~[Int] = 1 <> 10

   val ss : Int.~  = s.raw

Note: If underlying type is not primitive, the method will not compile

Source
_mutate.scala
inline def ref[A]: ~[A]

Generalize

Generalize

If stream is specialized it will be up-cast to general Val.Stream type, and further operations will be general (unless they are specialized, like map)

  val special : Int.><  = (1 <> 10).~.><

  val general : ><[Int] = (1 <> 10).~.ref.><

  special.getClass.tp // Prints class scalqa.lang.int.g.Pack

  general.getClass.tp // Prints class scalqa.val.pack.z.ArrayPack

Note: This is a true zero cost operation. It does not change byte code (only compiler context)

Source
_mutate.scala
inline def reverse[A]: ~[A]

Reverse order

Reverse order

Re-arranges all elements is reverse order

('A' <> 'F').~.reverse.tp  // Prints ~(F, E, D, C, B, A)
Source
_mutate.scala
inline def reverseEvery[A](inline size: Int): ~[A]

Reverse order in segments

Reverse order in segments

Reverses order of elements within segments of fixed size

(1 <> 15).~.reverseEvery(5).tp

(1 <> 15).~.reverseEvery(5).reverseEvery(3).reverseEvery(7).tp

// Output
~(5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 15, 14, 13, 12, 11)

~(7, 2, 1, 10, 5, 4, 3, 12, 11, 6, 15, 14, 9, 8, 13)

Use Case: Predefined Shuffle

For testing purposes it is often required to get elements in random order. However the order cannot be completely random, if we want to replicate bugs

reverseEvery can shuffle elements in a predefined order which looks random

Source
_mutate.scala
inline def shuffle[A]: ~[A]

Randomize order

Randomize order

Re-arranges elements is random order

Source
_mutate.scala
inline def transpose[A](using inline f: A => ~[B]): ~[~[B]]

Transpose

Transpose

Transposes matrix where rows become columns

 def stream : ~[~[Int]] = ~~(11 <> 15,
                             List(21, 22, 23, 24, 25),
                             Vector(31, 32, 33, 34, 35))

 stream.print

 stream.transpose.print

 // Output
 ---------------------
 ?
 ---------------------
 ~(11, 12, 13, 14, 15)
 ~(21, 22, 23, 24, 25)
 ~(31, 32, 33, 34, 35)
 ---------------------

 -------------
 ?
 -------------
 ~(11, 21, 31)
 ~(12, 22, 32)
 ~(13, 23, 33)
 ~(14, 24, 34)
 ~(15, 25, 35)
 -------------
Source
_mutate.scala