Re: Re: Scala beginner question

Kevin Wright <[email protected]> Thu, 3 Mar 2016 19:37:46 +0000
Newsgroups gmane.comp.lang.scala
Message-ID <CABHxxC0M9vA2eFd=8OiP-LA5+u4uYkLZDEckfJNxtLx4hCa=Hw@mail.gmail.com>
takeWhile (_ !=> null)


should of course be:

takeWhile (_ != null)


Ignore the >



On 3 March 2016 at 19:35, Kevin Wright <[email protected]> wrote:

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