Apache Spark, usage of yield.

0 votes

Can someone tell me what is the use of yield? I know it will print the loops iterating but can I know about it on the conceptual level?

Feb 21 in Apache Spark by Ganesh

edited Feb 21 112 views

1 answer to this question.

0 votes

Yield is used in sequence comprehensions. It is applied in combination with for-loop and writes a new element into the resulting sequence. Scala's for-loop comprehensions are syntactic for the composition of multiple operations with map, flatMap, and filter. Or foreach. Scala actually translates a for-expression into calls to those methods, so any class providing them, or a subset of them, can be used with for comprehensions

This : 

for(x <- c; if cond) yield {...} 

is translated on Scala 2.7 into

c.filter(x => cond).map(x => {...}) 

or, on Scala 2.8, into 

c.withFilter(x => cond).map(x => {...}) 

Simple example (from scala-lang)

/** Turn command line arguments to uppercase */

object Main {

  def main(args: Array[String]) {

    val res = for (a <- args) yield a.toUpperCase

    println("Arguments: " + res.toString)


answered Feb 21 by Saruj

Related Questions In Apache Spark

0 votes
1 answer

Error: value textfile is not a member of org.apache.spark.SparkContext

Hi, Regarding this error, you just need to change ...READ MORE

answered Jul 4 in Apache Spark by Gitika
• 19,720 points
0 votes
1 answer

Error : split value is not a member of org.apache.spark.sql.Row

spark.read.csv is used when loading into a ...READ MORE

answered 5 days ago in Apache Spark by Rishi
+1 vote
2 answers

Hadoop 3 compatibility with older versions of Hive, Pig, Sqoop and Spark

Hadoop 3 is not widely used in ...READ MORE

answered Apr 20, 2018 in Apache Spark by kurt_cobain
• 9,240 points
+1 vote
2 answers

Apache Spark vs Apache Spark 2

Spark 2 doesn't differ much architecture-wise from ...READ MORE

answered Apr 24, 2018 in Apache Spark by kurt_cobain
• 9,240 points
0 votes
0 answers
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 9,990 points
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 9,990 points
0 votes
10 answers

hadoop fs -put command?

copy command can be used to copy files ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Sujay
0 votes
7 answers

How to print the contents of RDD in Apache Spark?

Simple and easy: line.foreach(println) READ MORE

answered Dec 10, 2018 in Apache Spark by Kuber
+5 votes
11 answers

Concatenate columns in apache spark dataframe

its late but this how you can ...READ MORE

answered Mar 21 in Apache Spark by anonymous