<commit_before>package ml.wolfe.term


trait Dynamic[T] {
  parent =>
  protected var children: List[Dynamic[_]] = Nil
  protected var owners: List[Dynamic[_]] = Nil

  def ownerCount = owners.size

  def childCount = children.size

  def descendentCount:Int = 1 + children.map(_.descendentCount).sum

  def printTree(indent:Int = 0):Unit = {
    for (_ <- 0 until indent) print(" ")
    try { print(value()) } catch { case _:Exception => println("Exception")}
    println(owners.map(_.value()).mkString(" (",",",")"))
    for (c <- children) c.printTree(indent + 2)
  }

  def size = 1

  protected def detach(): Unit = {
    for (owner <- owners) {
      owner.children = owner.children.filterNot(_ == parent)
    }
    owners = Nil
  }

  private def attach(child: Dynamic[_]): Unit = {
    children ::= child
    child.owners ::= parent
  }

  protected def update()

  def updateValue(): Unit = {
    update()
    children.reverse foreach (_.updateValue())
  }

  def value(): T

  def map[A](f: T => A):Dynamic[A] = new Dynamic[A] {
    parent.attach(this)

    private var current: A = _
    private var needsUpdate = true

    protected def update() = {
      if (owners.nonEmpty) needsUpdate = true
    }

    def value() = {
      if (needsUpdate) {
        current = f(parent.value())
        needsUpdate = false
      }
      current
    }
  }

  def flatMap[A](f: T => Dynamic[A]):Dynamic[A] = new Dynamic[A] {
    parent.attach(this)

    private var current: Dynamic[A] = _
    private var needsUpdate = true

    protected def update() = {
      if (current != null) current.detach()
      needsUpdate = true
    }


    override protected def detach() = {
      super.detach()
      if (current != null) current.detach()
    }

    def value() = {
      if (needsUpdate) {
        current = f(parent.value())
        current.updateValue()
        needsUpdate = false
      }
      current.value()
    }

  }

}

object Dynamic {
  def sequential[T](seq: IndexedSeq[T]): Dynamic[T] = new Dynamic[T] {
    private var _current = -1
<commit_msg>simplified type-safe models<commit_after>package ml.wolfe.term


trait Dynamic[T] {
  parent =>
  protected var children: List[Dynamic[_]] = Nil
  protected var owners: List[Dynamic[_]] = Nil

  def ownerCount = owners.size

  def childCount = children.size

  def descendentCount:Int = 1 + children.map(_.descendentCount).sum

  def printTree(indent:Int = 0):Unit = {
    for (_ <- 0 until indent) print(" ")
    try { print(value()) } catch { case _:Exception => println("Exception")}
    println(owners.map(_.value()).mkString(" (",",",")"))
    for (c <- children) c.printTree(indent + 2)
  }

  def size = 1

  protected def detach(): Unit = {
    for (owner <- owners) {
      owner.children = owner.children.filterNot(_ == parent)
    }
    owners = Nil
  }

  private def attach(child: Dynamic[_]): Unit = {
    children ::= child
    child.owners ::= parent
  }

  protected def update()

  def updateValue(): Unit = {
    update()
    children.reverse foreach (_.updateValue())
  }

  def value(): T

  def map[A](f: T => A):Dynamic[A] = new Dynamic[A] {
    parent.attach(this)

    private var current: A = _
    private var needsUpdate = true

    protected def update() = {
      if (owners.nonEmpty) needsUpdate = true
    }

    def value() = {
      if (needsUpdate) {
        current = f(parent.value())
        needsUpdate = false
      }
      current
    }
  }

  def flatMap[A](f: T => Dynamic[A]):Dynamic[A] = new Dynamic[A] {
    parent.attach(this)

    private var current: Dynamic[A] = _
    private var needsUpdate = true

    protected def update() = {
      if (current != null) current.detach()
      needsUpdate = true
    }


    override protected def detach() = {
      super.detach()
      if (current != null) current.detach()
    }

    def value() = {
      if (needsUpdate) {
        current = f(parent.value())
        current.updateValue()
        needsUpdate = false
      }
      current.value()
    }

  }

}

object Dynamic {

  def singleton[T](value:T) = sequential(IndexedSeq(value))

  def sequential[T](seq: IndexedSeq[T]): Dynamic[T] = new Dynamic[T] {
    private var _current = -1

{'fraction_non_alphanumeric': 0.1413924654268002, 'fraction_numerical': 0.002861230329041488, 'mean_word_length': 2.591609589041096, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2009409', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1293, 'n_words': 341}