-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem 10 [Language] #19
Comments
Jamie about (2) if k1 * k2 = j then either k1 * k1 <= j or k2 * k2 <= j Luc On Sat, Dec 13, 2014 at 10:30 PM, Jamie Phillips [email protected]
__~O reality goes far beyond imagination |
by the way: you can also "flatMap that shit" val ps: Stream[Int] = Luc On Sun, Dec 14, 2014 at 10:30 PM, Luc Duponcheel [email protected]
__~O reality goes far beyond imagination |
and, this one works as well (ans may be more intuitive) val ps: Stream[Int] = 2 #:: ps.flatMap(i => Stream.from(i + 1).filter(j => (flattening a stream of streams) On Sun, Dec 14, 2014 at 10:35 PM, Luc Duponcheel [email protected]
__~O reality goes far beyond imagination |
and if you do not like flatMap, then this one works as well val ps: Stream[Int] = 2 #:: Stream.from(3).filter(j => ps.takeWhile(k => k
On Sun, Dec 14, 2014 at 10:52 PM, Luc Duponcheel [email protected]
__~O reality goes far beyond imagination |
Hi Jamie, Most, if not all of your questions regarding views are answered here: http://docs.scala-lang.org/overviews/collections/views.html http://docs.scala-lang.org/overviews/collections/views.html Funny that palindromes come up during the explanation. More useful articles one level up: http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137 http://docs.scala-lang.org/overviews/?_ga=1.26260989.1311091313.1410020137 Cheers, Eric
|
Hi Jamie and Eric, note that
So, maybe, for Stream, using view does not matter to much? On Mon, Dec 15, 2014 at 9:13 AM, Eric Loots [email protected]
__~O reality goes far beyond imagination |
I would agree with @LucDupAtGitHub that a view on a Stream is pretty useless. In general, if you don't need memoization use a lazy view of some collection rather than a Stream. |
or, using the given Stream.from(2).filter(isPrime).takeWhile(_ < 2000000).map(_.toLong).sum The conversion to |
Hi Guys
So I was looking on the internet for solutions to this that would return a value in the next millennium - because my attempts were not performing well at all! ;-)
So I found this somewhere on the net
lazy val ps: Stream[Int] = 2 #:: ps.map(i => Stream.from(i + 1).find(j => ps.takeWhile(k => k * k <= j).forall(j % _ > 0)).get)
ps.view.takeWhile(_ < 2000000).foldLeft(0L)(_ + _)
This works like a charm and returns within 2 seconds with the correct result
Questions
I hope that all the gluhwein I had today has made my questions understandable
Jamie
The text was updated successfully, but these errors were encountered: