scala

Type-safety with ADT and Aux pattern

断了今生、忘了曾经 提交于 2021-02-07 09:51:28
问题 I'm designing type-safe code with ADT and Aux-pattern and cannot get rid of some asInstanceOf . Here is the example: sealed trait Source case object FileSystem extends Source case object Network extends Source sealed trait Data { type S <: Source } object Data { type Aux[T <: Source] = Data { type S = T } } case class RegularFile(path: String) extends Data { type S = FileSystem.type } case class Directory(path: String) extends Data { type S = FileSystem.type } case class UnixDevice(path:

Accessing nested fields in AVRO GenericRecord (Java/Scala)

社会主义新天地 提交于 2021-02-07 09:17:58
问题 I have a GenericRecord with nested fields. When I use genericRecord.get(1) it returns an Object that contains the nested AVRO data. I want to be able to access that object like genericRecord.get(1).get(0) , but I can't because AVRO returns an Object. Is there an easy way around this? When I do something like returnedObject.get("item") it says item not a member of returnedObject . 回答1: I figured out one way to do it. Cast the returned Object as a GenericRecord . Example (scala): val data

Reverse Cartesian Product

空扰寡人 提交于 2021-02-07 09:16:28
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Reverse Cartesian Product

我是研究僧i 提交于 2021-02-07 09:16:22
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Reverse Cartesian Product

百般思念 提交于 2021-02-07 09:12:46
问题 Given the data set below: a | b | c | d 1 | 3 | 7 | 11 1 | 5 | 7 | 11 1 | 3 | 8 | 11 1 | 5 | 8 | 11 1 | 6 | 8 | 11 Perform a reverse Cartesian product to get: a | b | c | d 1 | 3,5 | 7,8 | 11 1 | 6 | 8 | 11 I am currently working with scala, and my input/output data type is currently: ListBuffer[Array[Array[Int]]] I have come up with a solution (seen below), but feel it could be optimized. I am open to optimizations of my approach, and completely new approaches. Solutions in scala and c# are

Is State monad needed/useful in a language with mutable (local) variables (such as Scala)?

怎甘沉沦 提交于 2021-02-07 09:10:55
问题 I understand that in Haskell the State monad is useful because there are no mutable variables (unless we are in the IO monad). However, what is the deal with Scala ? Is State Monad useful in a language where there are mutable variables ? In some sense all the State Monad allows is to use some local mutable variables within the Monad context. For example here: newtype Labeled anytype = Labeled (S -> (S, anytype)) instance Monad Labeled where return contents = Labeled (\st -> (st, contents))

Update actor state only after all events are persisted

﹥>﹥吖頭↗ 提交于 2021-02-07 08:55:43
问题 In the receive method of a persistent actor, I receive a bunch a events I want to persist, and only after all events are persisted, update again my state. How can I do that? def receive: Receive = { ... case NewEvents(events) => persist(events) { singleEvent => // Update state using this single event } // After every events are persisted, do one more thing } Note that the persist() call is not blocking so I cannot put my code just after that. Update: Why I need this These new events come from

Update actor state only after all events are persisted

爷,独闯天下 提交于 2021-02-07 08:53:46
问题 In the receive method of a persistent actor, I receive a bunch a events I want to persist, and only after all events are persisted, update again my state. How can I do that? def receive: Receive = { ... case NewEvents(events) => persist(events) { singleEvent => // Update state using this single event } // After every events are persisted, do one more thing } Note that the persist() call is not blocking so I cannot put my code just after that. Update: Why I need this These new events come from

Update actor state only after all events are persisted

我们两清 提交于 2021-02-07 08:53:42
问题 In the receive method of a persistent actor, I receive a bunch a events I want to persist, and only after all events are persisted, update again my state. How can I do that? def receive: Receive = { ... case NewEvents(events) => persist(events) { singleEvent => // Update state using this single event } // After every events are persisted, do one more thing } Note that the persist() call is not blocking so I cannot put my code just after that. Update: Why I need this These new events come from

Difference between object with main() and extends App in scala

不羁岁月 提交于 2021-02-07 08:49:35
问题 I'm working through ScalaInAction (book is still a MEAP, but code is public on github) Right now I'm in chapter 2 looking at this restClient: : https://github.com/nraychaudhuri/scalainaction/blob/master/chap02/RestClient.scala First, I setup intelliJ with scala extensions and created a HelloWorld with main() : <ALL the imports> object HelloWorld { def main(args: Array[String]) { <ALL the rest code from RestClient.scala> } } I get the following error when compiling: scala: forward reference