All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dregex.impl.Util.scala Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package dregex.impl

import com.typesafe.scalalogging.slf4j.StrictLogging
import java.time.Duration

object Util extends StrictLogging {

  def mergeWithUnion[B, C](left: Map[B, Set[C]], right: Map[B, Set[C]]) = merge(left, right)(_ union _)

  def merge[A, B](left: Map[A, B], right: Map[A, B])(fn: (B, B) => B): Map[A, B] = {
    val merged = for ((k, lv) <- left) yield {
      val mergedValue = right.get(k) match {
        case Some(rv) => fn(lv, rv)
        case None => lv
      }
      k -> mergedValue
    }
    merged ++ right.filterKeys(!left.contains(_))
  }

  def doIntersect[A](left: Set[A], right: Set[A]) = left exists right

  def time[A](thunk: => A): (A, Duration) = {
    val start = System.nanoTime()
    val res = thunk
    val time = Duration.ofNanos(System.nanoTime() - start)
    (res, time)
  }

  
  implicit class StrictMap[K, A](map: Map[K, A]) {
    def mapValuesNow[B](f: A => B): Map[K, B] = map.map { case (a, b) => (a, f(b)) }
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy