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 173 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
• 25,340 points
123 views
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 Jul 10 in Apache Spark by Rishi
178 views
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 Jul 22 in Apache Spark by Firoz
203 views
+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,733 views
0 votes
1 answer
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
• 10,670 points
2,736 views
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
• 10,670 points
289 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
13,563 views
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
9,774 views
+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
28,572 views