Re: Partition A Stream Error
Stephen Compall <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
On 2015-08-08 8:58 PM, [email protected] wrote: > created the partitioning function: > |scala>defmatchOrders(o :Order,s > :Stream[Order])=s.contains(o)matchOrders:(o:Order,s:Stream[Order])Boolean| > > then tried to apply this to stream: > > |scala>vals > :(Stream[Order],Stream[Order])=orders.toStream.partition(matchOrders(_,s._1))| > > I got a null pointer exception since I guess the |s._1| is empty > initially?? I'm not sure. I've tried other ways but I'm not getting > very far. Is there a way to achieve this partitioning? > You cannot, logically, use circular programming <https://en.wikibooks.org/wiki/Haskell/Fix_and_recursion> like this, because contains may reach the end of s._1, and at the very least, even if you circularly-program this with the right primitive (lazy val not val <https://github.com/scalaz/scalaz/blob/v7.1.3/core/src/main/scala/scalaz/std/Function.scala#L160>), you don't know where the end of s._1 is, so contains cannot terminate. In other words, matchOrders is strict on the s argument's spine. (You may be tempted to try making that argument to matchOrders by-name; try to work out why that won't make a difference.) I have included links with working examples of this kind of programming; I suggest using the lazy val or fix function to port these to Scala. The simplest is a basic infinite stream of a given value. -- Stephen Compall If anyone in the MSA is online, you should watch this flythrough. -- 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.