Re: Scala beginner question

Pietro <[email protected]> Sat, 05 Mar 2016 17:34:06 +0000
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
Kevin Wright <[email protected]> writes:

>     import scala.io.StdIn 
>
>     val inputLines = Iterator continually StdIn.readLine() takeWhile
>     (_ !=> null)
>     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.
>
> Easy enough to explain!
>
> import scala.io.StdIn
>
>     We use scala.io.StdIn for the StdIn.readLine() method, because
>     readLine() in predef is deprecated
>
> Iterator continually StdIn.readLine() takeWhile (_ !=> null)
>
>     This is infix notation, it’s directly equivalent to:
>
> Iterator.continually(StdIn.readLine()).takeWhile(_ !=> null)
>
>     Where:
>
> Iterator.continually(expr)
>
>     returns an iterator (a potentially infinite, lazily evaluated
>     “collection”), where each element is generated on-demand by
>     evaluating expr.
>
> Iterator continually StdIn.readLine()
>
>     Is one such iterator, with each element coming from a successive
>     calls to readLine()
>
> someIterator takeWhile (_ !=> null)
>
>     Transforms someIterator, returning another iterator that
>     terminates as soon as a null element is encountered in the
>     original
>     Therefore…
>
> Iterator continually StdIn.readLine() takeWhile (_ !=> null)
>
> Is an iterator that returns successive calls to readLine(), (each time
> you call next(), or when you use foreach, or map, or flatMap, or use
> it in a for-comprehension - which is just syntactic sugar for foreach,
> map or flatMap) until there’s a null element
>
> On 3 March 2016 at 18:59, Pietro <[email protected]> wrote:
>
>     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.
>     
>
> -- 
> Kevin Wright
> mail / hangouts / msn : [email protected]
> quora: http://www.quora.com/Kevin-Wright
> google+: http://gplus.to/thecoda
> twitter: @thecoda
> vibe / skype: kev.lee.wright
>
> "My point today is that, if we wish to count lines of code, we should
> not regard them as "lines produced" but as "lines spent": the current
> conventional wisdom is so foolish as to book that count on the wrong
> side of the ledger" ~ Dijkstra
Thanks a lot Kevin for your exhaustive explanation.

I have got an additional question for you if you do not mind me asking again, I am
trying - and again banging my head on the wall - to use Scalatest.

It is not a big deal to write some tests but I am finding challenging to
execute them, googling the problem it seems to be a classpath problem
but I haven't found a fix yet and the proposed solution I have found
which says to add the classpath entries in a single string delimited by
a colon on a per-entry basis does not work.

This is what I get when I try to run my test, do you have any idea ?

xxxx@xxxxcrunchbang:~/pplp/scala$ scala -cp scalatest_2.11-2.2.6.jar:/usr/share/java/scala-library.jar  org.scalatest.run scala-test.scala
java.lang.NoClassDefFoundError: scala/concurrent/duration/Duration
	at org.scalatest.tools.Runner$.<init>(Runner.scala:779)
	at org.scalatest.tools.Runner$.<clinit>(Runner.scala)
	at org.scalatest.run$.main(run.scala:120)
	at org.scalatest.run.main(run.scala)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
	at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
	at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
	at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
	at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: scala.concurrent.duration.Duration
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:88)
	at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:44)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.findClass(ScalaClassLoader.scala:88)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$loadClass(ScalaClassLoader.scala:88)
	at scala.tools.nsc.util.ScalaClassLoader$class.loadClass(ScalaClassLoader.scala:50)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.loadClass(ScalaClassLoader.scala:88)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	... 19 more

-- 
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.