_group

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

Def

inline def group[A]: ~[~[A]]

Simple grouping

Simple grouping

Puts consecutive elements in the same group if they are equal

   def stream =  ~~(1, 2, 3).repeat(3)

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

   stream.group.print  // Prints  ------------
                                ?
                                ------------
                                ~(1, 1, 1)
                                ~(2, 2, 2)
                                ~(3, 3, 3)
                                ------------

Note: Non consecutive equal elements will end up in different groups. Prior ordering might be needed

Source
_group.scala
inline def group[A](inline f: (A, A) => Boolean, inline peek: (A, Boolean) => U): ~[~[A]]

Group by test

Group by test

Puts elements in the same group based on a function test for every two consecutive elements

   // Putting Ints into groups of 3

   (0 <> 20).~.group(_ / 3 == _ / 3).print

   // Output
   ---------------
   ?
   ---------------
   ~(0, 1, 2)
   ~(3, 4, 5)
   ~(6, 7, 8)
   ~(9, 10, 11)
   ~(12, 13, 14)
   ~(15, 16, 17)
   ~(18, 19, 20)
   ---------------
Value Params
f

function for two consecutive elements. if 'false' is returned, the second tested element will start a new group

peek

side-effect convenience function will run for each element. Boolean parameter indicates if the element starts a new group

Source
_group.scala
inline def groupBy[A](inline f: A => Any, inline more: A => Any*): ~[~[A]]

Grouping on properties

Grouping on properties

Puts consecutive elements in the same group if all the specified properties are equal

When properties change, a new group is started

    ('#' <> '|').~.groupBy(_.isLetter, _.isDigit).print

   // Output
   ---------------------------------------------------------------------------------
   ?
   ---------------------------------------------------------------------------------
   ~(#, $, %, &, ', (, ), *, +, ,, -, ., /)
   ~(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
   ~(:, ;, <, =, >, ?, @)
   ~(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)
   ~([, \, ], /\, _, `)
   ~(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, p, t, u, v, w, x, y, z)
   ~({, ~)
   ---------------------------------------------------------------------------------
Value Params
properties

a set of functions, each indicating element property

Source
_group.scala
inline def groupEvery[A](inline cnt: Int): ~[~[A]]

Fixed size groups

Fixed size groups

Puts consecutive elements into fixed size groups

('a' <> 'z').~.groupEvery(8).print

// Output
-------------------------
?
-------------------------
~(a, b, c, d, e, f, g, h)
~(i, j, k, l, m, n, o, p)
~(q, r, p, t, u, v, w, x)
~(y, z)
-------------------------
Source
_group.scala
inline def groupWith[A](f: A => B): ~[(B, ~[A])]

Grouping on a property

Grouping on a property

Puts consecutive elements in the same group if their properties are equal

  (0 <> 20).~.groupWith(_ / 3).print

  // Output
  -- -------------
  _1 _2
  -- -------------
  0  ~(0, 1, 2)
  1  ~(3, 4, 5)
  2  ~(6, 7, 8)
  3  ~(9, 10, 11)
  4  ~(12, 13, 14)
  5  ~(15, 16, 17)
  6  ~(18, 19, 20)
  -- -------------

Note: groupWith also returns the groupped property value (unlike groupBy)

Value Params
properties

a set of functions, each indicating an element property

Source
_group.scala
inline def partition[A](inline p: A => Boolean, inline more: A => Boolean*): ~[~[A]]

Predicate grouping

Predicate grouping

All stream elements are grouped by given predicates, which are applied in sequence. Thus if an element is accepted into a group, it will not be evaluated by the rest of the filters.

The resulting stream size will be equal to the number of predicates plus one. The last group will hold spill over elements, not accepted by any predicate. Groups can be empty.

val ~~(odd,even) = (1 <> 10).~.partition(_ % 2 == 1)

odd.tp
even.tp

// Age groups
(1 <> 80).~.partition(_ <= 12, _ in 13 <> 19, _ < 30, _ in 30 <> 40, _ < 50, _ < 65).print
// Output
~(1, 3, 5, 7, 9)
~(2, 4, 6, 8, 10)

-------------------------------------------------------------------
?
-------------------------------------------------------------------
~(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
~(13, 14, 15, 16, 17, 18, 19)
~(20, 21, 22, 23, 24, 25, 26, 27, 28, 29)
~(30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40)
~(41, 42, 43, 44, 45, 46, 47, 48, 49)
~(50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64)
~(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80)
-------------------------------------------------------------------
Source
_group.scala
inline def sliding[A](inline size: Int, inline step: Int): ~[~[A]]

Sliding group view

Sliding group view

Example: group size 3 with step 1

 ('a' <> 'g').~.sliding(3).print

 // Output
 ----------
 ?
 ----------
 ~(a, b, c)
 ~(b, c, d)
 ~(c, d, e)
 ~(d, e, f)
 ~(e, f, g)
 ----------

Example: group size 4 with step 2

 ('a' <> 'g').~.sliding(4,2).print

 // Output
 -------------
 ?
 -------------
 ~(a, b, c, d)
 ~(c, d, e, f)
 ~(e, f, g)
 -------------
Source
_group.scala
inline def splitAt[A](inline positions: Int*): ~[~[A]]

Positional split

Positional split

Splits ~ at specified positions

val ~~(s1, s2, s3) = (0 <> 20).~.splitAt(5, 15)

s1.tp   // Prints ~(0, 1, 2, 3, 4)
s2.tp   // Prints ~(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
s3.tp   // Prints ~(15, 16, 17, 18, 19, 20)

Note. The same could be accomplished with read_~

```

val s3 = (0 <> 20).~ val s1 = s3.read_~(5) val s2 = s3.read_~(10) ```

Source
_group.scala