_take

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

Def

inline def take(f: A => Boolean): Stream[A]

Main filter

Main filter

Only takes Stream elements satisfying the given function

  (0 <>> 10).stream.take(_ > 5).tp

  // Output
  Stream(6, 7, 8, 9)

Note: Traditional method filter is also available and can be used, but take is prefferable in most cases.

Source
_take.scala
inline def TAKE(f: A => Boolean): Stream[A]

Heavy filter

Heavy filter

Filters Stream elements according to given function

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

Source
_take.scala
inline def takeDuplicates: Stream[A]

Duplicates filter

Duplicates filter

Takes only elements equal to the passed in prior position

Note: To generally get all duplicates, the stream must be sorted to arrange them in sequence

   Stream(1,1,2,3,3,4,5,5,5).takeDuplicates.tp

   // Output
   Stream(1, 3, 5, 5)
Source
_take.scala
inline def takeDuplicatesBy[B](f: A => B): Stream[A]

Mapped duplicates filter

Mapped duplicates filter

Takes only elements, which evaluate to the same value as elements passed in prior position

Note: To generally get all duplicates, the stream must be sorted by the mapping function

  (0 <> 10).stream.takeDuplicatesBy(_ / 2).tp

  // Output
  Stream(1, 3, 5, 7, 9)
Source
_take.scala
inline def takeEvery(nTh: Int): Stream[A]

Every Nth element filter

Every Nth element filter

Only lets every nTh element

  (1 <> 20).stream.takeEvery(4).tp   // Prints: Stream(4, 8, 12, 16, 20)
Source
_take.scala
inline def takeFirst(n: Int): Stream[A]

Head filter

Head filter

Only takes given number of first elements

  (1 <> 10).stream.takeFirst(3).tp  // Prints  Stream(1, 2, 3)
Source
_take.scala
inline def takeIndexed(f: (Int, A) => Boolean, start: Int): Stream[A]

Indexed filter

Indexed filter

Only lets elements satisfying the given function, which also accepts element sequential index

  ('a' <> 'z').stream.takeIndexed((i, _) => i >= 2 && i <= 7, 1).tp

  // Output
  Stream(b, c, d, e, f, g)

Note: By default indexing starts from 0, but starting value can also be explicitly specified.

Source
_take.scala
inline def takeLast(n: Int): Stream[A]

Tail filter

Tail filter

Only takes given number of elements coming last

  (1 <> 10).stream.takeLast(3).tp  // Prints  Stream(8, 9, 10)

Note: This method will block on unlimited streams

Source
_take.scala
inline def takeOnly(v: A): Stream[A]

Single value filter

Single value filter

Filters only specified value.

  (0 <>> 10).stream.takeOnly(5).tp

  // Output
  Stream(5)

Note: takeOnly is more efficient than general filter ".take(_ == value)", because there is no function involved.

Source
_take.scala
inline def takeRange(i: Int.Range): Stream[A]

Range filter

Range filter

Only allows elements withing specified sequencial range

  ('a' <> 'z').stream.takeRange(1 <> 7).tp

  // Output
  Stream(b, c, d, e, f, g, h)

Note: Range indexing starts from 0

Source
_take.scala
inline def takeType[B](using t: scala.reflect.ClassTag[B]): Stream[B]

Type filter

Type filter

Only lets elements of specified type

  Stream(1, '2', "3", new Object(), 0.0).takeType[String].tp  // Prints: Stream(3)
Source
_take.scala
inline def takeValues(v: Stream[A]): Stream[A]

Multi value filter

Multi value filter

Takes only provided set of values

    ('a' <> 'z').stream.takeValues('z','x','b').tp   // Prints Stream('b','x','y')

    ('a' <> 'z').stream.takeValues('b' <> 'f').tp    // Prints Stream('b','c','d','e','f')

Note: takeValues is macro optimized when given value tuples sized from 2 to 5

Source
_take.scala
inline def takeValuesBy[B](f: A => B, v: Stream[B]): Stream[A]

Mapped multi value filter

Mapped multi value filter

Takes only values, which convert to provided set of values

  (0 <>> 10).stream.takeValuesBy(_ % 5, (1,3) ).tp

  // Output
  Stream(1, 3, 6, 8)

Note: takeValuesBy is macro optimized when given value tuples sized from 2 to 5

Source
_take.scala
inline def takeWhile(f: A => Boolean): Stream[A]

Conditional head filter

Conditional head filter

Only takes first consecutive elements satisfying the condition

  def stream = (1 <> 5).stream ++ (1 <> 5)

  stream.tp                     // Prints Stream(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

  stream.takeWhile(_ <= 3).tp    // Prints Stream(1, 2, 3)

Note: Everything starting from the first non compliant element will be discarded (including later compliant elements)

Source
_take.scala