object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list : Int = List(1:input)
for (i <- list if i % 2 == 0)
yeild i
println("even :" i)
val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
for {
if (strigList startWith "P")
print(stringList)
} yield stringList
}
we are getting below error as
$ scalac ForComprehensions1.scala
ForComprehensions1.scala:3: error: not found: type input
val list : Int = List(1:input)
^
ForComprehensions1.scala:4: error: value filter is not a member of Int
for (i <- list if i % 2 == 0)
^
ForComprehensions1.scala:6: error: value i is not a member of String
println(âeven :â i)
^
three errors found
ForComprehensions1.scala:10: error: illegal start of simple pattern
if (stringList startsWith âPâ)
^
ForComprehensions1.scala:11: error: â<-â expected but â;â found.
print(stringList)
^
ForComprehensions1.scala:12: error: â<-â expected but â}â found.
} yield stringList
^
three errors found
You have also misspelled yield. And you clearly donât mean if (stringList startsWith âPâ) - you want entries of the list to start with âPâ. The correct code begins with:
for {string <- stringList
if (string startsWith âPâ)
...
} ...
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yeild i
println("even :" i)
val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
for {string <- stringList
if (string startWith "P")
} yield string
println(string)
}
error as
$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: value i is not a member of String
println(âeven :â i)
^
ForComprehensions1.scala:10: error: value startWith is not a member of String
if (string startWith âPâ)
^
ForComprehensions1.scala:12: error: not found: value string
println(string)
^
three errors found
It is startsWith not startWith, should be println("even :" + i) (or better println("even :" + i.toString) or use string interpolation). And stringis a local variable inside thefor`, you should define a list variable and use it.
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i
println("even :" + i.toString)
val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
for {string <- stringList
if (string starstWith "P")
println(string)
} yield string
}
error as
$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: not found: value i
println(âeven :â + i.toString)
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i
println("even :" + i.toString)
val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
for {
string <- stringList
if (string startsWith "P")
} yield string
}
error as
$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: not found: value i
println(âeven :â + i.toString)
^
one error found
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i
val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
for {
string <- stringList
if (string startsWith "P")
} yield string
}
It is working fine but there is no output, we need output
It isnât printing any values becauseâŚyou never ask it to?
If you want to print i + 5, maybe you should try println(i + 5) instead of yield i + 5. Yield returns the result.
val lengths = for (i <- list) yield i + 5
You might want to consult a book to familiarize yourself with the basics of Scala syntax. Itâs going to be very painful to try to get your advice bit by bit on a forum! There are a bunch of good onesâŚScala for the Impatient is, as the name suggests, pretty good if you want to get going in a hurry.
Thanks for ur advice , we are able to get output but could not pass Lab test. Our task is
Declare an implicit integer variable 5. Generate a List with input1 such that the number of elements is equal to input1 , and the numbers are from 1 to input1 (when input is 3 -> List(1,2,3)). Write a curried function add which takes two integer variables in which one variable is an implicit variable. Using a for comprehension , apply the add function on the generated list and print the result.
We written code as
object Demo extends App {
val input1 :Int = args(0).toInt
val list = (1 to input1).toList
for (i <- list)
println(i + 5)
}
Working code is:
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i â list if i % 2 == 0)
println("even: " + i)
val stringList :List[String] = List(âScalaâ, âRubyâ, âPythonâ, âC#â, âJavaâ, âGroovyâ, âJavaScriptâ, âPHPâ, âHaskellâ)
for {
string â stringList
if (string startsWith âPâ)
} println(string)
}
@ashishsme14 - You should talk to your teaching assistant or whoever else can help you with the class, if there is such a person available. Reading a solution with full code here is only going to impair your understanding. Programming is a skillâyou canât build it effectively by having others give you the answers, just like you canât much get stronger by watching others lift weights, or better at darts by watching people play.
Figure out what you donât know, and ask targeted questions hereâŚor talk to someone teaching the class who might have a better idea of what youâre expected to know and what youâre having trouble with. And then do it yourself! And understand what youâre doing.
For instance, look at the requirements:
Declare an implicit variable 5. Youâre not doing this. Do you know what an implicit variable is? Do you know how to declare one?
Generate a list with input1 such that the number of elements is equal to input1 and the numbers are from 1 to input1. You are doing thisâa previous answer told you how. Do you understand why it works?
Write a curried function add which takes two integer variables in which one variable is an implicit variable. Do you know how to create a function (or method, actually, in this case)? Do you know how to specify variables? Do you know how to make one implicit? Do you know what they mean by âcurried functionâ?
Using a for comprehension, apply the add function on the generated list and print the result. You have the for comprehension, but youâre using + instead of an add function. Youâre also printing.
Anyway, this assignment requires you to have basic familiarity with a range of concepts in Scala, and if you happen to see code that solves the assignment but you donât get the concepts, you are not accomplishing the learning youâre supposed to!
If you donât understand the individual concepts, go back and study them until you do!
object Demo extends App {
val input1 :Int = args(0).toInt
val list = (1 to input1).toList
implicit val test = 5
def add(x : Int)(implicit y: Int = 5) = x + y
for (i <- list)
println(i + 5)
}
@ashishsme14 - You missed the part where you use the add method in the for comprehensionâinstead of i + 5 you should use the add method. (What argument(s) should you pass to add? How would it be different if test were not implicit, and add did not take an implicit parameter?)
First of all, i would suggest you start with the syntax shown below
import org.scalatest.flatspec.AnyFlatSpec
class FirstSpec extends AnyFlatSpec {
// tests go hereâŚ
}
I agree scala could be quite confusing while doing particular lab tests. Therefore I would suggest you opt different testing strategies like akka framework or test automation. Sometimes you need to test whether a method throws an expected exception under certain circumstances and these framework or strategies will be a much better option.