Streams in Scala

How can I learn the shortest word in a file that I have taken with the url?

object Task2 extends App {

  def read_Url(url: String): Int = {
    var url_name = Source.fromURL(s"$url").getLines.toArray
    var size : Int = url_name.length
    return size


Welcome to Scala @kelted
You can use the method minBy with the length function on the list of words.

val url = ""
val contents = io.Source.fromURL(url).getLines
val shortestWord = contents.minBy(_.length)

I found that the shortest word is ad.

Notice that I’m using immutable vals which are preferred in Scala, instead of the vars you used. Also I’m using camelCase (like shortestWord) preferred in the Scala/Java world, instead of the snake_case you are using (popular in Python world). I also avoided the array (a mutable structure) because immutable structures are preferred in Scala.

The _.length is a function literal, which is short for word => word.length.

If you are coming from Python here is a useful article


Thank u @spamegg1 Actually i did not coming from pthython or the another programming langiage ,I am a student and we learn scala programming language. :slight_smile:

And I don’t have a programming background. I’ve been doing well so far, but nothing with these streams.


Nice to hear that you are a beginner student of Scala!

I’m a professor at the dept of computer science at Lund University in Sweden, with a special interest in Scala as a beginner language, and I’m curious which university you are attending?

We have a special forum for teachers of Scala so maybe you can tell your teacher about our teacher community, if your university is not already participating here:
Teacher community:

Here is a page about Scala as a tool for teaching including a map of universities using Scala:

We want to enlarge the Scala community by attracting more beginner learners and teachers of Scala!


I just saw your comment. i am a software engineering student at an engineering school in switzerland . I will forward what you spread to the teacher of the programming course.

I would also like to meet you, if it is convenient for you @bjornregnell

1.)What is the longest word in the file? Again, if several words have the same size, we take the first one.of these words ?

// I tried to use maxBy as you typed just to find the longest word but it didn’t work?

“”“xception in thread “main” java.lang.UnsupportedOperationException: empty.maxBy
at scala.collection.IterableOnceOps.maxBy(IterableOnce.scala:1011)
at scala.collection.IterableOnceOps.maxBy$(IterableOnce.scala:1009)
at scala.collection.AbstractIterator.maxBy(Iterator.scala:1300)


I am getting an error? Why so ?

2.) Which word contains the letter ‘a’ the most times?

I also got your error. It seems like contents cannot be used more than once. It gets emptied after being iterated through once. We should convert it to, say, a list. Then it can be used many times.

val url = ""
lazy val contents = io.Source.fromURL(url).getLines.toList
val shortestWord = contents.minBy(_.length)
val longestWord = contents.maxBy(_.length)

If I load this into the REPL I get:

scala> :l test.scala
val url: String =
lazy val contents: List[String]
val shortestWord: String = ad
val longestWord: String = intergouvernementalisations

Think about it. You can still use maxBy. But you have to use a different function instead of _.length. What kind of function can count the number of times 'a' occurs in a word?

1 Like

Okay. i will be write again , when i found the fonction.( I hope ) :slight_smile: