title: Parallel Stream in scala
body: <p>is there any (hopefully out of the box) way to traverse an Scala stream in parallel?</p>
  
  <p>for instance, see this java 8 code:</p>
  
  <pre><code>String[] s = {"a","b","c","d","e"};
  List&lt;String&gt; list = Arrays.asList(s);
  list.parallelStream().forEach(System.out::println);
  </code></pre>
  
  <p>this will print all list stream contents in parallel..
  however, to my understanding, streams in scala are sequential.</p>
  
  <p>any workarounds for this?</p>
  
  <p><strong>EDIT:</strong> 
  please notice that, streams allows us to process data as they arrive. then, if data is not necessary, remove them from memory.
  for instance:</p>
  
  <pre><code>"abcd".toStream.filter { x =&gt; 
    println(s"1 filter $x")  
     if(x.toInt%2==0) true;else false;
    } //end of first block
    .foreach { x =&gt; 
    println(s"2 filter-&gt;$x")  
    } //end of second block
  </code></pre>
  
  <p>will output something like this:</p>
  
  <p>1 filter a</p>
  
  <p>1 filter b</p>
  
  <p>2 filter->b</p>
  
  <p>1 filter c</p>
  
  <p>1 filter d</p>
  
  <p>2 filter->d</p>
  
  <p>on the other hand, the below code, will process data in blocks. keeping variables in memory on each transformation:</p>
  
  <pre><code>  "abcd".toVector.par.filter { x =&gt; 
    println(s"1 filter $x")  
     if(x.toInt%2==0) true;else false;
    } //end of first block
    .foreach { x =&gt; 
    println(s"2 filter-&gt;$x")  
    } //end of second block
  </code></pre>
  
  <p>output:
  1 filter c</p>
  
  <p>1 filter a</p>
  
  <p>1 filter b</p>
  
  <p>1 filter d</p>
  
  <p>2 filter->b</p>
  
  <p>2 filter->d</p>