Re: Scala beginner question
Pietro <[email protected]> Thu, 03 Mar 2016 18:59:33 +0000
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
Kevin Wright <[email protected]> writes: > Numerous better ideas… > > 1. You’re making checks against two different regexes that can be > combined into one. > 2. You’re attempting to match “.” in one of those expressions. This > will match any character unless you escape it to a literal with “\.” > 3. Anything used in a pattern match should begin with an uppercase > letter > > Taking points 1,2,3 into consideration: > > val UnwantedChar = """[0-9]|\.|;|,|"|:""".r > > 4. You don’t need to supply the empty parameter block when calling > `toLowerCase`. By convention, this syntax is only used for methods > with side effects, not for simple getters or conversions > 5. You’re explicitly converting a String to a StringOps - This is > already done for you via the magic of implicits > 6. When pattern matching against a regex, it destructures the string > import scala.io.StdIn > val inputLines = Iterator continually StdIn.readLine() takeWhile (_ != > null) > // note the () … because readLine() *is* side-effecting > for(line <- inputLines) { > … > } > I might be because I am still a Scala novel nonetheless I find this a bit hard to read, it does its job though. > 10. The `transformContent` method is now so short that it’s easier to > write the content inline > 11. Try to avoid side-effects in the middle of other blocks of code, > they’re better at the boundary of your app > > Putting it all together: > > import scala.io.StdIn > val UnwantedChar = """[0-9]|\.|;|,|"|:""".r > val inputLines = Iterator continually StdIn.readLine() takeWhile (_ != > null) > val filteredInputLines = inputLines map {line => > UnwantedChar.replaceAllIn(line.toLowerCase, "") > } > filteredInputLines foreach println > Thanks a lot, my previous solution was definitely a cumbersome solution. -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.