_take

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

Def

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

Main filter

Main filter

Only takes ~ elements satisfying the given function

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

  // Output
  ~(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[A](inline f: A => Boolean): ~[A]

Heavy filter

Heavy filter

Filters ~ 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
@scala.annotation.targetName("take_Range")
inline def take_<>[A](inline i: Int.<>): ~[A]

Range filter

Range filter

Only allows elements withing specified sequencial range

  ('a' <> 'z').~.take_<>(1 <> 7).tp

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

Note: Sequence indexing starts from 0

Source
_take.scala
def takeAll[A](v: ~[A]): ~[A]

Multi element filter

Multi element filter

Only lets elements equal to the found in given stream

  ('a' <> 'z').~.takeAll(~~('z','x','b')).tp   // Prints ~('b','x','y')

  ('a' <> 'z').~.takeAll('b' <> 'f') .tp       // Prints ~('b','c','d','e','f')
Source
_take.scala
def takeAllBy[A](f: A => B, v: ~[B]): ~[A]

Multi element mapped filter

Multi element mapped filter

Only lets elements, which mapped value is found in given stream

  ('a' <> 'z').~.takeAllBy(_.Int % 10, ~~(1,3,7)).tp

  // Output
  ~(a, e, g, k, o, q, u, y)
Source
_take.scala
inline def takeEvery[A](inline nTh: Int): ~[A]

Every Nth element filter

Every Nth element filter

Only lets every nTh element

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

Head filter

Head filter

Only takes given number of first elements

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

Indexed filter

Indexed filter

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

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

  // Output
  ~(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[A](inline n: Int): ~[A]

Tail filter

Tail filter

Only takes given number of elements coming last

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

Note: This method will block on unlimited streams

Source
_take.scala
def takeOnly[A](v: A, vs: A*): ~[A]

Single value filter

Single value filter

Filters only specified value.

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

  // Output
  ~(5)

Note: takeOnly is more efficient than general filter, because there is no function involved.

Source
_take.scala
def takeOnlyBy[A](f: A => B, v: B*): ~[A]
inline def takeType[A](using inline t: scala.reflect.ClassTag[B]): ~[B]

Type filter

Type filter

Only lets elements of specified type

  ~~(1, '2', "3", new Object(), 0.0).takeType[String].tp  // Prints: ~(3)
Source
_take.scala
inline def takeWhile[A](inline f: A => Boolean): ~[A]

Conditional head filter

Conditional head filter

Only takes first consecutive elements satisfying the condition

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

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

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

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

Source
_take.scala