score:1
i wrote sumup2
to return either[int, (int, int)]
: left is the number of visited elements and the right is a pair of the number of visited elements and running sum.
type intor[a] = either[int, a]
val sumup2: ((int, int), int) => intor[(int, int)] = (pair, y) => {
val (size, x) = pair
val sum = x + y
println(s"sum = $sum, y = $y")
if (sum > 0) (size + 1, sum).asright else size.asleft
}
we know that foldm
stops when sumup2
returns left
so sumup2
won't be invoked for all elements:
scala> val r = foldable[stream].foldm(xs.tostream, (0, 0))(sumup2)
sum = 1, y = 1
sum = 3, y = 2
sum = 6, y = 3
sum = 4, y = -2
sum = -1, y = -5
r: intor[(int, int)] = left(4)
given r: either[int, (int, int)]
we can get the tail:
scala> r match { case right(_) => nil; case left(n) => xs.drop(n) }
res63: list[int] = list(-5, 1, 2, 3)
the solution seems to work fine but doesn't look nice to me. how would you improve it ?
score:3
what you can do is accumulate two values (in a tuple): the running sum up until it turns negative or zero; and the tail, which starts to accumulate values then.
val sumup2: ((int, list[int]), int) => id[(int, list[int])] = (x, y) => {
val sum = if (x._1 < 0) x._1 else x._1 + y
if (sum > 0) (sum, x._2) else (-1, x._2 ++ list(y))
}
then, you can get the tail from the second element in the tuple:
val xs = list(1, 2, 3, -2, -5, 1, 2, 3)
val res = foldable[stream].foldm(xs.tostream, (0, list[int]()))(sumup2)
println(res._2)
fiddle here.
Source: stackoverflow.com
Related Query
- How to use foldM of Foldable?
- What is a TypeTag and how do I use it?
- How to use java.String.format in Scala?
- How to use Shapeless in a Quasiquote?
- How can I use map and receive an index as well in Scala?
- How does type Dynamic work and how to use it?
- How to use Column.isin with list?
- How to use JDBC source to write and read data in (Py)Spark?
- how do I get sbt to use a local maven proxy repository (Nexus)?
- How to use third party libraries with Scala REPL?
- Scala type keyword: how best to use it across multiple classes
- How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?
- How to use switch/case (simple pattern matching) in Scala?
- How to use / refer to the negation of a boolean function in Scala?
- How to use Scala's this typing, abstract types, etc. to implement a Self type?
- How to define and use a User-Defined Aggregate Function in Spark SQL?
- How to use scala.None from Java code
- How to create and use a multi-dimensional array in Scala?
- How can I use a Scala singleton object in Java?
- How to force SBT to use Java 8?
- How to use synchronized in Scala?
- How to use MySQL JDBC driver in an SBT Scala project?
- Understand how to use apply and unapply
- How to use IntelliJ with Play Framework and Scala
- How to use SQL "LIKE" operator in SLICK
- In Scala, how to use Ordering[T] with List.min or List.max and keep code readable
- How to use orderby() with descending order in Spark window functions?
- How to use regex to include/exclude some input files in sc.textFile?
- How to use scala trait with `self` reference?
- How to use Environment Variables in build.sbt?
More Query from same tag
- Custom configuration src jar ending up in jars folder instead of srcs when using sbt publishLocal
- Slick and liftweb
- Trying to implement a Circular Enumeration in Scala
- Flink Gelly - Type mismatch while creating graph
- How do I develop on a virtual machine using Eclipse?
- Stripe Webhook Verification Error with Play Framework
- intellij idea scala "" autocompletion turn-off
- How can you add custom artifacts in a SBT command
- Unsupported literal type class in Apache Spark in scala
- What does a type have to be constrained to before it can be assigned "null"?
- Getting Started With Lift, Using Databases to Build Dynamic Sites
- using external ivy for sbt build
- Scala- creating a map from string
- understanding this scala code
- How can I install openjdk 8 and sbt on mac and have openjdk 8 (not 13) the default one?
- Checking the number of digits in Scala
- Jumping to source while editing scala with vim
- Error when use akka http unmarshall entity as case class with default value
- How to filter out entries from List[Map[String,String]]?
- How to insert dynamically developed string query in to with column function scala
- Merge List[List[_]] conditionally
- Class member assigned to None or null or _ in scala which is better and why?
- how can i extend a type to a trait in scala
- Get or create child actor by ID
- Play 2.3 scala - Slick with MYSQL issue
- How do I get sbt 0.10.0 to compile files in a subdirectory?
- Getting the index of a slice
- How to Pass argument to Oozie
- Tell is not a member of actor
- Best way to pass the value in Some to a function with a default parameter value?