<commit_before>package rescala.incremental

import rescala.core._
import rescala.macros.cutOutOfUserComputation
import rescala.reactives.Signal



trait Delta[T]
case class Addition[T](value: T) extends Delta[T]
case class Removal[T](value: T) extends Delta[T]



trait ReactiveDeltaSet[T, S <: Struct] extends ReSource[S] {

  /** the value of deltas send through the set */
  override type Value = Delta[T]


  @cutOutOfUserComputation
  def map[A](expression: T => A)(implicit ticket: CreationTicket[S]): ReactiveDeltaSet[A, S] = ???

  @cutOutOfUserComputation
  def filter[A](expression: T => Boolean)(implicit ticket: CreationTicket[S]): ReactiveDeltaSet[A, S] = ???

  @cutOutOfUserComputation
  def aggregate[A](expression: (A, Delta[T]) => A)(implicit ticket: CreationTicket[S]): Signal[A, S] = ???

}



/** Source events with imperative occurrences
  *
  * @param initialState of by the event
  * @tparam T Type returned when the event fires
  * @tparam S Struct type used for the propagation of the event
  */
final class SetSource[T, S <: Struct] private[rescala](initialState:  S#State[Delta[T], S], name: REName)
  extends Base[Delta[T], S](initialState, name) with ReactiveDeltaSet[T, S] {

  def add(value: T)(implicit fac: Scheduler[S]): Unit =
    fac.forceNewTransaction(this) {
      addInTx(Addition(value))(_)
    }

  def addInTx(delta: Delta[T])(implicit ticket: AdmissionTicket[S]): Unit = {
    ticket.recordChange(new InitialChange[S] {
      override val source = SetSource.this
      override def writeValue(b: Delta[T], v: Delta[T] => Unit): Boolean = {v(delta); true}
    })
  }
}

<commit_msg>implement aggregate<commit_after>package rescala.incremental

import rescala.core._
import rescala.macros.cutOutOfUserComputation
import rescala.reactives.{Event, Events, Signal}



trait Delta[T]
case class Addition[T](value: T) extends Delta[T]
case class Removal[T](value: T) extends Delta[T]



trait ReactiveDeltaSet[T, S <: Struct] extends ReSource[S] {

  /** the value of deltas send through the set */
  override type Value = Delta[T]


  @cutOutOfUserComputation
  def map[A](expression: T => A)(implicit ticket: CreationTicket[S]): ReactiveDeltaSet[A, S] = ???

  @cutOutOfUserComputation
  def filter[A](expression: T => Boolean)(implicit ticket: CreationTicket[S]): ReactiveDeltaSet[A, S] = ???


  @cutOutOfUserComputation
  def asEvent(implicit ticket: CreationTicket[S]): Event[Delta[T], S] = {
    Events.static( this) { staticTicket =>
      val delta = staticTicket.collectStatic(this)
      Some(delta)
    }
  }

  @cutOutOfUserComputation
  def aggregate[A: ReSerializable](initial: A)(expression: (A, Delta[T]) => A)(implicit ticket: CreationTicket[S]): Signal[A, S] = {
    val event = asEvent
    Events.foldOne(event, initial){ expression }
  }

}



/** Source events with imperative occurrences
  *
  * @param initialState of by the event
  * @tparam T Type returned when the event fires
  * @tparam S Struct type used for the propagation of the event
  */
final class SetSource[T, S <: Struct] private[rescala](initialState:  S#State[Delta[T], S], name: REName)
  extends Base[Delta[T], S](initialState, name) with ReactiveDeltaSet[T, S] {

  def add(value: T)(implicit fac: Scheduler[S]): Unit =
    fac.forceNewTransaction(this) {
      addInTx(Addition(value))(_)
    }

  def addInTx(delta: Delta[T])(implicit ticket: AdmissionTicket[S]): Unit = {
    ticket.recordChange(new InitialChange[S] {
      override val source = SetSource.this
      override def writeValue(b: Delta[T], v: Delta[T] => Unit): Boolean = {v(delta); true}
    })
  }
}


{'fraction_non_alphanumeric': 0.1315570358034971, 'fraction_numerical': 0.0, 'mean_word_length': 4.622464898595944, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1467720', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1173, 'n_words': 315}