Skip to main content
added 2 characters in body
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

 Max length=3, words are "mnp","pqr","jkl","abc","xyz"
    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(","))

(With an Ideone running the code)

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(","))

(With an Ideone running the code)

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

Given the list below, the expected output is

 Max length=3, words are "mnp","pqr","jkl","abc","xyz"
    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(","))

(With an Ideone running the code)

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

Post Reopened by toto2, rolfl
Make the code actually work (add missing parenthesis)
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(","))

(With an Ideone running the code)

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(",")

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(","))

(With an Ideone running the code)

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

added 119 characters in body
Source Link

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

So// Given the list below, herethe expected output is what I have done:
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

// Given the list above, the expected output is 
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)

What I get is this:

// Map(  
//       2 -> Set((2,be), (2,ab), (2,bd), (2,bf), (2,bc), (2,ac)),  
//       1 -> Set((1,a), (1,d), (1,b)),  
//       3 -> Set((3,mnp), (3,pqr), (3,jkl), (3,abc), (3,xyz))  
//    )

I then flatten the Map, and extract the entry with highest key thus:

    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(",")

This works for me but I have this nagging feeling that mine is using too many operators/functions and perhaps mine is inefficient also. Do you guys think so? What is a better alternative?

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length.

So, here is what I have done:

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

// Given the list above, the expected output is 
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)

What I get is this:

// Map(  
//       2 -> Set((2,be), (2,ab), (2,bd), (2,bf), (2,bc), (2,ac)),  
//       1 -> Set((1,a), (1,d), (1,b)),  
//       3 -> Set((3,mnp), (3,pqr), (3,jkl), (3,abc), (3,xyz))  
//    )

I then flatten the Map, and extract the entry with highest key thus:

    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(",")

This works for me but I have this nagging feeling that mine is using too many operators/functions and perhaps mine is inefficient also. Do you guys think so? What is a better alternative?

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.

// Given the list below, the expected output is
// Max length=3, words are "mnp","pqr","jkl","abc","xyz"

    val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")

    val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
    val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head

    println("Max length =" + 
            longest._1 +
            ", words are" + 
            longest._2.map(nextEntry => nextEntry._2).mkString(",")

I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.

added 119 characters in body
Source Link
Loading
Thanks for pointing out the areas of incompleteness in the code. I have tried to correct them. I assumed things while writing the post, which I should not had.
Source Link
Loading
deleted 13 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
lengths -> length, which does clarify the meaning of the question.
Source Link
toto2
  • 5.4k
  • 16
  • 21
Loading
Post Closed as "Needs details or clarity" by 200_success
Source Link
Loading