Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What foreach(_ => ()) this mean in Scala?

Tags:

scala

I found this foreach(_ => ()) in a code that uses Monix stream, I am not understanding the meaning. Can someone explain me this?

monix_concurrent_subject.foreach(_ => ())

like image 993
marla Avatar asked Feb 03 '26 09:02

marla


1 Answers

As urmaul explained in the comments, it depends:

  • If the datastructure is eager, it does nothing. Examples are List, Option etc.

  • If the datastructure is lazy, it initialises its content. An example is a Stream.

Also as foreach returns nothing, it implies that somewhere will be a Side-Effect in the code before the foreach.

If you check the API (ConcurrentSubject.html#foreach) it states:

foreachL(cb: (O) ⇒ Unit): Task[Unit] Creates a new Task that will consume the source observable, executing the given callback for each element.

like image 159
pme Avatar answered Feb 05 '26 23:02

pme