Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von AizazZaidee » 12. Jul 2018 11:23

How can I convert rescala.reactives.Signal[List[Int],rescala.parrp.ParRP] to List[Int]

Code: Alles auswählen

import rescala.Evt

val event = Evt[Int]()
event(10)
event(10)
event(10)
val f = (x: Int, y: Int) => (x + y)
val signal = event.list().map{
  matcher => 
    matcher match {
      case _ => print(f) //not working
    }
}

llllllll
Erstie
Erstie
Beiträge: 19
Registriert: 29. Okt 2017 21:47

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von llllllll » 12. Jul 2018 11:37

Hello,

I' m not exactly sure, what you are trying to do, but if you want to combine the values of the event to a list I'd recommend something like this:

Code: Alles auswählen

val e = Evt[List[Int]]()
val f = (x: List[Int], y: List[Int]) => (x:::y)
val s: Signal[List[Int]] = e.fold(List(10))(f)
If you want to apply it on

Code: Alles auswählen

val e = Evt[Int]()
, I'm pretty sure you can just define a new function which converts from Evt[Int] to Evt[List[Int]], but I haven't tried it myself.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von AizazZaidee » 12. Jul 2018 11:41

Yeah I guess I am doing something wrong, I am trying to sum all the values of the Signal/Event. I am not sure how to do it.

llllllll
Erstie
Erstie
Beiträge: 19
Registriert: 29. Okt 2017 21:47

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von llllllll » 12. Jul 2018 11:58

I'm not anywhere as good with scala to either understand your approach or tell you how to fix it. My code is just my naive approach to your task/problem.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von AizazZaidee » 12. Jul 2018 12:13

Yeah I know the only problem here is Scala. Well suppose I have following

Code: Alles auswählen

val event1 = Evt[Int]()
val signal1 = event1.last(10)
How can I can get of all the values in signal1?

Code: Alles auswählen

val event1 = Evt[Int]()
    val signal1 = event1.last(10)
    val sum = signal1.change.map(diff =>
    diff.pair match {
      case // Signal[LinearSeq[T1], S] How to sum linear sequence values?

    }

nknauber
Erstie
Erstie
Beiträge: 11
Registriert: 25. Apr 2018 16:48

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von nknauber » 12. Jul 2018 13:15

I was also very confused about this but we just tried it our in the IDE and tested it. The following code works for exam WS 15/16 task 5 c) :

Code: Alles auswählen

  val e1: Evt[Int] = Evt[Int]()
  val e2: Evt[Unit] = Evt[Unit]()
  val s1: Signal[Seq[Int]] = e1.last(10)
  val s2: Signal[Int] = Signal{s1().sum} // first idea: fold(0)(x:Int, y:Int) => (x+y)) // easier: s1().fold(0)(_ + _)
  val s3: Signal[Seq[Int]] = e2.snapshot(s1)
  val s4: Signal[Int] = e2.count()
Our first intuition was to look at the RP2 slides where they state on slide 31, that the following should work:

Code: Alles auswählen

val e = Evt[Int]()
val s: Signal[List[Int]] = e.last(5)
However, compiler says no, as it does not return a Signal[List[Int]] but instead a Signal[Seq[Int]] and this makes it very easy to just call .sum on the Seq. I was also very bummed out to find out that the examples on the slides actually don't compile.
@pmueller: If you're reading this, can you please forward this example to the professor?

typ
Windoof-User
Windoof-User
Beiträge: 32
Registriert: 5. Nov 2016 19:06

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von typ » 12. Jul 2018 17:43

check this out from exercise 3:

Code: Alles auswählen

  private def lastNDays(days: Int): Signal[Seq[StockPrice]] = updates.last(days)

  def averageNDays(days: Int): Signal[Double] = Signal[Double] {
    val lastN = lastNDays(days)()
    if (lastN.isEmpty) 0.0
    else {
      var sum = 0.0
      lastN.foreach((price: StockPrice) => {
        sum += price.value
      })
      sum / lastN.size
    }
  }
this is how i would sum up all values. this is the original solution:

Code: Alles auswählen

  
  def averageNDays(days: Int): Signal[Double] = Signal {
    val lastNDays: Signal[LinearSeq[StockPrice]] = updates.last(days)
    lastNDays().map(sp => sp.value).sum / lastNDays().size
  }
If it returns Signal[LinearSeq[T1], S], you can just use Signal[Seq[T1], S], since Seq>LinearSeq.
If the IDE somewhere writes Signal[Seq[Int],rescala.parrp.ParRP], you can just write Signal[Seq[Int]]
If you have val lala : Signal[Seq[Int]] and you are inside another signal, you can just write lala() to get the Seq[Int]. Outside of a signal, you have to use lala.now. Check if the returned value is Some and not None.
Always import full "rescala._", which prevents weird compiler errors.

The function map is to convert from (10, 10, 10) to (f(10), f(10), f(10)). The output list has the same length as the input map, so you can't get the sum of a list with map alone.
The function fold will do something like f(10, f(10, f(10, init)))

Hope this helps and everything is correct
Zuletzt geändert von typ am 12. Jul 2018 17:56, insgesamt 1-mal geändert.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Signal[List[Int],rescala.parrp.ParRP] to List[Int] How?

Beitrag von AizazZaidee » 12. Jul 2018 17:56

I think s1.sum() is enough, I have tried it with latest API, I think documentation and slides are not updated.

Antworten

Zurück zu „Software Engineering - Design and Construction“