scala.annotation.tailrec Scala Examples
The following examples show how to use scala.annotation.tailrec.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Factorial.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package edu.neu.coe.csye._7200.factorial import scala.annotation.tailrec class Factorial(val n: Int) extends (()=>Long) { def apply: Long = Factorial.factorial(n) } object Factorial extends App { println(new Factorial(5)()) def factorial(n: Int) = { @tailrec def inner(r: Long, n: Int): Long = if (n <= 1) r else inner(n * r, n - 1) inner(1L, n) } }
Example 2
Source File: OneOfMacro.scala From tapir with Apache License 2.0 | 5 votes |
package sttp.tapir.generic.internal import sttp.tapir.Schema import scala.annotation.tailrec import scala.reflect.macros.blackbox object OneOfMacro { // http://onoffswitch.net/extracting-scala-method-names-objects-macros/ def oneOfMacro[E: c.WeakTypeTag, V: c.WeakTypeTag]( c: blackbox.Context )(extractor: c.Expr[E => V], asString: c.Expr[V => String])(mapping: c.Expr[(V, Schema[_])]*): c.Expr[Schema[E]] = { import c.universe._ @tailrec def resolveFunctionName(f: Function): String = { f.body match { // the function name case t: Select => t.name.decodedName.toString case t: Function => resolveFunctionName(t) // an application of a function and extracting the name case t: Apply if t.fun.isInstanceOf[Select @unchecked] => t.fun.asInstanceOf[Select].name.decodedName.toString // curried lambda case t: Block if t.expr.isInstanceOf[Function @unchecked] => val func = t.expr.asInstanceOf[Function] resolveFunctionName(func) case _ => throw new RuntimeException("Unable to resolve function name for expression: " + f.body) } } val weakTypeE = weakTypeOf[E] def extractTypeArguments(weakType: c.Type): List[String] = { def allTypeArguments(tn: c.Type): Seq[c.Type] = tn.typeArgs.flatMap(tn2 => tn2 +: allTypeArguments(tn2)) allTypeArguments(weakType).map(_.typeSymbol.name.decodedName.toString).toList } val name = resolveFunctionName(extractor.tree.asInstanceOf[Function]) val schemaForE = q"""{ import sttp.tapir.Schema._ import sttp.tapir.SchemaType._ val rawMapping = scala.collection.immutable.Map(..$mapping) val discriminator = Discriminator($name, rawMapping.map{case (k, sf)=> $asString.apply(k) -> SRef(sf.schemaType.asInstanceOf[SObject].info)}) Schema(SCoproduct(SObjectInfo(${weakTypeE.typeSymbol.fullName},${extractTypeArguments(weakTypeE)}), rawMapping.values.toList, Some(discriminator))) }""" Debug.logGeneratedCode(c)(weakTypeE.typeSymbol.fullName, schemaForE) c.Expr[Schema[E]](schemaForE) } }
Example 3
Source File: ModifySchemaMacro.scala From tapir with Apache License 2.0 | 5 votes |
package sttp.tapir.internal import sttp.tapir.Schema import scala.annotation.tailrec import scala.reflect.macros.blackbox object ModifySchemaMacro { private val ShapeInfo = "Path must have shape: _.field1.field2.each.field3.(...)" def modifyMacro[T: c.WeakTypeTag, U: c.WeakTypeTag]( c: blackbox.Context )(path: c.Expr[T => U])(modification: c.Expr[Schema[U] => Schema[U]]): c.Tree = applyModification[T, U](c)(extractPathFromFunctionCall(c)(path), modification) def setDescriptionMacro[T: c.WeakTypeTag, U: c.WeakTypeTag]( c: blackbox.Context )(path: c.Expr[T => U], description: c.Expr[String]): c.Tree = addDescription[T, U](c)(extractPathFromFunctionCall(c)(path), description) private def addDescription[T: c.WeakTypeTag, U: c.WeakTypeTag](c: blackbox.Context)( path: c.Expr[List[String]], description: c.Expr[String] ): c.Tree = { import c.universe._ q"""{ ${c.prefix}.modifyUnsafe($path:_*)((v: sttp.tapir.Schema[${c.weakTypeOf[T]}]) => v.description($description)) }""" } private def applyModification[T: c.WeakTypeTag, U: c.WeakTypeTag](c: blackbox.Context)( path: c.Expr[List[String]], modification: c.Expr[Schema[U] => Schema[U]] ): c.Tree = { import c.universe._ q"""{ ${c.prefix}.modifyUnsafe($path:_*)($modification) }""" } @tailrec def collectPathElements(tree: c.Tree, acc: List[PathElement]): List[PathElement] = { def typeSupported(quicklensType: c.Tree) = Seq("ModifyEach", "ModifyEither", "ModifyEachMap") .exists(quicklensType.toString.endsWith) tree match { case q"$parent.$child " => collectPathElements(parent, TermPathElement(child) :: acc) case q"$tpname[..$_]($t)($f) " if typeSupported(tpname) => val newAcc = acc match { // replace the term controlled by quicklens case TermPathElement(term, xargs @ _*) :: rest => FunctorPathElement(f, term, xargs: _*) :: rest case pathEl :: _ => c.abort(c.enclosingPosition, s"Invalid use of path element $pathEl. $ShapeInfo, got: ${path.tree}") case Nil => c.abort(c.enclosingPosition, s"Invalid use of path element(Nil). $ShapeInfo, got: ${path.tree}") } collectPathElements(t, newAcc) case _: Ident => acc case _ => c.abort(c.enclosingPosition, s"Unsupported path element. $ShapeInfo, got: $tree") } } val pathEls = path.tree match { case q"($arg) => $pathBody " => collectPathElements(pathBody, Nil) case _ => c.abort(c.enclosingPosition, s"$ShapeInfo, got: ${path.tree}") } def isOptionalFunctor(tree: c.Tree): Boolean = { tree match { case TypeApply(Select(_, TermName("optionModifyFunctor")), _) => true case _ => false } } c.Expr[List[String]](q"${pathEls.collect { case TermPathElement(c) => c.decodedName.toString case FunctorPathElement(functor, method, _ @_*) if !isOptionalFunctor(functor) => method.decodedName.toString }}") } private[internal] def ignoredFromPath[T, U](path: T => U): List[String] = macro extractPathFromFunctionCall[T, U] }
Example 4
Source File: update_comments.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import scala.annotation.tailrec object Update_Comments { def update_comments(path: Path) { def make_comment(tok: Token): String = Symbol.comment + Symbol.space + Symbol.cartouche(Symbol.trim_blanks(tok.content)) @tailrec def update(toks: List[Token], result: List[String]): String = { toks match { case tok :: rest if tok.source == "--" || tok.source == Symbol.comment => rest.dropWhile(_.is_space) match { case tok1 :: rest1 if tok1.is_text => update(rest1, make_comment(tok1) :: result) case _ => update(rest, tok.source :: result) } case tok :: rest if tok.is_formal_comment && tok.source.startsWith(Symbol.comment) => update(rest, make_comment(tok) :: result) case tok :: rest => update(rest, tok.source :: result) case Nil => result.reverse.mkString } } val text0 = File.read(path) val text1 = update(Token.explode(Keyword.Keywords.empty, text0), Nil) if (text0 != text1) { Output.writeln("changing " + path) File.write_backup2(path, text1) } } val isabelle_tool = Isabelle_Tool("update_comments", "update formal comments in outer syntax", args => { val getopts = Getopts(""" Usage: isabelle update_comments [FILES|DIRS...] Recursively find .thy files and update formal comments in outer syntax. Old versions of files are preserved by appending "~~". """) val specs = getopts(args) if (specs.isEmpty) getopts.usage() for { spec <- specs file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy")) } update_comments(Path.explode(File.standard_path(file))) }) }
Example 5
Source File: consumer_thread.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import scala.annotation.tailrec object Consumer_Thread { def fork[A](name: String = "", daemon: Boolean = false)( consume: A => Boolean, finish: () => Unit = () => ()): Consumer_Thread[A] = new Consumer_Thread[A](name, daemon, consume, finish) private def request(x: A, ack: Option[Consumer_Thread.Ack]) { synchronized { if (is_active) mailbox.send(Some((x, ack))) else error("Consumer thread not active: " + quote(thread.getName)) } ack.foreach(a => Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) }))) } def send(arg: A) { request(arg, None) } def send_wait(arg: A) { request(arg, Some(Synchronized(None))) } def shutdown(): Unit = { synchronized { if (is_active) { active = false; mailbox.send(None) } } thread.join } }
Example 6
Source File: cygwin.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{File => JFile} import java.nio.file.Files import scala.annotation.tailrec object Cygwin { def init(isabelle_root: String, cygwin_root: String) { require(Platform.is_windows) def exec(cmdline: String*) { val cwd = new JFile(isabelle_root) val env = sys.env + ("CYGWIN" -> "nodosfilewarning") val proc = Isabelle_System.process(cmdline.toList, cwd = cwd, env = env, redirect = true) val (output, rc) = Isabelle_System.process_output(proc) if (rc != 0) error(output) } val uninitialized_file = new JFile(cygwin_root, "isabelle\\uninitialized") val uninitialized = uninitialized_file.isFile && uninitialized_file.delete if (uninitialized) { val symlinks = { val path = (new JFile(cygwin_root + "\\isabelle\\symlinks")).toPath Files.readAllLines(path, UTF8.charset).toArray.toList.asInstanceOf[List[String]] } @tailrec def recover_symlinks(list: List[String]): Unit = { list match { case Nil | List("") => case link :: content :: rest => val path = (new JFile(isabelle_root, link)).toPath val writer = Files.newBufferedWriter(path, UTF8.charset) try { writer.write("!<symlink>" + content + "\u0000") } finally { writer.close } Files.setAttribute(path, "dos:system", true) recover_symlinks(rest) case _ => error("Unbalanced symlinks list") } } recover_symlinks(symlinks) exec(cygwin_root + "\\bin\\dash.exe", "/isabelle/rebaseall") exec(cygwin_root + "\\bin\\bash.exe", "/isabelle/postinstall") } } }
Example 7
Source File: update_comments.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import scala.annotation.tailrec object Update_Comments { def update_comments(path: Path) { def make_comment(tok: Token): String = Symbol.comment + Symbol.space + Symbol.cartouche(Symbol.trim_blanks(tok.content)) @tailrec def update(toks: List[Token], result: List[String]): String = { toks match { case tok :: rest if tok.source == "--" || tok.source == Symbol.comment => rest.dropWhile(_.is_space) match { case tok1 :: rest1 if tok1.is_text => update(rest1, make_comment(tok1) :: result) case _ => update(rest, tok.source :: result) } case tok :: rest if tok.is_formal_comment && tok.source.startsWith(Symbol.comment) => update(rest, make_comment(tok) :: result) case tok :: rest => update(rest, tok.source :: result) case Nil => result.reverse.mkString } } val text0 = File.read(path) val text1 = update(Token.explode(Keyword.Keywords.empty, text0), Nil) if (text0 != text1) { Output.writeln("changing " + path) File.write_backup2(path, text1) } } val isabelle_tool = Isabelle_Tool("update_comments", "update formal comments in outer syntax", args => { val getopts = Getopts(""" Usage: isabelle update_comments [FILES|DIRS...] Recursively find .thy files and update formal comments in outer syntax. Old versions of files are preserved by appending "~~". """) val specs = getopts(args) if (specs.isEmpty) getopts.usage() for { spec <- specs file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy")) } update_comments(File.path(file)) }) }
Example 8
Source File: consumer_thread.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import scala.annotation.tailrec object Consumer_Thread { def fork[A](name: String = "", daemon: Boolean = false)( consume: A => Boolean, finish: () => Unit = () => ()): Consumer_Thread[A] = new Consumer_Thread[A](name, daemon, consume, finish) private def request(x: A, ack: Option[Consumer_Thread.Ack]) { synchronized { if (is_active) mailbox.send(Some((x, ack))) else error("Consumer thread not active: " + quote(thread.getName)) } ack.foreach(a => Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) }))) } def send(arg: A) { request(arg, None) } def send_wait(arg: A) { request(arg, Some(Synchronized(None))) } def shutdown(): Unit = { synchronized { if (is_active) { active = false; mailbox.send(None) } } thread.join } }
Example 9
Source File: cygwin.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{File => JFile} import java.nio.file.Files import scala.annotation.tailrec object Cygwin { def init(isabelle_root: String, cygwin_root: String) { require(Platform.is_windows) def exec(cmdline: String*) { val cwd = new JFile(isabelle_root) val env = sys.env + ("CYGWIN" -> "nodosfilewarning") val proc = Isabelle_System.process(cmdline.toList, cwd = cwd, env = env, redirect = true) val (output, rc) = Isabelle_System.process_output(proc) if (rc != 0) error(output) } val uninitialized_file = new JFile(cygwin_root, "isabelle\\uninitialized") val uninitialized = uninitialized_file.isFile && uninitialized_file.delete if (uninitialized) { val symlinks = { val path = (new JFile(cygwin_root + "\\isabelle\\symlinks")).toPath Files.readAllLines(path, UTF8.charset).toArray.toList.asInstanceOf[List[String]] } @tailrec def recover_symlinks(list: List[String]): Unit = { list match { case Nil | List("") => case target :: content :: rest => link(content, new JFile(isabelle_root, target)) recover_symlinks(rest) case _ => error("Unbalanced symlinks list") } } recover_symlinks(symlinks) exec(cygwin_root + "\\bin\\dash.exe", "/isabelle/rebaseall") exec(cygwin_root + "\\bin\\bash.exe", "/isabelle/postinstall") } } def link(content: String, target: JFile) { val target_path = target.toPath using(Files.newBufferedWriter(target_path, UTF8.charset))( _.write("!<symlink>" + content + "\u0000")) Files.setAttribute(target_path, "dos:system", true) } }
Example 10
Source File: consumer_thread.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import scala.annotation.tailrec object Consumer_Thread { def fork[A](name: String = "", daemon: Boolean = false)( consume: A => Boolean, finish: () => Unit = () => ()): Consumer_Thread[A] = new Consumer_Thread[A](name, daemon, consume, finish) private def request(x: A, ack: Option[Consumer_Thread.Ack]) { synchronized { if (is_active) mailbox.send(Some((x, ack))) else error("Consumer thread not active: " + quote(thread.getName)) } ack.foreach(a => Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) }))) } def send(arg: A) { request(arg, None) } def send_wait(arg: A) { request(arg, Some(Synchronized(None))) } def shutdown(): Unit = { synchronized { if (is_active) { active = false; mailbox.send(None) } } thread.join } }
Example 11
Source File: cygwin.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{File => JFile} import java.nio.file.Files import scala.annotation.tailrec object Cygwin { def init(isabelle_root: String, cygwin_root: String) { require(Platform.is_windows) def exec(cmdline: String*) { val cwd = new JFile(isabelle_root) val env = sys.env + ("CYGWIN" -> "nodosfilewarning") val proc = Isabelle_System.process(cmdline.toList, cwd = cwd, env = env, redirect = true) val (output, rc) = Isabelle_System.process_output(proc) if (rc != 0) error(output) } val uninitialized_file = new JFile(cygwin_root, "isabelle\\uninitialized") val uninitialized = uninitialized_file.isFile && uninitialized_file.delete if (uninitialized) { val symlinks = { val path = (new JFile(cygwin_root + "\\isabelle\\symlinks")).toPath Files.readAllLines(path, UTF8.charset).toArray.toList.asInstanceOf[List[String]] } @tailrec def recover_symlinks(list: List[String]): Unit = { list match { case Nil | List("") => case link :: content :: rest => val path = (new JFile(isabelle_root, link)).toPath val writer = Files.newBufferedWriter(path, UTF8.charset) try { writer.write("!<symlink>" + content + "\u0000") } finally { writer.close } Files.setAttribute(path, "dos:system", true) recover_symlinks(rest) case _ => error("Unbalanced symlinks list") } } recover_symlinks(symlinks) exec(cygwin_root + "\\bin\\dash.exe", "/isabelle/rebaseall") exec(cygwin_root + "\\bin\\bash.exe", "/isabelle/postinstall") } } }
Example 12
Source File: XmlParsing.scala From aws4s with MIT License | 5 votes |
package org.aws4s.core import cats.implicits._ import scala.annotation.tailrec private[aws4s] object XmlParsing { @tailrec def text(nodes: xml.NodeSeq)(path: String*): String = path.headOption match { case None => nodes.text case Some(pathHead) => text(nodes \ pathHead)(path.tail: _*) } def integer(nodes: xml.NodeSeq)(path: String*): Option[Int] = nonEmptyText(nodes)(path: _*).flatMap(t => Either.catchNonFatal(t.toInt).toOption) }
Example 13
Source File: SecuritySchemesForEndpoints.scala From tapir with Apache License 2.0 | 5 votes |
package sttp.tapir.docs.openapi import sttp.tapir.internal._ import sttp.tapir.openapi.{OAuthFlow, OAuthFlows, SecurityScheme} import sttp.tapir.{Endpoint, EndpointIO, EndpointInput} import scala.annotation.tailrec private[openapi] object SecuritySchemesForEndpoints { def apply(es: Iterable[Endpoint[_, _, _, _]]): SecuritySchemes = { val auths = es.flatMap(e => e.input.auths) val authSecuritySchemes = auths.map(a => (a, authToSecurityScheme(a))) val securitySchemes = authSecuritySchemes.map(_._2).toSet val namedSecuritySchemes = nameSecuritySchemes(securitySchemes.toVector, Set(), Map()) authSecuritySchemes.map { case (a, s) => a -> ((namedSecuritySchemes(s), s)) }.toMap } @tailrec private def nameSecuritySchemes( schemes: Vector[SecurityScheme], takenNames: Set[SchemeName], acc: Map[SecurityScheme, SchemeName] ): Map[SecurityScheme, SchemeName] = { schemes match { case Vector() => acc case scheme +: tail => val baseName = scheme.`type` + "Auth" val name = uniqueName(baseName, !takenNames.contains(_)) nameSecuritySchemes(tail, takenNames + name, acc + (scheme -> name)) } } private def authToSecurityScheme(a: EndpointInput.Auth[_]): SecurityScheme = a match { case EndpointInput.Auth.ApiKey(input) => val (name, in) = apiKeyInputNameAndIn(input.asVectorOfBasicInputs()) SecurityScheme("apiKey", None, Some(name), Some(in), None, None, None, None) case EndpointInput.Auth.Http(scheme, _) => SecurityScheme("http", None, None, None, Some(scheme.toLowerCase()), None, None, None) case EndpointInput.Auth.Oauth2(authorizationUrl, tokenUrl, scopes, refreshUrl, _) => SecurityScheme( "oauth2", None, None, None, None, None, Some(OAuthFlows(authorizationCode = Some(OAuthFlow(authorizationUrl, tokenUrl, refreshUrl, scopes)))), None ) case EndpointInput.Auth.ScopedOauth2(EndpointInput.Auth.Oauth2(authorizationUrl, tokenUrl, scopes, refreshUrl, _), _) => SecurityScheme( "oauth2", None, None, None, None, None, Some(OAuthFlows(authorizationCode = Some(OAuthFlow(authorizationUrl, tokenUrl, refreshUrl, scopes)))), None ) } private def apiKeyInputNameAndIn(input: Vector[EndpointInput.Basic[_]]) = input match { case Vector(EndpointIO.Header(name, _, _)) => (name, "header") case Vector(EndpointInput.Query(name, _, _)) => (name, "query") case Vector(EndpointInput.Cookie(name, _, _)) => (name, "cookie") case _ => throw new IllegalArgumentException(s"Api key authentication can only be read from headers, queries or cookies, not: $input") } }
Example 14
Source File: Newton.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package edu.neu.coe.csye._7200 import scala.annotation.tailrec import scala.util.{Failure, Success, Try} case class Newton(f: Double => Double, dfbydx: Double => Double) { private def step(x: Double, y: Double) = x - y / dfbydx(x) def solve(tries: Int, threshold: Double, initial: Double): Try[Double] = { @tailrec def inner(r: Double, n: Int): Try[Double] = { val y = f(r) if (math.abs(y) < threshold) Success(r) else if (n == 0) Failure(new Exception("failed to converge")) else inner(step(r, y), n - 1) } inner(initial, tries) } }
Example 15
package fr.hmil.roshttp.tools.io import java.io.{ByteArrayOutputStream, OutputStream, Writer, _} import scala.annotation.tailrec import scala.reflect.ClassTag def pipe(in: Reader, out: Writer): Unit = { val buffer = newBuffer[Char] @tailrec def loop(): Unit = { val size = in.read(buffer) if (size > 0) { out.write(buffer, 0, size) loop() } } loop() } @inline private def newBuffer[T: ClassTag] = new Array[T](4096) }
Example 16
Source File: Base64Encoder.scala From reactive-cli with Apache License 2.0 | 5 votes |
package com.lightbend.rp.reactivecli.http import scala.annotation.tailrec object Base64Encoder { private val charSet: Seq[Char] = ('A' to 'Z') ++ ('a' to 'z') ++ ('0' to '9') ++ Seq('+', '/') private val blockLength: Int = 3 private val padding: String = "=" def apply(in: String): String = { val charsToPad = if (in.length % blockLength == 0) 0 else blockLength - (in.length % blockLength) val data = in + ("\u0000" * charsToPad) val rightPad = padding * charsToPad @tailrec def encodeBlock(blockPosition: Int, result: Seq[Char]): Seq[Char] = if (blockPosition >= data.length) result else { val n = (data.charAt(blockPosition) << 16) + (data.charAt(blockPosition + 1) << 8) + data.charAt(blockPosition + 2) val encoded: Seq[Char] = Seq( charSet((n >> 18) & 63), charSet((n >> 12) & 63), charSet((n >> 6) & 63), charSet(n & 63)) encodeBlock(blockPosition = blockPosition + blockLength, result ++ encoded) } val chars = encodeBlock(blockPosition = 0, Seq.empty) chars.mkString.substring(0, chars.length - rightPad.length) + rightPad } }
Example 17
Source File: CustomCodeEntryPoint.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.workflowexecutor.customcode import java.util.concurrent.TimeoutException import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{Await, Promise} import org.apache.spark.api.java.JavaSparkContext import org.apache.spark.sql.DataFrame import org.apache.spark.{SparkConf, SparkContext} import io.deepsense.commons.utils.Logging import io.deepsense.deeplang._ import io.deepsense.sparkutils.SparkSQLSession class CustomCodeEntryPoint( val sparkContext: SparkContext, val sparkSQLSession: SparkSQLSession, val dataFrameStorage: DataFrameStorage, val operationExecutionDispatcher: OperationExecutionDispatcher) extends Logging { import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint._ def getSparkContext: JavaSparkContext = sparkContext def getSparkSQLSession: SparkSQLSession = sparkSQLSession def getNewSparkSQLSession: SparkSQLSession = sparkSQLSession.newSession() def getSparkConf: SparkConf = sparkContext.getConf private val codeExecutor: AtomicReference[Promise[CustomCodeExecutor]] = new AtomicReference(Promise()) private val pythonPort: AtomicReference[Promise[Int]] = new AtomicReference(Promise()) def getCodeExecutor(timeout: Duration): CustomCodeExecutor = getFromPromise(codeExecutor.get, timeout) def getPythonPort(timeout: Duration): Int = getFromPromise(pythonPort.get, timeout) def registerCodeExecutor(newCodeExecutor: CustomCodeExecutor): Unit = replacePromise(codeExecutor, newCodeExecutor) def registerCallbackServerPort(newPort: Int): Unit = replacePromise(pythonPort, newPort) def retrieveInputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getInputDataFrame(workflowId, nodeId, portNumber).get def retrieveOutputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getOutputDataFrame(workflowId, nodeId, portNumber).get def registerOutputDataFrame( workflowId: String, nodeId: String, portNumber: Int, dataFrame: DataFrame): Unit = dataFrameStorage.setOutputDataFrame(workflowId, nodeId, portNumber, dataFrame) def executionCompleted(workflowId: String, nodeId: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Right(())) def executionFailed(workflowId: String, nodeId: String, error: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Left(error)) } object CustomCodeEntryPoint { private case class PromiseReplacedException() extends Exception @tailrec private def getFromPromise[T](promise: => Promise[T], timeout: Duration): T = { try { Await.result(promise.future, timeout) } catch { case e: TimeoutException => throw e case e: PromiseReplacedException => getFromPromise(promise, timeout) } } private def replacePromise[T](promise: AtomicReference[Promise[T]], newValue: T): Unit = { val oldPromise = promise.getAndSet(Promise.successful(newValue)) try { oldPromise.failure(new PromiseReplacedException) } catch { // The oldPromise will have been completed always, except for the first time. // The illegal state is expected, but we have to complete the oldPromise, // since someone might be waiting on it. case e: IllegalStateException => () } } case class CustomCodeEntryPointConfig( pyExecutorSetupTimeout: Duration = 5.seconds) }
Example 18
Source File: BacktestBenchmarks.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.concurrency.backtesting import java.util.concurrent.TimeUnit import highperfscala.concurrency.backtesting.Backtest.{BacktestPerformanceSummary, DecisionDelayMillis} import org.joda.time.{DateTime, Interval, MonthDay} import org.openjdk.jmh.annotations.Mode._ import org.openjdk.jmh.annotations._ import scala.annotation.tailrec @BenchmarkMode(Array(Throughput)) @OutputTimeUnit(TimeUnit.SECONDS) @Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 30, time = 10, timeUnit = TimeUnit.SECONDS) @Fork(value = 1, warmups = 1, jvmArgs = Array("-Xms1G", "-Xmx1G")) class BacktestBenchmarks { import BacktestBenchmarks._ @Benchmark def withoutConcurrency(state: BenchmarkState): BacktestPerformanceSummary = Backtest.backtestWithoutConcurrency(state.backtestDays, state.decisionDelay) .unsafePerformSync @Benchmark def withBatchedForking(state: BenchmarkState): BacktestPerformanceSummary = Backtest.backtestWithBatchedForking(state.backtestDays, state.decisionDelay) .unsafePerformSync @Benchmark def withAllForked(state: BenchmarkState): BacktestPerformanceSummary = Backtest.backtestWithAllForked(state.backtestDays, state.decisionDelay) .unsafePerformSync } object BacktestBenchmarks { private def daysWithin(i: Interval): List[MonthDay] = { @tailrec def recurse(xs: List[MonthDay], current: DateTime): List[MonthDay] = current.isAfter(i.getEnd) match { case true => xs case false => recurse( new MonthDay(current.getMonthOfYear, current.getDayOfMonth) :: xs, current.plusDays(1)) } recurse(Nil, i.getStart) } // Constant starting point to avoid differences due to number of days // per month private val end: DateTime = new DateTime(2016, 1, 1, 0, 0, 0, 0) private def trailingMonths(backtestIntervalMonths: Int): Interval = new Interval( end.minusMonths(backtestIntervalMonths), end) @State(Scope.Benchmark) class BenchmarkState { @Param(Array("1", "10")) var decisionDelayMillis: Long = 0 @Param(Array("1", "12", "24" )) var backtestIntervalMonths: Int = 0 var decisionDelay: DecisionDelayMillis = DecisionDelayMillis(-1) var backtestDays: List[MonthDay] = Nil @Setup def setup(): Unit = { decisionDelay = DecisionDelayMillis(decisionDelayMillis) backtestDays = daysWithin(trailingMonths(backtestIntervalMonths)) } } }
Example 19
Source File: MidpointSeries.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.dataanalysis import scala.annotation.tailrec class MidpointSeries private(val points: Vector[Midpoint]) extends AnyVal { def returns(rollUp: MinuteRollUp): Vector[Return] = { for { i <- (rollUp.value until points.size).toVector } yield Return.fromMidpoint(points(i - rollUp.value), points(i)) } def midpointAt(time: TimestampMinutes): Option[Midpoint] = { if (points.isEmpty || time.value < points.head.time.value || time.value > points.last.time.value) { None } else { val index = time.value - points.head.time.value Some(points(index)) } } private[dataanalysis] def size = points.size // provided for testing } object MidpointSeries { private def removeDuplicates(v: Vector[Midpoint]): Vector[Midpoint] = { @tailrec def loop( current: Midpoint, rest: Vector[Midpoint], result: Vector[Midpoint]): Vector[Midpoint] = { val sameTime = current +: rest.takeWhile(_.time == current.time) val average = sameTime.map(_.value).sum / sameTime.size val newResult = result :+ Midpoint(current.time, average) rest.drop(sameTime.size - 1) match { case h +: r => loop(h, r, newResult) case _ => newResult } } v match { case h +: rest => loop(h, rest, Vector.empty) case _ => Vector.empty } } private def extrapolate( a: Midpoint, b: Midpoint, time: TimestampMinutes): Midpoint = { val price = a.value + ((time.value - a.time.value) / (b.time.value - a.time.value)) * (b.value - a.value) Midpoint(time, price) } private def addMissingDataPoints(v: Vector[Midpoint]): Vector[Midpoint] = { @tailrec def loop( previous: Midpoint, rest: Vector[Midpoint], result: Vector[Midpoint]): Vector[Midpoint] = rest match { case current +: mPoints if previous.time.value == current.time.value - 1 => // Nothing to extrapolate, the data points are consecutive loop(current, mPoints, result :+ previous) case current +: mPoints if previous.time.value < current.time.value - 1 => //Need to generate a data point val newPoint = extrapolate(previous, current, previous.time.next) loop(newPoint, rest, result :+ previous) case _ => result :+ previous } v match { case h +: rest => loop(h, rest, Vector.empty) case _ => Vector.empty } } def fromExecution(executions: Vector[Execution]): MidpointSeries = { new MidpointSeries( addMissingDataPoints( removeDuplicates( executions.map(Midpoint.fromExecution)))) } }
Example 20
Source File: FirstLatencyBenchmark.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.benchmarks import java.io.File import highperfscala.benchmarks.util._ import highperfscala.orderbook.Commands.Command import highperfscala.orderbook.OrderBook import org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir import scala.annotation.tailrec object FirstLatencyBenchmark { def main(args: Array[String]): Unit = { val commandSample = DataCodec.read(new File(args(0))) val (commandsPerSecond, iterations) = (args(1).toInt, args(2).toInt) val totalCommandCount = commandsPerSecond * iterations jvmWarmUp(commandSample) @tailrec def sendCommands( xs: List[(List[Command], Int)], ob: OrderBook, testStart: Long, histogram: HdrHistogramReservoir): (OrderBook, HdrHistogramReservoir) = xs match { case head :: tail => val (batch, offsetInSeconds) = head val shouldStart = testStart + (1000 * offsetInSeconds) while (shouldStart > System.currentTimeMillis()) { // keep the thread busy while waiting for the next batch to be sent } val updatedBook = batch.foldLeft(ob) { case (accBook, c) => val operationStart = System.currentTimeMillis() val newBook = OrderBook.handle(accBook, c)._1 val operationEnd = System.currentTimeMillis() // record latency histogram.update(operationEnd - operationStart) newBook } sendCommands(tail, updatedBook, testStart, histogram) case Nil => (ob, histogram) } val (_, histogram) = sendCommands( // Organizes commands per 1 second batches generateCount(commandSample, totalCommandCount) .grouped(commandsPerSecond).zipWithIndex .toList, OrderBook.empty, System.currentTimeMillis(), new HdrHistogramReservoir()) printSnapshot(histogram.getSnapshot) } }
Example 21
Source File: FinalLatencyBenchmark.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala package benchmarks import java.io.File import highperfscala.orderbook.Commands.Command import highperfscala.orderbook.OrderBook import org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir import util._ import scala.annotation.tailrec object FinalLatencyBenchmark { case class CommandsPerSecond(value: Int) extends AnyVal case class BenchmarkIterationCount(value: Int) extends AnyVal case class CommandSentTimestamp(value: Long) extends AnyVal def runBenchmark( sampleCommands: List[Command], cps: CommandsPerSecond, count: BenchmarkIterationCount): Unit = { val totalCommandCount = cps.value * count.value jvmWarmUp(sampleCommands) @tailrec def sendCommands( xs: List[(Command, Int)], ob: OrderBook, testStart: Long, histogram: HdrHistogramReservoir): (OrderBook, HdrHistogramReservoir) = xs match { case head :: tail => val (command, offsetInMs) = head val shouldStart = testStart + offsetInMs while (shouldStart > System.currentTimeMillis()) { // keep the thread busy while waiting for the next batch to be sent } val newBook = OrderBook.handle(ob, command)._1 val operationEnd = System.currentTimeMillis() histogram.update(operationEnd - shouldStart) sendCommands(tail, newBook, testStart, histogram) case Nil => (ob, histogram) } val (_, histogram) = sendCommands( generateCount(sampleCommands, totalCommandCount) .grouped(cps.value) .toList.zipWithIndex .flatMap { case (secondBatch, sBatchIndex) => val batchOffsetInMs = sBatchIndex * 1000 val commandIntervalInMs = 1000.0 / cps.value secondBatch.zipWithIndex.map { case (command, commandIndex) => val commandOffsetInMs = Math.floor(commandIntervalInMs * commandIndex).toInt (command, batchOffsetInMs + commandOffsetInMs) } }, OrderBook.empty, System.currentTimeMillis(), new HdrHistogramReservoir()) printSnapshot(histogram.getSnapshot) } def main(args: Array[String]): Unit = { runBenchmark(DataCodec.read(new File(args(0))), CommandsPerSecond(args(1).toInt), BenchmarkIterationCount(args(2).toInt)) } }
Example 22
Source File: SecondLatencyBenchmark.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala package benchmarks import java.io.File import highperfscala.orderbook.Commands.Command import highperfscala.orderbook.OrderBook import org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir import util._ import scala.annotation.tailrec object SecondLatencyBenchmark { def main(args: Array[String]): Unit = { val commandSample = DataCodec.read(new File(args(0))) val (commandsPerSecond, iterations) = (args(1).toInt, args(2).toInt) val totalCommandCount = commandsPerSecond * iterations jvmWarmUp(commandSample) @tailrec def sendCommands( xs: List[(List[Command], Int)], ob: OrderBook, testStart: Long, histogram: HdrHistogramReservoir): (OrderBook, HdrHistogramReservoir) = xs match { case head :: tail => val (batch, offsetInSeconds) = head val shouldStart = testStart + (1000 * offsetInSeconds) while (shouldStart > System.currentTimeMillis()) { // keep the thread busy while waiting for the next batch to be sent } val updatedBook = batch.foldLeft(ob) { case (accBook, c) => val newBook = OrderBook.handle(accBook, c)._1 val operationEnd = System.currentTimeMillis() // record latency histogram.update(operationEnd - shouldStart) newBook } sendCommands(tail, updatedBook, testStart, histogram) case Nil => (ob, histogram) } val (_, histogram) = sendCommands( // Organizes commands per 1 second batches generateCount(commandSample, totalCommandCount) .grouped(commandsPerSecond).zipWithIndex .toList, OrderBook.empty, System.currentTimeMillis(), new HdrHistogramReservoir()) printSnapshot(histogram.getSnapshot) } }
Example 23
Source File: StreamsExamples.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.clientreports.streams import scala.annotation.tailrec object StreamsExamples { def powerOf2: Stream[Int] = { def next(n: Int): Stream[Int] = { println(s"Adding $n") n #:: next(2 * n) } 1 #:: next(1) } def recursivePowerOf2(n: Int): Stream[Int] = math.pow(2, n).toInt #:: recursivePowerOf2(n+1) @tailrec def drop[A](s: Stream[A], count: Int): Stream[A] = count match { case 0 => s case n if n > 0 => drop(s.tail, count - 1) case n if n < 0 => throw new Exception("cannot drop negative count") } def max(s: => Stream[Int]): Option[Int] = { @tailrec def loop(ss: Stream[Int], current: Option[Int]): Option[Int] = ss match { case Stream.Empty => current case h #:: rest if current.exists(_ >= h) => loop(rest, current) case h #:: rest => loop(rest, Some(h)) } loop(s, None) } def memoizingMax(s: => Stream[Int]): Option[Int] = { @tailrec def loop(ss: Stream[Int], current: Int): Int = ss match { case Stream.Empty => current case h #:: rest if h > current => loop(rest, h) case h #:: rest if h <= current => loop(rest, current) } // This pattern matching creates a reference to the tail of s and // prevents eager garbage collection of the intermediate elements. s match { case Stream.Empty => None case h #:: rest => Some(loop(rest, h)) } } def range(s: Seq[Int]): Int = s.max - s.min }
Example 24
Source File: Dual.scala From newts with Apache License 2.0 | 5 votes |
package newts import cats.kernel.Eq import cats.syntax.functor._ import cats.{Applicative, Distributive, Eval, Functor, Monad, Monoid, Semigroup, Show, Traverse} import scala.annotation.tailrec final case class Dual[A](getDual: A) extends AnyVal object Dual extends DualInstances0 { implicit def newtypeInstance[A]: Newtype.Aux[Dual[A], A] = Newtype.from[Dual[A], A](Dual.apply)(_.getDual) implicit val monadInstance: Monad[Dual] = new Monad[Dual] { def pure[A](x: A): Dual[A] = Dual(x) def flatMap[A, B](fa: Dual[A])(f: A => Dual[B]): Dual[B] = f(fa.getDual) @tailrec def tailRecM[A, B](a: A)(f: A => Dual[Either[A, B]]): Dual[B] = f(a) match { case Dual(Left(a1)) => tailRecM(a1)(f) case Dual(Right(b)) => Dual(b) } } implicit def semigroupInstance[A](implicit A: Semigroup[A]): Semigroup[Dual[A]] = new Semigroup[Dual[A]]{ def combine(x: Dual[A], y: Dual[A]): Dual[A] = Dual(A.combine(y.getDual, x.getDual)) } implicit def showInstance[A](implicit ev: Show[A]): Show[Dual[A]] = Show.show(a => s"Dual(${Show[A].show(a.getDual)})" ) implicit def dualEq[A: Eq]: Eq[Dual[A]] = Eq.by(_.getDual) } trait DualInstances0 { implicit def monoidInstances[A](implicit A: Monoid[A]): Monoid[Dual[A]] = new Monoid[Dual[A]]{ def empty: Dual[A] = Dual(A.empty) def combine(x: Dual[A], y: Dual[A]): Dual[A] = Dual(A.combine(y.getDual, x.getDual)) } implicit val traverseInstance: Traverse[Dual] = new Traverse[Dual] { def traverse[G[_], A, B](fa: Dual[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Dual[B]] = f(fa.getDual).map(Dual(_)) def foldLeft[A, B](fa: Dual[A], b: B)(f: (B, A) => B): B = f(b, fa.getDual) def foldRight[A, B](fa: Dual[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getDual, lb) } implicit val distributiveInstance: Distributive[Dual] = new Distributive[Dual] { def distribute[G[_], A, B](ga: G[A])(f: A => Dual[B])(implicit ev: Functor[G]): Dual[G[B]] = Dual(ga.map(f(_).getDual)) def map[A, B](fa: Dual[A])(f: A => B): Dual[B] = Dual(f(fa.getDual)) } }
Example 25
Source File: SubString.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.utils import ru.tinkoff.tschema.utils.SubString.outOfBound import scala.annotation.tailrec import scala.util.hashing.MurmurHash3 final class SubString private[SubString] (private val arr: Array[Char], private val from: Int, private val to: Int) extends CharSequence { @inline private[this] def size = to - from def length(): Int = size def charAt(index: Int): Char = { if (index >= size || index < 0) outOfBound(index) arr(from + index) } def subSequence(start: Int, end: Int): CharSequence = { if (start < 0) outOfBound(start) if (end > size) outOfBound(end) val len = end - start if (len < 0) outOfBound(len) if ((start == 0) && (end == size)) this else new SubString(arr, from + start, from + end) } override def equals(obj: Any): Boolean = obj match { case s: CharSequence => @tailrec def go(i: Int): Boolean = (i == size) || ((s.charAt(i) == charAt(i)) && go(i + 1)) size == s.length() && go(0) case _ => false } override def hashCode(): Int = { var h = MurmurHash3.stringSeed var i = from while (i + 1 < to) { val data = (arr(i) << 16) + arr(i + 1) h = MurmurHash3.mix(h, data) i += 2 } if (i < to) h = MurmurHash3.mixLast(h, arr(i).toInt) MurmurHash3.finalizeHash(h, size) } override def toString: String = new String(arr.slice(from, to)) } object SubString { @inline private def outOfBound(index: Int) = throw new StringIndexOutOfBoundsException(index) def apply(s: String): SubString = new SubString(s.toCharArray, 0, s.length) }
Example 26
Source File: Authorize.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.example import cats.Applicative import com.twitter.finagle.http.Response import com.twitter.util.Base64StringEncoder import derevo.circe.{decoder, encoder} import derevo.derive import derevo.tethys.{tethysReader, tethysWriter} import ru.tinkoff.tschema.finagle.Authorization.{Basic, Bearer, Kind} import ru.tinkoff.tschema.finagle.{Authorization, Credentials, Rejection, Routed, SimpleAuth, BearerToken} import ru.tinkoff.tschema.swagger._ import ru.tinkoff.tschema.finagle._ import ru.tinkoff.tschema.syntax._ import shapeless.{HNil, Witness} import cats.syntax.applicative._ import ru.tinkoff.tschema.finagle.Credentials.secure_equals import ru.tinkoff.tschema.finagle.util.Unapply import scala.annotation.tailrec import ru.tinkoff.tschema.finagle.tethysInstances._ object Authorize extends ExampleModule { implicitly[Routed[Http]] override def route: Http[Response] = MkService[Http](api)(handler) override def swag: SwaggerBuilder = MkSwagger(api) final case class User(name: String, roles: List[String]) @derive(tethysWriter, tethysReader, Swagger) final case class Client(name: String, products: List[String]) val anonClient = Client("anon", List.empty) val users = Unapply( Map( "admin" -> ("adminadmin", List("admin")), "guest" -> ("guest", List()) ) ) val clients = Unapply( Map( "123456" -> Client("client", List("diamond card", "premium subscription")) ) ) val sessions = Map( "x123" -> List(1, 2, 3), "x12" -> List(1, 2), "y" -> List.empty ) implicit val userAuth: Authorization[Basic, Http, User] = SimpleAuth { case Credentials(id @ users(pass, roles), pwd) if secure_equals(pass, pwd) => User(id, roles) } implicit val clientAuth: Authorization[Bearer, Http, Client] = SimpleAuth { case BearerToken(clients(client)) => client } def api = tagPrefix("auth") |> (( operation("roles") |> basicAuth[User]("users", "user") |> get[List[String]] ) <> ( operation("client") |> bearerAuth[Option[Client]]("clients", "client") |> get[Client] ) <> ( operation("numbers") |> apiKeyAuth("sessionId", queryParam[Option[String]]("sessionId")) |> get[List[Int]] )) object handler { def roles(user: User): List[String] = user.roles def client(client: Option[Client]): Client = client.getOrElse(anonClient) def numbers(sessionId: Option[String]): List[Int] = sessionId.flatMap(sessions.get).getOrElse(List(-1)) } }
Example 27
Source File: Authorize.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.example import cats.Applicative import com.twitter.finagle.http.Response import com.twitter.util.Base64StringEncoder import org.manatki.derevo.circeDerivation.{decoder, encoder} import org.manatki.derevo.derive import org.manatki.derevo.tethysInstances.{tethysReader, tethysWriter} import org.manatki.derevo.tschemaInstances._ import ru.tinkoff.tschema.finagle.Authorization.{Basic, Bearer, Kind} import ru.tinkoff.tschema.finagle.{Authorization, Credentials, MkService, Rejection, Routed, SimpleAuth, BearerToken} import ru.tinkoff.tschema.swagger.{SwaggerBuilder, _} import ru.tinkoff.tschema.syntax._ import shapeless.{HNil, Witness} import cats.syntax.applicative._ import ru.tinkoff.tschema.finagle.Credentials.secure_equals import ru.tinkoff.tschema.finagle.util.Unapply import scala.annotation.tailrec import ru.tinkoff.tschema.finagle.tethysInstances._ object Authorize extends ExampleModule { override def route: Http[Response] = MkService[Http](api)(handler) override def swag: SwaggerBuilder = MkSwagger(api) final case class User(name: String, roles: List[String]) @derive(tethysWriter, tethysReader, swagger) final case class Client(name: String, products: List[String]) val anonClient = Client("anon", List.empty) val users = Unapply( Map( "admin" -> ("adminadmin", List("admin")), "guest" -> ("guest", List()) )) val clients = Unapply( Map( "123456" -> Client("client", List("diamond card", "premium subscription")) )) val sessions = Map( "x123" -> List(1, 2, 3), "x12" -> List(1, 2), "y" -> List.empty ) implicit val userAuth: Authorization[Basic, Http, User] = SimpleAuth { case Credentials(id @ users(pass, roles), pwd) if secure_equals(pass, pwd) => User(id, roles) } implicit val clientAuth: Authorization[Bearer, Http, Client] = SimpleAuth { case BearerToken(clients(client)) => client } def api = tagPrefix('auth) |> (( operation('roles) |> basicAuth[User]("users", 'user) |> get[List[String]] ) <> ( operation('client) |> bearerAuth[Option[Client]]("clients", 'client) |> get[Client] ) <> ( operation('numbers) |> apiKeyAuth('sessionId, queryParam[Option[String]]('sessionId)) |> get[List[Int]] )) object handler { def roles(user: User): List[String] = user.roles def client(client: Option[Client]): Client = client.getOrElse(anonClient) def numbers(sessionId: Option[String]): List[Int] = sessionId.flatMap(sessions.get).getOrElse(List(-1)) } }
Example 28
Source File: DerivationUtils.scala From zio-config with Apache License 2.0 | 5 votes |
package zio.config.derivation import zio.config._ import scala.annotation.tailrec import scala.collection.JavaConverters._ object DerivationUtils { case class ConstantString(value: String) extends PropertyType[String, String] { def read(propertyValue: String): Either[PropertyType.PropertyReadError[String], String] = if (propertyValue == value) Right(value) else Left(PropertyType.PropertyReadError(propertyValue, s"constant string '$value'")) def write(a: String): String = a } def constantString(value: String): ConfigDescriptor[String] = ConfigDescriptorAdt.Source(ConfigSource.empty, ConstantString(value)) ?? s"constant string '$value'" def constant[T](label: String, value: T): ConfigDescriptor[T] = constantString(label)(_ => value, p => Some(p).filter(_ == value).map(_ => label)) def toSnakeCase(name: String): String = { def addToAcc(acc: List[String], current: List[Int]) = { def currentWord = current.reverse.flatMap(i => Character.toChars(i)).mkString.toLowerCase if (current.isEmpty) acc else if (acc.isEmpty) currentWord :: Nil else currentWord :: "_" :: acc } @tailrec def loop(chars: List[Int], acc: List[String], current: List[Int], beginning: Boolean): String = chars match { case Nil => addToAcc(acc, current).reverse.mkString case head :: tail if beginning => loop(tail, acc, head :: current, Character.isUpperCase(head) || !Character.isLetter(head)) case head :: tail if Character.isUpperCase(head) => loop(tail, addToAcc(acc, current), head :: Nil, beginning = true) case head :: tail => loop(tail, acc, head :: current, beginning = false) } loop(name.codePoints().iterator().asScala.map(x => x: Int).toList, Nil, Nil, beginning = true) } }
Example 29
Source File: KeyConversionFunctions.scala From zio-config with Apache License 2.0 | 5 votes |
package zio.config import scala.annotation.tailrec private[config] trait KeyConversionFunctions { private def camelToDelimiter(delimiter: String): String => String = s => { @tailrec def loop(s: String, output: String, lastUppercase: Boolean): String = if (s.isEmpty) output else { val c = if (s.head.isUpper && !lastUppercase) delimiter + s.head.toLower else s.head.toLower loop(s.tail, output + c, s.head.isUpper && !lastUppercase) } loop(s, "", lastUppercase = true) } val camelToKebab: String => String = camelToDelimiter("-") val camelToSnake: String => String = camelToDelimiter("_") def addPrefix(prefix: String): String => String = s => s"${prefix}${s}" def postFix(string: String): String => String = s => s"${s}${string}" }
Example 30
Source File: NodeToJson.scala From crjdt with Apache License 2.0 | 5 votes |
package eu.timepit.crjdt.circe import eu.timepit.crjdt.core.Node.{ListNode, MapNode, RegNode} import eu.timepit.crjdt.core._ import io.circe.Json import scala.annotation.tailrec private[circe] trait NodeToJson { protected def mapToJson( mapNode: MapNode )(implicit rcr: RegNodeConflictResolver): Json = if (mapNode.children.contains(TypeTag.MapT(Key.DocK))) mapNode.getChild(TypeTag.MapT(Key.DocK)) match { case node: MapNode => mapToJson(node) case node: ListNode => listToJson(node) case node: RegNode => rcr.registerToJson(node) } else { val fields = mapNode.children.collect { case (TypeTag.MapT(Key.StrK(key)), node: MapNode) if mapNode.getPres(Key.StrK(key)).nonEmpty => key -> mapToJson(node) case (TypeTag.ListT(Key.StrK(key)), node: ListNode) if mapNode.getPres(Key.StrK(key)).nonEmpty => key -> listToJson(node) case (TypeTag.RegT(Key.StrK(key)), node: RegNode) if mapNode.getPres(Key.StrK(key)).nonEmpty => key -> rcr.registerToJson(node) } Json.fromFields(fields) } protected def listToJson( listNode: ListNode )(implicit rcr: RegNodeConflictResolver): Json = { @tailrec def loopOrder(listRef: ListRef, keyOrder: Vector[Key]): Vector[Key] = listRef match { case keyRef: KeyRef => val key = keyRef.toKey val next = listNode.order(keyRef) if (listNode.getPres(key).nonEmpty) loopOrder(next, keyOrder :+ key) else loopOrder(next, keyOrder) case ListRef.TailR => keyOrder } val keyOrder = loopOrder(ListRef.HeadR, Vector.empty).zipWithIndex.toMap val jsons = new Array[Json](keyOrder.size) listNode.children.collect { case (TypeTag.MapT(key), node: MapNode) if listNode.getPres(key).nonEmpty => jsons(keyOrder(key)) = mapToJson(node) case (TypeTag.ListT(key), node: ListNode) if listNode.getPres(key).nonEmpty => jsons(keyOrder(key)) = listToJson(node) case (TypeTag.RegT(key), node: RegNode) if listNode.getPres(key).nonEmpty => jsons(keyOrder(key)) = rcr.registerToJson(node) } Json.fromValues(jsons) } }
Example 31
Source File: Nat.scala From scala_typeclassopedia with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
package typetheory import scala.annotation.tailrec import typetheory.logic.Bool._ object Nat { sealed trait Nat case object Zero extends Nat case class Succ(n: Nat) extends Nat // unary operations def inc(n: Nat): Nat = Succ(n) def dec(n: Nat): Nat = n match { case Zero => Zero case Succ(m) => m } // binary operators @tailrec def plus(lhs: Nat, rhs: Nat): Nat = lhs match { case Zero => rhs case Succ(m) => plus(m, Succ(rhs)) } def times(lhs: Nat, rhs: Nat): Nat = (lhs, rhs) match { case (Zero, _) => Zero case (Succ(m), n) => plus(m, times(m, n)) } @tailrec def monus(lhs: Nat, rhs: Nat): Nat = (lhs, rhs) match { case (Zero, _) => Zero case (Succ(m), Succ(n)) => monus(m,n) } // relations @tailrec def eq(lhs: Nat, rhs: Nat): Bool = (lhs, rhs) match { case (Zero, Succ(_)) => False case (Succ(m), Succ(n)) => eq(m,n) } @tailrec def lq(lhs: Nat, rhs: Nat): Bool = (lhs, rhs) match { case (Zero, Succ(_)) => True case (Succ(m), Succ(n)) => lq(m,n) } }
Example 32
Source File: DiscreteSampler.scala From zen with Apache License 2.0 | 5 votes |
package com.github.cloudml.zen.ml.sampler import java.util.Random import scala.annotation.tailrec import spire.math.{Numeric => spNum} trait DiscreteSampler[@specialized(Double, Int, Float, Long) T] extends Sampler[T] { def length: Int def used: Int def update(state: Int, value: => T): Unit def deltaUpdate(state: Int, delta: => T): Unit def resetDist(probs: Array[T], space: Array[Int], psize: Int): DiscreteSampler[T] def resetDist(distIter: Iterator[(Int, T)], psize: Int): DiscreteSampler[T] def reset(newSize: Int): DiscreteSampler[T] @tailrec final def resampleRandom(gen: Random, state: Int, residualRate: Double, numResampling: Int = 2)(implicit ev: spNum[T]): Int = { val newState = sampleRandom(gen) if (newState == state && numResampling >= 0 && used > 1 && (residualRate >= 1.0 || gen.nextDouble() < residualRate)) { resampleRandom(gen, state, residualRate, numResampling - 1) } else { newState } } @tailrec final def resampleFrom(base: T, gen: Random, state: Int, residualRate: Double, numResampling: Int = 2)(implicit ev: spNum[T]): Int = { val newState = sampleFrom(base, gen) if (newState == state && numResampling >= 0 && used > 1 && (residualRate >= 1.0 || gen.nextDouble() < residualRate)) { val newBase = ev.fromDouble(gen.nextDouble() * ev.toDouble(norm)) resampleFrom(newBase, gen, state, residualRate, numResampling - 1) } else { newState } } }
Example 33
Source File: First.scala From newts with Apache License 2.0 | 5 votes |
package newts import cats.{Applicative, Distributive, Eq, Eval, Functor, Monad, Semigroup, SemigroupK, Show, Traverse} import cats.syntax.functor._ import scala.annotation.tailrec final case class First[A](getFirst: A) extends AnyVal object First extends FirstInstances0 { implicit def newtypeInstance[A]: Newtype.Aux[First[A], A] = Newtype.from[First[A], A](First.apply)(_.getFirst) implicit val monadInstance: Monad[First] = new Monad[First] { def pure[A](x: A): First[A] = First(x) def flatMap[A, B](fa: First[A])(f: A => First[B]): First[B] = f(fa.getFirst) @tailrec def tailRecM[A, B](a: A)(f: A => First[Either[A, B]]): First[B] = f(a) match { case First(Left(a1)) => tailRecM(a1)(f) case First(Right(b)) => First(b) } } implicit val semigroupKInstance: SemigroupK[First] = new SemigroupK[First] { def combineK[A](x: First[A], y: First[A]) = x } implicit def semigroupInstance[A]: Semigroup[First[A]] = SemigroupK[First].algebra implicit def eqInstance[A: Eq]: Eq[First[A]] = Eq.by(_.getFirst) implicit def showInstance[A](implicit ev: Show[A]): Show[First[A]] = Show.show(a => s"First(${Show[A].show(a.getFirst)})" ) } trait FirstInstances0 { implicit val traverseInstance: Traverse[First] = new Traverse[First] { def traverse[G[_], A, B](fa: First[A])(f: A => G[B])(implicit ev: Applicative[G]): G[First[B]] = f(fa.getFirst).map(First(_)) def foldLeft[A, B](fa: First[A], b: B)(f: (B, A) => B): B = f(b, fa.getFirst) def foldRight[A, B](fa: First[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getFirst, lb) } implicit val distributiveInstance: Distributive[First] = new Distributive[First] { def distribute[G[_], A, B](ga: G[A])(f: A => First[B])(implicit ev: Functor[G]): First[G[B]] = First(ga.map(f(_).getFirst)) def map[A, B](fa: First[A])(f: A => B): First[B] = First(f(fa.getFirst)) } }
Example 34
Source File: Last.scala From newts with Apache License 2.0 | 5 votes |
package newts import cats.{Applicative, Distributive, Eq, Eval, Functor, Monad, Semigroup, SemigroupK, Show, Traverse} import cats.syntax.functor._ import scala.annotation.tailrec final case class Last[A](getLast: A) extends AnyVal object Last extends LastInstances0 { implicit def newtypeInstance[A]: Newtype.Aux[Last[A], A] = Newtype.from[Last[A], A](Last.apply)(_.getLast) implicit val monadInstance: Monad[Last] = new Monad[Last] { def pure[A](x: A): Last[A] = Last(x) def flatMap[A, B](fa: Last[A])(f: A => Last[B]): Last[B] = f(fa.getLast) @tailrec def tailRecM[A, B](a: A)(f: A => Last[Either[A, B]]): Last[B] = f(a) match { case Last(Left(a1)) => tailRecM(a1)(f) case Last(Right(b)) => Last(b) } } implicit val semigroupKInstance: SemigroupK[Last] = new SemigroupK[Last] { def combineK[A](x: Last[A], y: Last[A]) = y } implicit def semigroupInstance[A]: Semigroup[Last[A]] = SemigroupK[Last].algebra implicit def eqInstance[A: Eq]: Eq[Last[A]] = Eq.by(_.getLast) implicit def showInstance[A](implicit ev: Show[A]): Show[Last[A]] = Show.show(a => s"Last(${ev.show(a.getLast)})" ) } trait LastInstances0 { implicit val traverseInstance: Traverse[Last] = new Traverse[Last] { def traverse[G[_], A, B](fa: Last[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Last[B]] = f(fa.getLast).map(Last(_)) def foldLeft[A, B](fa: Last[A], b: B)(f: (B, A) => B): B = f(b, fa.getLast) def foldRight[A, B](fa: Last[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getLast, lb) } implicit val distributiveInstance: Distributive[Last] = new Distributive[Last] { def distribute[G[_], A, B](ga: G[A])(f: A => Last[B])(implicit ev: Functor[G]): Last[G[B]] = Last(ga.map(f(_).getLast)) def map[A, B](fa: Last[A])(f: A => B): Last[B] = Last(f(fa.getLast)) } }
Example 35
Source File: ShapelessMacros.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.macros import shapeless.ReprTypes import scala.annotation.tailrec import scala.reflect.macros.blackbox trait ShapelessMacros extends ReprTypes with MacroMessages with SymbolMacros { val c: blackbox.Context import c.universe._ def unfoldCompoundTpe(compoundTpe: Type, nil: Type, cons: Type): List[Type] = { @tailrec def loop(tpe: Type, acc: List[Type]): List[Type] = tpe.dealias match { case TypeRef(_, consSym, List(hd, tl)) if consSym.asType.toType.typeConstructor =:= cons => loop(tl, hd :: acc) case `nil` => acc case other => abort(s"Bad compound type $compoundTpe") } loop(compoundTpe, Nil).reverse } def hlistElements(tpe: Type): List[Type] = unfoldCompoundTpe(tpe, hnilTpe, hconsTpe) def extractRecord(tpe: Type): List[Option[(String, Type)]] = hlistElements(tpe).map { case FieldType(KeyName(name), value) => Some(name -> value) case _ => None } object FieldType { import internal._ def apply(kTpe: Type, vTpe: Type): Type = refinedType(List(vTpe, typeRef(keyTagTpe, keyTagTpe.typeSymbol, List(kTpe, vTpe))), NoSymbol) def unapply(fTpe: Type): Option[(Type, Type)] = { val KeyTagPre = keyTagTpe fTpe.dealias match { case RefinedType(List(v0, TypeRef(pre, sym, List(k, v1))), _) if sym.asType.toType.typeConstructor =:= KeyTagPre && v0 =:= v1 => Some((k, v0)) case _ => None } } } }
Example 36
Source File: Mult.scala From newts with Apache License 2.0 | 5 votes |
package newts import cats.kernel.{CommutativeMonoid, Eq} import cats.syntax.functor._ import cats.{Applicative, CommutativeMonad, Distributive, Eval, Functor, Show, Traverse} import scala.annotation.tailrec final case class Mult[A](getMult: A) extends AnyVal object Mult extends MultInstances0 { implicit def newtypeInstance[A]: Newtype.Aux[Mult[A], A] = Newtype.from[Mult[A], A](Mult.apply)(_.getMult) implicit val monadInstance: CommutativeMonad[Mult] = new CommutativeMonad[Mult] { def pure[A](x: A): Mult[A] = Mult(x) def flatMap[A, B](fa: Mult[A])(f: A => Mult[B]): Mult[B] = f(fa.getMult) @tailrec def tailRecM[A, B](a: A)(f: A => Mult[Either[A, B]]): Mult[B] = f(a) match { case Mult(Left(a1)) => tailRecM(a1)(f) case Mult(Right(b)) => Mult(b) } } implicit def monoidInstance[A](implicit num: Numeric[A]): CommutativeMonoid[Mult[A]] = new CommutativeMonoid[Mult[A]] { val empty: Mult[A] = Mult(num.one) def combine(x: Mult[A], y: Mult[A]): Mult[A] = Mult(num.times(x.getMult, y.getMult)) } implicit def eqInstance[A: Eq]: Eq[Mult[A]] = Eq.by(_.getMult) implicit def showInstance[A](implicit ev: Show[A]): Show[Mult[A]] = Show.show(a => s"Mult(${ev.show(a.getMult)})" ) } trait MultInstances0 { implicit val traverseInstance: Traverse[Mult] = new Traverse[Mult] { def traverse[G[_], A, B](fa: Mult[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Mult[B]] = f(fa.getMult).map(Mult(_)) def foldLeft[A, B](fa: Mult[A], b: B)(f: (B, A) => B): B = f(b, fa.getMult) def foldRight[A, B](fa: Mult[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getMult, lb) } implicit val distributiveInstance: Distributive[Mult] = new Distributive[Mult] { def distribute[G[_], A, B](ga: G[A])(f: A => Mult[B])(implicit ev: Functor[G]): Mult[G[B]] = Mult(ga.map(f(_).getMult)) def map[A, B](fa: Mult[A])(f: A => B): Mult[B] = Mult(f(fa.getMult)) } }
Example 37
Source File: FirstOption.scala From newts with Apache License 2.0 | 5 votes |
package newts import cats.{Alternative, Applicative, Eval, Monad, Monoid, MonoidK, Show, Traverse} import cats.instances.option._ import cats.syntax.functor._ import cats.syntax.traverse._ import cats.kernel.Eq import scala.annotation.tailrec final case class FirstOption[A](getFirstOption: Option[A]) extends AnyVal object FirstOption extends FirstOptionInstances0 { implicit def newtypeInstance[A]: Newtype.Aux[FirstOption[A], Option[A]] = Newtype.from[FirstOption[A], Option[A]](FirstOption.apply)(_.getFirstOption) implicit val monadAlternativeInstance: Monad[FirstOption] with Alternative[FirstOption] = new Monad[FirstOption] with Alternative[FirstOption] { def empty[A]: FirstOption[A] = FirstOption(None) def combineK[A](x: FirstOption[A], y: FirstOption[A]): FirstOption[A] = FirstOption(x.getFirstOption.orElse(y.getFirstOption)) def pure[A](x: A): FirstOption[A] = FirstOption(Some(x)) def flatMap[A, B](fa: FirstOption[A])(f: A => FirstOption[B]): FirstOption[B] = fa.getFirstOption.fold(empty[B])(f) @tailrec def tailRecM[A, B](a: A)(f: A => FirstOption[Either[A, B]]): FirstOption[B] = f(a).getFirstOption match { case None => empty case Some(Left(a1)) => tailRecM(a1)(f) case Some(Right(b)) => pure(b) } } implicit def monoidInstance[A]: Monoid[FirstOption[A]] = MonoidK[FirstOption].algebra implicit def eqInstance[A: Eq]: Eq[FirstOption[A]] = Eq.by(_.getFirstOption) implicit def showInstance[A: Show]: Show[FirstOption[A]] = Show.show(a => s"FirstOption(${Show[Option[A]].show(a.getFirstOption)})" ) } trait FirstOptionInstances0 { implicit val traverseInstance: Traverse[FirstOption] = new Traverse[FirstOption] { def traverse[G[_], A, B](fa: FirstOption[A])(f: A => G[B])(implicit ev: Applicative[G]): G[FirstOption[B]] = fa.getFirstOption.traverse(f).map(FirstOption(_)) def foldLeft[A, B](fa: FirstOption[A], b: B)(f: (B, A) => B): B = fa.getFirstOption.fold(b)(f(b, _)) def foldRight[A, B](fa: FirstOption[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = fa.getFirstOption.fold(lb)(f(_, lb)) } }
Example 38
package newts import cats.kernel.{CommutativeMonoid, CommutativeSemigroup, Order} import cats.syntax.functor._ import cats.syntax.order._ import cats.{Applicative, CommutativeMonad, Distributive, Eval, Functor, Show, Traverse} import newts.internal.MaxBounded import scala.annotation.tailrec final case class Min[A](getMin: A) extends AnyVal object Min extends MinInstances0{ implicit def newtypeInstance[A]: Newtype.Aux[Min[A], A] = Newtype.from[Min[A], A](Min.apply)(_.getMin) implicit val monadInstance: CommutativeMonad[Min] = new CommutativeMonad[Min] { def pure[A](x: A): Min[A] = Min(x) def flatMap[A, B](fa: Min[A])(f: A => Min[B]): Min[B] = f(fa.getMin) @tailrec def tailRecM[A, B](a: A)(f: A => Min[Either[A, B]]): Min[B] = f(a) match { case Min(Left(a1)) => tailRecM(a1)(f) case Min(Right(b)) => Min(b) } } implicit def instances[A: Order]: Order[Min[A]] with CommutativeSemigroup[Min[A]] = new Order[Min[A]] with CommutativeSemigroup[Min[A]] { def combine(x: Min[A], y: Min[A]): Min[A] = Min(x.getMin min y.getMin) def compare(x: Min[A], y: Min[A]): Int = x.getMin compare y.getMin } implicit def showInstance[A](implicit ev: Show[A]): Show[Min[A]] = Show.show(a => s"Min(${ev.show(a.getMin)})" ) } trait MinInstances0{ implicit def minMonoid[A](implicit A: MaxBounded[A]): CommutativeMonoid[Min[A]] = new CommutativeMonoid[Min[A]]{ def empty: Min[A] = Min(MaxBounded[A].maxValue) def combine(x: Min[A], y: Min[A]): Min[A] = Min(x.getMin min y.getMin) } implicit val traverseInstance: Traverse[Min] = new Traverse[Min] { def traverse[G[_], A, B](fa: Min[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Min[B]] = f(fa.getMin).map(Min(_)) def foldLeft[A, B](fa: Min[A], b: B)(f: (B, A) => B): B = f(b, fa.getMin) def foldRight[A, B](fa: Min[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getMin, lb) } implicit val distributiveInstance: Distributive[Min] = new Distributive[Min] { def distribute[G[_], A, B](ga: G[A])(f: A => Min[B])(implicit ev: Functor[G]): Min[G[B]] = Min(ga.map(f(_).getMin)) def map[A, B](fa: Min[A])(f: A => B): Min[B] = Min(f(fa.getMin)) } }
Example 39
package newts import cats.kernel.{CommutativeMonoid, CommutativeSemigroup, Order} import cats.syntax.functor._ import cats.syntax.order._ import cats.{Applicative, CommutativeMonad, Distributive, Eval, Functor, Show, Traverse} import newts.internal.MinBounded import scala.annotation.tailrec final case class Max[A](getMax: A) extends AnyVal object Max extends MaxInstances0{ implicit def newtypeInstance[A]: Newtype.Aux[Max[A], A] = Newtype.from[Max[A], A](Max.apply)(_.getMax) implicit val monadInstance: CommutativeMonad[Max] = new CommutativeMonad[Max] { def pure[A](x: A): Max[A] = Max(x) def flatMap[A, B](fa: Max[A])(f: A => Max[B]): Max[B] = f(fa.getMax) @tailrec def tailRecM[A, B](a: A)(f: A => Max[Either[A, B]]): Max[B] = f(a) match { case Max(Left(a1)) => tailRecM(a1)(f) case Max(Right(b)) => Max(b) } } implicit def instances[A: Order]: Order[Max[A]] with CommutativeSemigroup[Max[A]] = new Order[Max[A]] with CommutativeSemigroup[Max[A]] { def combine(x: Max[A], y: Max[A]): Max[A] = Max(x.getMax max y.getMax) def compare(x: Max[A], y: Max[A]): Int = x.getMax compare y.getMax } implicit def showInstance[A](implicit ev: Show[A]): Show[Max[A]] = Show.show(a => s"Max(${ev.show(a.getMax)})" ) } trait MaxInstances0{ implicit def maxMonoid[A](implicit A: MinBounded[A]): CommutativeMonoid[Max[A]] = new CommutativeMonoid[Max[A]]{ def empty: Max[A] = Max(A.minValue) def combine(x: Max[A], y: Max[A]): Max[A] = Max(x.getMax max y.getMax) } implicit val traverseInstance: Traverse[Max] = new Traverse[Max] { def traverse[G[_], A, B](fa: Max[A])(f: A => G[B])(implicit ev: Applicative[G]): G[Max[B]] = f(fa.getMax).map(Max(_)) def foldLeft[A, B](fa: Max[A], b: B)(f: (B, A) => B): B = f(b, fa.getMax) def foldRight[A, B](fa: Max[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = f(fa.getMax, lb) } implicit val distributiveInstance: Distributive[Max] = new Distributive[Max] { def distribute[G[_], A, B](ga: G[A])(f: A => Max[B])(implicit ev: Functor[G]): Max[G[B]] = Max(ga.map(f(_).getMax)) def map[A, B](fa: Max[A])(f: A => B): Max[B] = Max(f(fa.getMax)) } }
Example 40
Source File: FunctionsAndMethods.scala From scala-tutorials with MIT License | 5 votes |
package com.baeldung.scala.functionsandmethods import scala.annotation.tailrec object FunctionsAndMethods { // Anonymous function execution def anonymousFunctionUsage(anonymous: (Int) => Int): Int = { anonymous(10) } // Anonymous function execution def anonymousFunctionUsageWithApply(anonymous: (Int) => Int): Int = { anonymous.apply(10) } // Method with one parameter def inc(number: Int): Int = number + 1 // Method with parameter by-value def byValue(num: Int): (Int, Int) = (num, num) // Method with parameter by-name, take a look on => sign before Type of parameter // That is how we define a by-name parameters def byName(num: => Int): (Int, Int) = (num, num) // Here we defined a Value Class with extension method implicit class IntExtension(val value: Int) extends AnyVal { def isOdd: Boolean = value % 2 == 0 } def plot(f: Double => Double): List[Double] = { val xs: Range = -10 to 10 xs.map(x => f(x)).toList } val lines: (Double, Double, Double) => Double = (a,b,x) => a * x + b val line: (Double, Double) => Double => Double = (a,b) => x => lines(a,b,x) def factorial(num: Int): Int = { @tailrec def fact(num: Int, acc: Int): Int = { if (num == 0) acc else fact(num - 1, acc * num) } fact(num, 1) } def pop[T](seq: Seq[T]): T = seq.head }
Example 41
Source File: StringLength.scala From scala-tutorials with MIT License | 5 votes |
package com.baeldung.scala.tailrec import scala.annotation.tailrec object StringLength { def recursiveLength(list: List[String]): Long = list match { case Nil => 0 case head :: tail => 1 + recursiveLength(tail) } def recursiveLengthVerbose(list: List[String]): Long = list match { case Nil => 0 case head :: tail => { val accumulator = recursiveLengthVerbose(tail) 1 + accumulator } } @tailrec def tailRecursiveLength(list: List[String], accumulator: Long): Long = list match { case Nil => accumulator case head :: tail => tailRecursiveLength(tail, accumulator + 1) } }
Example 42
Source File: InsertRDD.scala From spark-vector with Apache License 2.0 | 5 votes |
package com.actian.spark_vector.datastream.writer import scala.annotation.tailrec import scala.reflect.ClassTag import org.apache.spark.{ OneToOneDependency, NarrowDependency, Partition, TaskContext } import org.apache.spark.rdd.RDD import com.actian.spark_vector.datastream.{ DataStreamPartition, DataStreamPartitionAssignment, VectorEndpointConf } private val endPointsToParentPartitionsMap = { val affinities = rdd.partitions.map(getPreferredLocationsRec(rdd, _)) val ret = DataStreamPartitionAssignment(affinities, writeConf.vectorEndpoints) logDebug(s"Computed endPointsToParentPartitionsMap and got: ${ (0 until ret.length).map { case idx => val vals = ret(idx) s"Datastream $idx -> RDD partitions ${vals.length}: ${vals.take(partitionsPerDataStreamToPrint).mkString(",")} ${if (vals.length > partitionsPerDataStreamToPrint) "..." else ""}" } }") ret.map(_.map(rdd.partitions(_).index)) } override protected def getPartitions = (0 until writeConf.size).map(idx => DataStreamPartition(idx, rdd, endPointsToParentPartitionsMap(idx))).toArray override protected def getPreferredLocations(split: Partition) = { logDebug(s"getPreferredLocations is called for partition ${split.index} and we are returning ${writeConf.vectorEndpoints(split.index).host}") Seq(writeConf.vectorEndpoints(split.index).host) } override def compute(split: Partition, taskContext: TaskContext): Iterator[R] = split.asInstanceOf[DataStreamPartition].parents.toIterator.flatMap(firstParent[R].iterator(_, taskContext)) override def getDependencies: Seq[NarrowDependency[_]] = Seq(new NarrowDependency(rdd) { def getParents(partitionId: Int) = endPointsToParentPartitionsMap(partitionId) }) }
Example 43
Source File: VectorSRPClient.scala From spark-vector with Apache License 2.0 | 5 votes |
package com.actian.spark_vector.srp import java.nio.channels.SocketChannel import scala.BigInt import scala.annotation.tailrec import com.actian.spark_vector.datastream.writer.DataStreamWriter import com.actian.spark_vector.datastream.reader.DataStreamReader import com.actian.spark_vector.util.Logging import com.actian.spark_vector.vector.ErrorCodes._ import com.actian.spark_vector.vector.VectorException def authenticate(username: String, password: String)(implicit socket: SocketChannel): Unit = { val a = super.a val A = super.A(a) writeWithByteBuffer { out => writeCode(out, authCode) writeString(out, username) writeByteArray(out, A) } val (s, b): (Array[Byte], Array[Byte]) = readWithByteBuffer[(Array[Byte], Array[Byte])]() { in => if (!readCode(in, sBCode)) { throw new VectorException(AuthError, "Authentication failed: unable to read Ok code after exchanging username and A") } (BigInt(readString(in), 16), readByteArray(in)) } val B = b val u = super.u(A, B) val x = super.x(s, username, password) val S = super.S(x, B, a, u) val K = H(S) val clientM = M(username, s, A, B, K) writeWithByteBuffer { out => writeCode(out, MCode) writeByteArray(out, clientM) } val serverM = readWithByteBuffer[Array[Byte]]() { in => if (!readCode(in, serverMCode)) { throw new VectorException(AuthError, "Authentication failed: unable to read code before verification of server M key") } readByteArray(in) } if (!H(A ++ clientM ++ K).sameElements(serverM)) { throw new VectorException(AuthError, "Authentication failed: M and serverM differ") } } } // scalastyle:on magic.number
Example 44
Source File: ScalastyleRepository.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.ncredinburgh.sonar.scalastyle import org.sonar.api.rule.Severity import org.sonar.api.server.rule.RulesDefinition import org.sonar.api.server.rule.RuleParamType import org.slf4j.LoggerFactory import org.sonar.api.server.rule.RulesDefinition.NewRepository import com.ncredinburgh.sonar.scalastyle.ScalastyleRepository.getStandardKey import scala.annotation.tailrec object ScalastyleRepository { def getStandardKey(clazz: String) = { val simpleClazz = clazz.reverse.takeWhile(_ != '.').reverse s"scalastyle_${simpleClazz}" } } private def determineFreeRuleKey(clazz: String, repo: NewRepository): String = { @tailrec def getFreeRuleKey(key: String, count: Int, repo: NewRepository): String = { val testee = if (count == 0) key else "$key_$count" if (repo.rule(testee) == null) { testee } else { getFreeRuleKey(key, (count + 1), repo) } } getFreeRuleKey(getStandardKey(clazz), 0, repo) } }
Example 45
Source File: Measures.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.sagacify.sonar.scala import scala.annotation.tailrec import scalariform.lexer.ScalaLexer import scalariform.lexer.Token import scalariform.lexer.Tokens.LINE_COMMENT import scalariform.lexer.Tokens.MULTILINE_COMMENT import scalariform.lexer.Tokens.XML_COMMENT import scalariform.lexer.Tokens.WS import scalariform.lexer.Tokens.EOF object Measures { @tailrec final def count_comment_lines(tokens: List[Token], i: Int = 0): Int = { tokens match { case Nil => i case token :: tail if token.tokenType.isComment => { token.tokenType match { case LINE_COMMENT => count_comment_lines(tail, i + 1) case MULTILINE_COMMENT => count_comment_lines(tail, i + token.rawText.count(_ == '\n') + 1) case XML_COMMENT => new scala.NotImplementedError("XML ?!"); i } } case _ :: tail => count_comment_lines(tail, i) } } @tailrec final def count_ncloc(tokens: List[Token], i: Int = 0): Int = { @tailrec def get_next_line(tokens: List[Token]): List[Token] = { tokens match { case Nil => Nil case token :: tail if token.tokenType == WS && token.text.contains('\n') => tail case token :: tail if token.tokenType == LINE_COMMENT => tail case token :: tail => get_next_line(tail) } } tokens match { case Nil => i case token :: tail if token.tokenType == WS => count_ncloc(tail, i) case token :: tail if token.tokenType == EOF => i case token :: tail => if( !token.tokenType.isNewline & !token.tokenType.isComment) { count_ncloc(get_next_line(tail), i + 1) } else { count_ncloc(tail, i) } } } }
Example 46
Source File: Batcher.scala From gfc-concurrent with Apache License 2.0 | 5 votes |
package com.gilt.gfc.concurrent import java.util.concurrent.{Executors, ScheduledExecutorService => JScheduledExecutorService} import java.util.concurrent.atomic.AtomicReference import com.gilt.gfc.logging.Loggable import scala.annotation.tailrec import scala.concurrent.duration._ import scala.util.control.NonFatal @tailrec override def flush(): Unit = { val b@(_, records) = currentBatch.get() if (currentBatch.compareAndSet(b, emptyBatch)) { safeSubmitBatch(records) } else { flush() // retry } } override def shutdown(): Unit = { isRunning = false task.cancel(true) flush() } private[this] def safeSubmitBatch(records: Vector[R]): Unit = { if (!records.isEmpty) { lastSubmit = System.currentTimeMillis() try { submitBatch(records) } catch { case NonFatal(e) => error(s"Failed to flush ${name} batch: ${e.getMessage}", e) } } } }
Example 47
Source File: LawlessTraversals.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.data import scala.collection.IterableLike import scala.collection.generic.CanBuildFrom import scala.annotation.tailrec private[daml] object LawlessTraversals { implicit final class `Lawless iterable traversal`[A, This](private val seq: IterableLike[A, This]) extends AnyVal { def traverseEitherStrictly[E, B, That](f: A => Either[E, B])( implicit cbf: CanBuildFrom[This, B, That]): Either[E, That] = { val that = cbf(seq.repr) that.sizeHint(seq) val i = seq.iterator @tailrec def lp(): Either[E, That] = if (i.hasNext) f(i.next) match { case Left(b) => Left(b) case Right(c) => that += c lp() } else Right(that.result) lp() } } }
Example 48
Source File: Blinding.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.engine import com.daml.lf.data._ import com.daml.lf.data.Ref.Party import com.daml.lf.transaction.Node.{NodeCreate, NodeExercises, NodeFetch, NodeLookupByKey} import com.daml.lf.transaction.{BlindingInfo, GenTransaction, Transaction} import com.daml.lf.ledger._ import com.daml.lf.data.Relation.Relation import scala.annotation.tailrec object Blinding { private[this] def maybeAuthorizeAndBlind( tx: Transaction.Transaction, authorization: Authorization): Either[AuthorizationError, BlindingInfo] = { val enrichedTx = EnrichedTransaction(authorization, tx) def authorizationErrors(failures: Map[Transaction.NodeId, FailedAuthorization]) = { failures .map { case (id, failure) => failure match { case nc: FailedAuthorization.NoControllers => s"node $id (${nc.templateId}) has no controllers" case am: FailedAuthorization.ActorMismatch => s"node $id (${am.templateId}) controllers don't match given actors ${am.givenActors.mkString(",")}" case ma: FailedAuthorization.CreateMissingAuthorization => s"node $id (${ma.templateId}) requires authorizers ${ma.requiredParties .mkString(",")}, but only ${ma.authorizingParties.mkString(",")} were given" case ma: FailedAuthorization.FetchMissingAuthorization => s"node $id requires one of the stakeholders ${ma.stakeholders} of the fetched contract to be an authorizer, but authorizers were ${ma.authorizingParties}" case ma: FailedAuthorization.ExerciseMissingAuthorization => s"node $id (${ma.templateId}) requires authorizers ${ma.requiredParties .mkString(",")}, but only ${ma.authorizingParties.mkString(",")} were given" case ns: FailedAuthorization.NoSignatories => s"node $id (${ns.templateId}) has no signatories" case nlbk: FailedAuthorization.LookupByKeyMissingAuthorization => s"node $id (${nlbk.templateId}) requires authorizers ${nlbk.maintainers} for lookup by key, but it only has ${nlbk.authorizingParties}" case mns: FailedAuthorization.MaintainersNotSubsetOfSignatories => s"node $id (${mns.templateId}) has maintainers ${mns.maintainers} which are not a subset of the signatories ${mns.signatories}" } } .mkString(";") } if (enrichedTx.failedAuthorizations.isEmpty) { Right( BlindingInfo( disclosure = enrichedTx.explicitDisclosure, globalDivulgence = enrichedTx.globalImplicitDisclosure, )) } else { Left( AuthorizationError( s"The following errors occured: ${authorizationErrors(enrichedTx.failedAuthorizations)}")) } } def divulgedTransaction[Nid, Cid, Val]( divulgences: Relation[Nid, Party], party: Party, tx: GenTransaction[Nid, Cid, Val]): GenTransaction[Nid, Cid, Val] = { val partyDivulgences = Relation.invert(divulgences)(party) // Note that this relies on the local divulgence to be well-formed: // if an exercise node is divulged to A but some of its descendants // aren't the resulting transaction will not be well formed. val filteredNodes = tx.nodes.filter { case (k, _) => partyDivulgences.contains(k) } @tailrec def go(filteredRoots: BackStack[Nid], remainingRoots: FrontStack[Nid]): ImmArray[Nid] = { remainingRoots match { case FrontStack() => filteredRoots.toImmArray case FrontStackCons(root, remainingRoots) => if (partyDivulgences.contains(root)) { go(filteredRoots :+ root, remainingRoots) } else { tx.nodes(root) match { case _: NodeFetch[Cid, Val] | _: NodeCreate[Cid, Val] | _: NodeLookupByKey[Cid, Val] => go(filteredRoots, remainingRoots) case ne: NodeExercises[Nid, Cid, Val] => go(filteredRoots, ne.children ++: remainingRoots) } } } } GenTransaction( roots = go(BackStack.empty, FrontStack(tx.roots)), nodes = filteredNodes ) } }
Example 49
Source File: InMemoryPrivateLedgerData.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.engine import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger import com.daml.lf.data.{FrontStack, FrontStackCons} import com.daml.lf.transaction.Node._ import com.daml.lf.transaction.{Transaction => Tx} import com.daml.lf.value.Value._ import scala.annotation.tailrec trait PrivateLedgerData { def update(tx: Tx.Transaction): Unit def get(id: ContractId): Option[ContractInst[VersionedValue[ContractId]]] def transactionCounter: Int def clear(): Unit } private[engine] class InMemoryPrivateLedgerData extends PrivateLedgerData { private val pcs: ConcurrentHashMap[ContractId, ContractInst[Tx.Value[ContractId]]] = new ConcurrentHashMap() private val txCounter: AtomicInteger = new AtomicInteger(0) def update(tx: Tx.Transaction): Unit = updateWithContractId(tx) def updateWithContractId(tx: Tx.Transaction): Unit = this.synchronized { // traverse in topo order and add / remove @tailrec def go(remaining: FrontStack[Tx.NodeId]): Unit = remaining match { case FrontStack() => () case FrontStackCons(nodeId, nodeIds) => val node = tx.nodes(nodeId) node match { case nc: NodeCreate.WithTxValue[ContractId] => pcs.put(nc.coid, nc.coinst) go(nodeIds) case ne: NodeExercises.WithTxValue[Tx.NodeId, ContractId] => go(ne.children ++: nodeIds) case _: NodeLookupByKey[_, _] | _: NodeFetch[_, _] => go(nodeIds) } } go(FrontStack(tx.roots)) txCounter.incrementAndGet() () } def get(id: ContractId): Option[ContractInst[VersionedValue[ContractId]]] = this.synchronized { Option(pcs.get(id)) } def clear(): Unit = this.synchronized { pcs.clear() } def transactionCounter: Int = txCounter.intValue() override def toString: String = s"InMemoryPrivateContractStore@{txCounter: $txCounter, pcs: $pcs}" } private[engine] object InMemoryPrivateLedgerData { def apply(): PrivateLedgerData = new InMemoryPrivateLedgerData() }
Example 50
Source File: SomeArrayEquals.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.speedy import scala.annotation.tailrec import scala.util.hashing.MurmurHash3._ trait SomeArrayEquals extends Product with Serializable { override final def equals(o: Any): Boolean = o match { case oo: AnyRef if oo eq this => true case oo: SomeArrayEquals if (this canEqual oo) && (oo canEqual this) && productArity == oo.productArity => val arr = productArity @SuppressWarnings(Array("org.wartremover.warts.Any")) @tailrec def lp(i: Int): Boolean = if (i >= arr) true else (productElement(i) match { case a: Array[_] => oo.productElement(i) match { case b: Array[_] => (a eq b) || (a sameElements b) case _ => false } case a => a == oo.productElement(i) }) && lp(i + 1) lp(0) case _ => false } override final def hashCode(): Int = { val arr = productArity if (arr == 0) productPrefix.## else { @tailrec def lp(i: Int, seed: Int): Int = if (i >= arr) seed else lp(i + 1, mix(seed, productElement(i) match { case a: Array[_] => arrayHash(a) case a => a.## })) finalizeHash(lp(0, productSeed), arr) } } } object SomeArrayEquals { private[speedy] final case class ComparableArray(a: Array[_]) { @SuppressWarnings(Array("org.wartremover.warts.Any")) override def equals(o: Any): Boolean = o match { case oo: AnyRef if oo eq this => true case oo: ComparableArray if oo canEqual this => val b = oo.a (a eq b) || { (a ne null) && (b ne null) && { a sameElements b } } case _ => false } override def hashCode(): Int = { if (a eq null) null.## else arrayHash(a) } } }
Example 51
Source File: Equality.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.speedy.svalue import com.daml.lf.data.FrontStack import com.daml.lf.speedy.SValue import com.daml.lf.speedy.SValue._ import scala.annotation.tailrec import scala.collection.JavaConverters._ private[lf] object Equality { // Equality between two SValues of same type. // Note it is not reflexive, in other words there is some value `v` // such that `areEqual(v, v)` returns `False`). // This follows the equality defined in the daml-lf spec. def areEqual(x: SValue, y: SValue): Boolean = equality(FrontStack((x, y))) private[this] def zipAndPush[X, Y]( h1: Iterator[X], h2: Iterator[Y], stack: FrontStack[(X, Y)], ): FrontStack[(X, Y)] = ((h1 zip h2) foldLeft stack)(_.+:(_)) @tailrec private[this] def equality(stack0: FrontStack[(SValue, SValue)]): Boolean = { stack0.pop match { case Some((tuple, stack)) => tuple match { case (x: SPrimLit, y: SPrimLit) => x == y && equality(stack) case (SEnum(tyCon1, _, rank1), SEnum(tyCon2, _, rank2)) => tyCon1 == tyCon2 && rank1 == rank2 && equality(stack) case (SRecord(tyCon1, fields1, args1), SRecord(tyCon2, fields2, args2)) => tyCon1 == tyCon2 && (fields1 sameElements fields2) && equality(zipAndPush(args1.iterator().asScala, args2.iterator().asScala, stack)) case (SVariant(tyCon1, _, rank1, arg1), SVariant(tyCon2, _, rank2, arg2)) => tyCon1 == tyCon2 && rank1 == rank2 && equality((arg1, arg2) +: stack) case (SList(lst1), SList(lst2)) => lst1.length == lst2.length && equality(zipAndPush(lst1.iterator, lst2.iterator, stack)) case (SOptional(None), SOptional(None)) => equality(stack) case (SOptional(Some(v1)), SOptional(Some(v2))) => equality((v1, v2) +: stack) case (STextMap(map1), STextMap(map2)) => map1.keySet == map2.keySet && { val keys = map1.keys equality(zipAndPush(keys.iterator.map(map1), keys.iterator.map(map2), stack)) } case (SGenMap(map1), SGenMap(map2)) => map1.keySet == map2.keySet && { val keys = map1.keys equality(zipAndPush(keys.iterator.map(map1), keys.iterator.map(map2), stack)) } case (SStruct(fields1, args1), SStruct(fields2, args2)) => (fields1 sameElements fields2) && equality( zipAndPush(args1.iterator().asScala, args2.iterator().asScala, stack), ) case (SAny(t1, v1), SAny(t2, v2)) => t1 == t2 && equality((v1, v2) +: stack) case (STypeRep(t1), STypeRep(t2)) => t1 == t2 && equality(stack) case _ => false } case _ => true } } }
Example 52
Source File: SortingPager.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator.query import com.daml.navigator.dotnot.{OnTreeReady, PropertyCursor} import com.daml.navigator.model._ import com.daml.navigator.query.project._ import com.typesafe.scalalogging.LazyLogging import scala.annotation.tailrec sealed abstract class SortingPager[N <: Node[_]]( criteria: List[SortCriterion], project: OnTreeReady[N, ProjectValue, DamlLfTypeLookup], ps: DamlLfTypeLookup ) extends PagerDecorator[N] with LazyLogging { final override def decorate(page: Page[N], ledger: Ledger): Page[N] = page.copy(rows = sort(page.rows), sortedLike = criteria) def sort(rows: Seq[N]): Seq[N] = rows.sortBy(projectAll)(ordering) private val ordering = new Ordering[List[Option[ProjectValue]]] { val optionProjectValueOrdering = Ordering[Option[ProjectValue]] override def compare(l1: List[Option[ProjectValue]], l2: List[Option[ProjectValue]]): Int = { @tailrec def loop( l1: List[Option[ProjectValue]], l2: List[Option[ProjectValue]], criteria: List[SortCriterion]): Int = { l1 match { case Nil => 0 case x1 :: xs1 => val x2 :: xs2 = l2 val c :: cs = criteria optionProjectValueOrdering.compare(x1, x2) match { case 0 => loop(xs1, xs2, cs) case x => c.direction match { case SortDirection.DESCENDING => x * (-1) case SortDirection.ASCENDING => x } } } } loop(l1, l2, criteria) } } private def projectAll(node: N): List[Option[ProjectValue]] = criteria.map(project(node)) private def project(node: N)(criterion: SortCriterion): Option[ProjectValue] = { val cursor = PropertyCursor.fromString(criterion.field) project.run(node, cursor, "", ps) match { case Left(failure) => logger.error(s"Cannot project $node with criterion $criterion: $failure. Using None.") None case Right(value) => Some(value) } } } final class ContractSorter( val criteria: List[SortCriterion], ps: DamlLfTypeLookup, val delegate: Pager[Contract]) extends SortingPager[Contract](criteria, contractProject, ps) final class TemplateSorter( val criteria: List[SortCriterion], ps: DamlLfTypeLookup, val delegate: Pager[Template]) extends SortingPager[Template](criteria, templateProject, ps) final class CommandSorter( val criteria: List[SortCriterion], ps: DamlLfTypeLookup, val delegate: Pager[Command]) extends SortingPager[Command](criteria, commandProject, ps)
Example 53
Source File: ResettableResourceOwner.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.resources import java.util.concurrent.atomic.AtomicReference import com.daml.resources.ResettableResourceOwner._ import scala.annotation.tailrec import scala.concurrent.{ExecutionContext, Future, Promise} class ResettableResourceOwner[A, ResetValue] private ( initialValue: ResetValue, owner: Reset => ResetValue => ResourceOwner[A], resetOperation: A => Future[ResetValue], ) extends ResourceOwner[A] { override def acquire()(implicit executionContext: ExecutionContext): Resource[A] = new Resource[A] { private val resettableOwner: ResetValue => ResourceOwner[A] = owner(reset _) @volatile private var resource = resettableOwner(initialValue).acquire() private val resetPromise = new AtomicReference[Option[Promise[Unit]]](None) override def asFuture: Future[A] = resetPromise.get().getOrElse(Promise.successful(())).future.flatMap(_ => resource.asFuture) override def release(): Future[Unit] = resetPromise.get().getOrElse(Promise.successful(())).future.flatMap(_ => resource.release()) @tailrec private def reset(): Future[Unit] = { val currentResetPromise = resetPromise.get() currentResetPromise match { case None => val newResetPromise = Some(Promise[Unit]()) if (resetPromise.compareAndSet(None, newResetPromise)) { for { value <- resource.asFuture _ <- resource.release() resetValue <- resetOperation(value) } yield { resource = resettableOwner(resetValue).acquire() newResetPromise.get.success(()) resetPromise.set(None) } } else { reset() } case Some(currentResetPromise) => currentResetPromise.future } } } } object ResettableResourceOwner { type Reset = () => Future[Unit] def apply[A](owner: Reset => ResourceOwner[A]) = new ResettableResourceOwner[A, Unit]( initialValue = (), reset => _ => owner(reset), resetOperation = _ => Future.unit, ) def apply[A, ResetValue]( initialValue: ResetValue, owner: Reset => ResetValue => ResourceOwner[A], resetOperation: A => Future[ResetValue], ) = new ResettableResourceOwner(initialValue, owner, resetOperation) }
Example 54
Source File: FreePort.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.testing.postgresql import java.net.{InetAddress, ServerSocket} import com.daml.ports.Port import scala.annotation.tailrec private[postgresql] object FreePort { @tailrec def find(tries: Int = 10): PortLock.Locked = { val socket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress) val portLock = try { val port = Port(socket.getLocalPort) PortLock.lock(port) } finally { socket.close() } portLock match { case Right(locked) => socket.close() locked case Left(failure) => socket.close() if (tries <= 1) { throw failure } else { find(tries - 1) } } } }
Example 55
Source File: GraphGen.scala From iotchain with MIT License | 5 votes |
package jbok.core import scalax.collection.GraphEdge.{DiEdge, UnDiEdge} import scalax.collection.GraphPredef._ import scalax.collection._ import scalax.collection.generator.RandomGraph.Metrics import scalax.collection.generator._ import scalax.collection.mutable.{Graph => MGraph} import scala.annotation.tailrec import scala.reflect.ClassTag object GraphGen { def metrics[N: ClassTag](n: Int, dr: Range, gen: () => N): Metrics[N] = new Metrics[N] { override def nodeGen = gen() override def order = n override def nodeDegrees = NodeDegreeRange(dr.start, dr.end) override def connected = true } def randomUnDiGraph[N: ClassTag](n: Int, dr: Range, gen: () => N): RandomGraph[N, UnDiEdge, Graph] = RandomGraph[N, UnDiEdge, Graph](Graph, metrics[N](n, dr, gen), Set(UnDiEdge)) def randomDiGraph[N: ClassTag](n: Int, dr: Range, gen: () => N): RandomGraph[N, DiEdge, Graph] = RandomGraph[N, DiEdge, Graph](Graph, metrics[N](n, dr, gen), Set(DiEdge)) @tailrec def ring[N: ClassTag](nodes: List[N], start: N, g: MGraph[N, DiEdge] = MGraph.empty[N, DiEdge]): Graph[N, DiEdge] = nodes match { case h1 :: h2 :: tail => g += h1 ~> h2 ring[N](h2 :: tail, start, g) case head :: Nil => g += head ~> start case _ => g } }
Example 56
Source File: Program.scala From iotchain with MIT License | 5 votes |
package jbok.evm import scodec.bits.ByteVector import scala.annotation.tailrec @tailrec private def validJumpDestinationsAfterPosition(pos: Int, accum: Set[Int] = Set.empty): Set[Int] = if (pos < 0 || pos >= length) accum else { val byte = code(pos) val opCode = frontierConfig.byteToOpCode.get(byte) // we only need to check PushOp and JUMPDEST, they are both present in Frontier opCode match { case Some(pushOp: PushOp) => validJumpDestinationsAfterPosition(pos + pushOp.i + 2, accum) case Some(JUMPDEST) => validJumpDestinationsAfterPosition(pos + 1, accum + pos) case _ => validJumpDestinationsAfterPosition(pos + 1, accum) } } }
Example 57
Source File: RetCalc.scala From Scala-Programming-Projects with MIT License | 5 votes |
package retcalc import retcalc.RetCalcError.MoreExpensesThanIncome import scala.annotation.tailrec case class RetCalcParams(nbOfMonthsInRetirement: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double) case class MultiSimResults(successCount: Int, simCount: Int, minCapitalAfterDeath: Double, maxCapitalAfterDeath: Double) { def successProbability: Double = successCount.toDouble / simCount } object RetCalc { def simulatePlan(returns: Returns, params: RetCalcParams, nbOfMonthsSavings: Int, monthOffset: Int = 0): Either[RetCalcError, (Double, Double)] = { import params._ for { capitalAtRetirement <- futureCapital( returns = OffsetReturns(returns, monthOffset), nbOfMonths = nbOfMonthsSavings, netIncome = netIncome, currentExpenses = currentExpenses, initialCapital = initialCapital) capitalAfterDeath <- futureCapital( returns = OffsetReturns(returns, monthOffset + nbOfMonthsSavings), nbOfMonths = nbOfMonthsInRetirement, netIncome = 0, currentExpenses = currentExpenses, initialCapital = capitalAtRetirement) } yield (capitalAtRetirement, capitalAfterDeath) } def nbOfMonthsSaving(params: RetCalcParams, returns: Returns): Either[RetCalcError, Int] = { import params._ @tailrec def loop(months: Int): Either[RetCalcError, Int] = { simulatePlan(returns, params, months) match { case Right((capitalAtRetirement, capitalAfterDeath)) => if (capitalAfterDeath > 0.0) Right(months) else loop(months + 1) case Left(err) => Left(err) } } if (netIncome > currentExpenses) loop(0) else Left(MoreExpensesThanIncome(netIncome, currentExpenses)) } def futureCapital(returns: Returns, nbOfMonths: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double): Either[RetCalcError, Double] = { val monthlySavings = netIncome - currentExpenses (0 until nbOfMonths).foldLeft[Either[RetCalcError, Double]](Right(initialCapital)) { case (accumulated, month) => for { acc <- accumulated monthlyRate <- Returns.monthlyRate(returns, month) } yield acc * (1 + monthlyRate) + monthlySavings } } def multiSim(params: RetCalcParams, nbOfMonthsSavings: Int, variableReturns: VariableReturns): MultiSimResults = { variableReturns.returns.indices.foldLeft(MultiSimResults(0, 0, Double.PositiveInfinity, Double.NegativeInfinity)) { case (acc, i) => simulatePlan(variableReturns, params, nbOfMonthsSavings, i) match { case Right((capitalAtRetirement, capitalAfterDeath)) => MultiSimResults( successCount = if (capitalAfterDeath > 0) acc.successCount + 1 else acc.successCount, simCount = i + 1, minCapitalAfterDeath = if (capitalAfterDeath < acc.minCapitalAfterDeath) capitalAfterDeath else acc.minCapitalAfterDeath, maxCapitalAfterDeath = if (capitalAfterDeath > acc.maxCapitalAfterDeath) capitalAfterDeath else acc.maxCapitalAfterDeath) // Could have a more clever rule which would reduce params.nbOfMonthsInRetirement. // say If the capital after nbOfMonthsInRetirement/2 is > 2*capitalAtRetirement, // it is very likely that we can count it as a success, even if we cannot run // the simulation until the end case Left(err) => acc } } } }
Example 58
Source File: RetCalc.scala From Scala-Programming-Projects with MIT License | 5 votes |
package retcalc import scala.annotation.tailrec case class RetCalcParams(nbOfMonthsInRetirement: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double) object RetCalc { def simulatePlan(returns: Returns, params: RetCalcParams, nbOfMonthsSavings: Int, monthOffset: Int = 0): (Double, Double) = { import params._ val capitalAtRetirement = futureCapital( returns = OffsetReturns(returns, monthOffset), nbOfMonths = nbOfMonthsSavings, netIncome = netIncome, currentExpenses = currentExpenses, initialCapital = initialCapital) val capitalAfterDeath = futureCapital( returns = OffsetReturns(returns, monthOffset + nbOfMonthsSavings), nbOfMonths = nbOfMonthsInRetirement, netIncome = 0, currentExpenses = currentExpenses, initialCapital = capitalAtRetirement) (capitalAtRetirement, capitalAfterDeath) } def nbOfMonthsSaving(params: RetCalcParams, returns: Returns): Int = { import params._ @tailrec def loop(months: Int): Int = { val (capitalAtRetirement, capitalAfterDeath) = simulatePlan(returns, params, months) if (capitalAfterDeath > 0.0) months else loop(months + 1) } if (netIncome > currentExpenses) loop(0) else Int.MaxValue } def futureCapital(returns: Returns, nbOfMonths: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double): Double = { val monthlySavings = netIncome - currentExpenses (0 until nbOfMonths).foldLeft(initialCapital) { case (accumulated, month) => accumulated * (1 + Returns.monthlyRate(returns, month)) + monthlySavings } } }
Example 59
Source File: package.scala From prometheus-akka with Apache License 2.0 | 5 votes |
package com.workday.prometheus import scala.annotation.tailrec import io.prometheus.client.Collector package object akka { def metricFriendlyActorName(actorPath: String) = { Collector.sanitizeMetricName(trimLeadingSlashes(actorPath).toLowerCase.replace("/", "_")) } @tailrec private def trimLeadingSlashes(s: String): String = { if (s.startsWith("/")) trimLeadingSlashes(s.substring(1)) else s } type ForkJoinPoolLike = { def getParallelism: Int def getPoolSize: Int def getActiveThreadCount: Int def getRunningThreadCount: Int def getQueuedSubmissionCount: Int def getQueuedTaskCount: Long def getStealCount: Long } }
Example 60
Source File: catsContrib.scala From spark-tools with Apache License 2.0 | 5 votes |
package io.univalence import cats.Eq import cats.Monad import io.univalence.centrifuge.Result import scala.annotation.tailrec object CatsContrib { implicit val resultMonad: Monad[Result] = new Monad[Result] { override def pure[A](x: A): Result[A] = Result.pure(x) @tailrec override def tailRecM[A, B](a: A)(f: (A) => Result[Either[A, B]]): Result[B] = f(a) match { case r if r.isEmpty => r.asInstanceOf[Result[B]] case r @ Result(None, xs) => r.asInstanceOf[Result[B]] case Result(Some(Left(a2)), xs) => tailRecM(a2)(f.andThen(x => x.prependAnnotations(xs))) case r @ Result(Some(Right(b)), xs) => Result(Some(b), xs) } override def flatMap[A, B](fa: Result[A])(f: (A) => Result[B]): Result[B] = fa.flatMap(f) } implicit def equResul[T]: Eq[Result[T]] = Eq.fromUniversalEquals }
Example 61
Source File: MergeSort.scala From chymyst-core with Apache License 2.0 | 5 votes |
package io.chymyst.benchmark // Make all imports explicit, just to see what is the entire set of required imports. // Do not optimize imports in this file! import io.chymyst.jc.{+, FixedPool, M, m, B, b, go, Reaction, ReactionInfo, InputMoleculeInfo, AllMatchersAreTrivial, OutputMoleculeInfo, site, EmitMultiple} import io.chymyst.jc.ConsoleErrorsAndWarningsReporter import scala.annotation.tailrec import scala.collection.mutable object MergeSort { type Coll[T] = IndexedSeq[T] def arrayMerge[T: Ordering](arr1: Coll[T], arr2: Coll[T]): Coll[T] = { val result = new mutable.ArraySeq[T](arr1.length + arr2.length) // just to allocate space def isLess(x: T, y: T) = implicitly[Ordering[T]].compare(x, y) < 0 // Will now modify the `result` array in place. @tailrec def mergeRec(i1: Int, i2: Int, i: Int): Unit = { if (i1 == arr1.length && i2 == arr2.length) () else { val (x, newI1, newI2) = if (i1 < arr1.length && (i2 == arr2.length || isLess(arr1(i1), arr2(i2)))) (arr1(i1), i1 + 1, i2) else (arr2(i2), i1, i2 + 1) result(i) = x mergeRec(newI1, newI2, i + 1) } } mergeRec(0, 0, 0) result.toIndexedSeq } def performMergeSort[T: Ordering](array: Coll[T], threads: Int = 8): Coll[T] = { val finalResult = m[Coll[T]] val getFinalResult = b[Unit, Coll[T]] val reactionPool = FixedPool(threads) val pool2 = FixedPool(threads) site(pool2)( go { case finalResult(arr) + getFinalResult(_, r) => r(arr) } ) // The `mergesort()` molecule will start the chain reactions at one level lower. val mergesort = m[(Coll[T], M[Coll[T]])] site(reactionPool)( go { case mergesort((arr, resultToYield)) => if (arr.length <= 1) resultToYield(arr) else { val (part1, part2) = arr.splitAt(arr.length / 2) // The `sorted1()` and `sorted2()` molecules will carry the sorted results from the lower level. val sorted1 = m[Coll[T]] val sorted2 = m[Coll[T]] site(reactionPool)( go { case sorted1(x) + sorted2(y) => resultToYield(arrayMerge(x, y)) } ) // emit `mergesort` with the lower-level `sorted` result molecules mergesort((part1, sorted1)) + mergesort((part2, sorted2)) } } ) // Sort our array: emit `mergesort()` at top level. mergesort((array, finalResult)) val result = getFinalResult() reactionPool.shutdownNow() pool2.shutdownNow() result } }
Example 62
Source File: ParallelOrSpec.scala From chymyst-core with Apache License 2.0 | 5 votes |
package io.chymyst.test import io.chymyst.jc._ import scala.annotation.tailrec import scala.concurrent.duration._ import scala.language.postfixOps class ParallelOrSpec extends LogSpec { behavior of "test" def firstResult[T](b1: B[Unit, T], b2: B[Unit, T], tp: Pool): B[Unit, T] = { val get = b[Unit, T] val res = b[Unit, T] val res1 = m[Unit] val res2 = m[Unit] val done = m[T] site(tp)( go { case res1(_) => val x = b1(); done(x) }, // IntelliJ 2016.3 CE insists on `b1(())` here, but scalac is fine with `b1()`. go { case res2(_) => val x = b2(); done(x) } ) site(tp)(go { case get(_, r) + done(x) => r(x) }) site(tp)(go { case res(_, r) => res1() + res2(); val x = get(); r(x) }) res } @tailrec final def neverReturn[T]: T = { Thread.sleep(1000000) neverReturn[T] } it should "implement the First Result operation correctly" in { val never = b[Unit, String] val fast = b[Unit, String] val slow = b[Unit, String] val tp = FixedPool(30) site(tp)( go { case never(_, r) => r(neverReturn[String]) }, go { case fast(_, r) => Thread.sleep(10); r("fast") }, go { case slow(_, r) => Thread.sleep(200); r("slow") } ) firstResult(fast, fast, tp)() shouldEqual "fast" firstResult(fast, slow, tp)() shouldEqual "fast" firstResult(slow, fast, tp)() shouldEqual "fast" firstResult(slow, slow, tp)() shouldEqual "slow" firstResult(fast, never, tp)() shouldEqual "fast" firstResult(never, slow, tp)() shouldEqual "slow" tp.shutdownNow() } }
Example 63
Source File: MesosClusterDispatcherArguments.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.mesos import scala.annotation.tailrec import org.apache.spark.SparkConf import org.apache.spark.util.{IntParam, Utils} private[mesos] class MesosClusterDispatcherArguments(args: Array[String], conf: SparkConf) { var host = Utils.localHostName() var port = 7077 var name = "Spark Cluster" var webUiPort = 8081 var masterUrl: String = _ var zookeeperUrl: Option[String] = None var propertiesFile: String = _ parse(args.toList) propertiesFile = Utils.loadDefaultSparkProperties(conf, propertiesFile) @tailrec private def parse(args: List[String]): Unit = args match { case ("--host" | "-h") :: value :: tail => Utils.checkHost(value, "Please use hostname " + value) host = value parse(tail) case ("--port" | "-p") :: IntParam(value) :: tail => port = value parse(tail) case ("--webui-port") :: IntParam(value) :: tail => webUiPort = value parse(tail) case ("--zk" | "-z") :: value :: tail => zookeeperUrl = Some(value) parse(tail) case ("--master" | "-m") :: value :: tail => if (!value.startsWith("mesos://")) { // scalastyle:off println System.err.println("Cluster dispatcher only supports mesos (uri begins with mesos://)") // scalastyle:on println System.exit(1) } masterUrl = value.stripPrefix("mesos://") parse(tail) case ("--name") :: value :: tail => name = value parse(tail) case ("--properties-file") :: value :: tail => propertiesFile = value parse(tail) case ("--help") :: tail => printUsageAndExit(0) case Nil => if (masterUrl == null) { // scalastyle:off println System.err.println("--master is required") // scalastyle:on println printUsageAndExit(1) } case _ => printUsageAndExit(1) } private def printUsageAndExit(exitCode: Int): Unit = { // scalastyle:off println System.err.println( "Usage: MesosClusterDispatcher [options]\n" + "\n" + "Options:\n" + " -h HOST, --host HOST Hostname to listen on\n" + " -p PORT, --port PORT Port to listen on (default: 7077)\n" + " --webui-port WEBUI_PORT WebUI Port to listen on (default: 8081)\n" + " --name NAME Framework name to show in Mesos UI\n" + " -m --master MASTER URI for connecting to Mesos master\n" + " -z --zk ZOOKEEPER Comma delimited URLs for connecting to \n" + " Zookeeper for persistence\n" + " --properties-file FILE Path to a custom Spark properties file.\n" + " Default is conf/spark-defaults.conf.") // scalastyle:on println System.exit(exitCode) } }
Example 64
Source File: RateLimitedOutputStream.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.OutputStream import java.util.concurrent.TimeUnit._ import scala.annotation.tailrec import org.apache.spark.internal.Logging private[streaming] class RateLimitedOutputStream(out: OutputStream, desiredBytesPerSec: Int) extends OutputStream with Logging { require(desiredBytesPerSec > 0) private val SYNC_INTERVAL = NANOSECONDS.convert(10, SECONDS) private val CHUNK_SIZE = 8192 private var lastSyncTime = System.nanoTime private var bytesWrittenSinceSync = 0L override def write(b: Int) { waitToWrite(1) out.write(b) } override def write(bytes: Array[Byte]) { write(bytes, 0, bytes.length) } @tailrec override final def write(bytes: Array[Byte], offset: Int, length: Int) { val writeSize = math.min(length - offset, CHUNK_SIZE) if (writeSize > 0) { waitToWrite(writeSize) out.write(bytes, offset, writeSize) write(bytes, offset + writeSize, length) } } override def flush() { out.flush() } override def close() { out.close() } @tailrec private def waitToWrite(numBytes: Int) { val now = System.nanoTime val elapsedNanosecs = math.max(now - lastSyncTime, 1) val rate = bytesWrittenSinceSync.toDouble * 1000000000 / elapsedNanosecs if (rate < desiredBytesPerSec) { // It's okay to write; just update some variables and return bytesWrittenSinceSync += numBytes if (now > lastSyncTime + SYNC_INTERVAL) { // Sync interval has passed; let's resync lastSyncTime = now bytesWrittenSinceSync = numBytes } } else { // Calculate how much time we should sleep to bring ourselves to the desired rate. val targetTimeInMillis = bytesWrittenSinceSync * 1000 / desiredBytesPerSec val elapsedTimeInMillis = elapsedNanosecs / 1000000 val sleepTimeInMillis = targetTimeInMillis - elapsedTimeInMillis if (sleepTimeInMillis > 0) { logTrace("Natural rate is " + rate + " per second but desired rate is " + desiredBytesPerSec + ", sleeping for " + sleepTimeInMillis + " ms to compensate.") Thread.sleep(sleepTimeInMillis) } waitToWrite(numBytes) } } }
Example 65
Source File: MasterArguments.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.master import scala.annotation.tailrec import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.util.{IntParam, Utils} private def printUsageAndExit(exitCode: Int) { // scalastyle:off println System.err.println( "Usage: Master [options]\n" + "\n" + "Options:\n" + " -i HOST, --ip HOST Hostname to listen on (deprecated, please use --host or -h) \n" + " -h HOST, --host HOST Hostname to listen on\n" + " -p PORT, --port PORT Port to listen on (default: 7077)\n" + " --webui-port PORT Port for web UI (default: 8080)\n" + " --properties-file FILE Path to a custom Spark properties file.\n" + " Default is conf/spark-defaults.conf.") // scalastyle:on println System.exit(exitCode) } }
Example 66
Source File: ClientArguments.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy import java.net.{URI, URISyntaxException} import scala.annotation.tailrec import scala.collection.mutable.ListBuffer import org.apache.log4j.Level import org.apache.spark.util.{IntParam, MemoryParam, Utils} private def printUsageAndExit(exitCode: Int) { // TODO: It wouldn't be too hard to allow users to submit their app and dependency jars // separately similar to in the YARN client. val usage = s""" |Usage: DriverClient [options] launch <active-master> <jar-url> <main-class> [driver options] |Usage: DriverClient kill <active-master> <driver-id> | |Options: | -c CORES, --cores CORES Number of cores to request (default: $DEFAULT_CORES) | -m MEMORY, --memory MEMORY Megabytes of memory to request (default: $DEFAULT_MEMORY) | -s, --supervise Whether to restart the driver on failure | (default: $DEFAULT_SUPERVISE) | -v, --verbose Print more debugging output """.stripMargin // scalastyle:off println System.err.println(usage) // scalastyle:on println System.exit(exitCode) } } private[deploy] object ClientArguments { val DEFAULT_CORES = 1 val DEFAULT_MEMORY = Utils.DEFAULT_DRIVER_MEM_MB // MB val DEFAULT_SUPERVISE = false def isValidJarUrl(s: String): Boolean = { try { val uri = new URI(s) uri.getScheme != null && uri.getPath != null && uri.getPath.endsWith(".jar") } catch { case _: URISyntaxException => false } } }
Example 67
Source File: HistoryServerArguments.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.history import scala.annotation.tailrec import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.util.Utils private[history] class HistoryServerArguments(conf: SparkConf, args: Array[String]) extends Logging { private var propertiesFile: String = null parse(args.toList) @tailrec private def parse(args: List[String]): Unit = { if (args.length == 1) { setLogDirectory(args.head) } else { args match { case ("--dir" | "-d") :: value :: tail => setLogDirectory(value) parse(tail) case ("--help" | "-h") :: tail => printUsageAndExit(0) case ("--properties-file") :: value :: tail => propertiesFile = value parse(tail) case Nil => case _ => printUsageAndExit(1) } } } private def setLogDirectory(value: String): Unit = { logWarning("Setting log directory through the command line is deprecated as of " + "Spark 1.1.0. Please set this through spark.history.fs.logDirectory instead.") conf.set("spark.history.fs.logDirectory", value) } // This mutates the SparkConf, so all accesses to it must be made after this line Utils.loadDefaultSparkProperties(conf, propertiesFile) private def printUsageAndExit(exitCode: Int) { // scalastyle:off println System.err.println( """ |Usage: HistoryServer [options] | |Options: | DIR Deprecated; set spark.history.fs.logDirectory directly | --dir DIR (-d DIR) Deprecated; set spark.history.fs.logDirectory directly | --properties-file FILE Path to a custom Spark properties file. | Default is conf/spark-defaults.conf. | |Configuration options can be set by setting the corresponding JVM system property. |History Server options are always available; additional options depend on the provider. | |History Server options: | | spark.history.ui.port Port where server will listen for connections | (default 18080) | spark.history.acls.enable Whether to enable view acls for all applications | (default false) | spark.history.provider Name of history provider class (defaults to | file system-based provider) | spark.history.retainedApplications Max number of application UIs to keep loaded in memory | (default 50) |FsHistoryProvider options: | | spark.history.fs.logDirectory Directory where app logs are stored | (default: file:/tmp/spark-events) | spark.history.fs.updateInterval How often to reload log data from storage | (in seconds, default: 10) |""".stripMargin) // scalastyle:on println System.exit(exitCode) } }
Example 68
Source File: MinimaxSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.adversarial import scala.annotation.tailrec object MinimaxDecision { import scala.math.Ordering.Double.IeeeOrdering def minMaxDecision[PLAYER, STATE, ACTION]( g: Game[PLAYER, STATE, ACTION], noAction: ACTION ): (STATE) => ACTION = { (state: STATE) => @tailrec def maxMinValue(Actions: List[ACTION]): ACTION = Actions match { case Nil => noAction case singularAction :: Nil => singularAction case action1 :: action2 :: rest => maxMinValue( (if (minValue(g.result(state, action1)) > minValue(g.result(state, action2))) action1 else action2) :: rest ) } @tailrec def minMaxValue(Actions: List[ACTION]): ACTION = Actions match { case Nil => noAction case singularAction :: Nil => singularAction case action1 :: action2 :: rest => minMaxValue( (if (maxValue(g.result(state, action1)) < maxValue(g.result(state, action2))) action1 else action2) :: rest ) } def maxValue(state: STATE): UtilityValue = { if (g.isTerminalState(state)) g.getUtility(state) else minValue(g.result(state, maxMinValue(g.getActions(state)))) } def minValue(state: STATE): UtilityValue = { if (g.isTerminalState(state)) g.getUtility(state) else maxValue(g.result(state, minMaxValue(g.getActions(state)))) } maxMinValue(g.getActions(g.initialState)) } }
Example 69
Source File: ProblemSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search import scala.annotation.tailrec import scala.reflect.ClassTag trait Problem[State, Action] { def initialState: State def isGoalState(state: State): Boolean def actions(state: State): List[Action] def result(state: State, action: Action): State def stepCost(state: State, action: Action, childPrime: State): Int } sealed trait SearchNode[State, Action] { def state: State def action: Action def parent: Option[SearchNode[State, Action]] } final case class StateNode[State, Action]( state: State, action: Action, parent: Option[StateNode[State, Action]] ) extends SearchNode[State, Action] final case class CostNode[State, Action]( state: State, cost: Int, action: Action, parent: Option[CostNode[State, Action]] ) extends SearchNode[State, Action] final case class HeuristicsNode[State, Action]( state: State, gValue: Double, hValue: Option[Double], fValue: Option[Double], action: Action, parent: Option[CostNode[State, Action]] ) extends SearchNode[State, Action] trait ProblemSearch[State, Action, Node <: SearchNode[State, Action]] { implicit val nCT: ClassTag[Node] def newChildNode(problem: Problem[State, Action], parent: Node, action: Action): Node def solution(node: Node): List[Action] = { @tailrec def solutionHelper(n: Node, actions: List[Action]): List[Action] = { n.parent match { case None => actions case Some(parent: Node) => solutionHelper(parent, n.action :: actions) } } solutionHelper(node, Nil) } }
Example 70
Source File: IterativeDeepeningSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.uninformed import aima.core.search.Problem import scala.annotation.tailrec import scala.util.{Failure, Success, Try} trait IterativeDeepeningSearch[State, Action] { def depthLimitedTreeSearch: DepthLimitedTreeSearch[State, Action] def search(problem: Problem[State, Action], noAction: Action): Try[DLSResult[Action]] = { @tailrec def searchHelper(currentDepth: Int): Try[DLSResult[Action]] = { val result = depthLimitedTreeSearch.search(problem, currentDepth, noAction) result match { case Success(Solution(_)) | Failure(_) => result case _ if currentDepth == Int.MaxValue => Failure[DLSResult[Action]](new Exception("Depth has reached Int.MaxValue")) case _ => searchHelper(currentDepth + 1) } } searchHelper(currentDepth = 0) } }
Example 71
Source File: DepthLimitedTreeSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.uninformed import aima.core.search.{Problem, ProblemSearch, StateNode} import scala.annotation.tailrec import scala.util.{Failure, Success, Try} sealed trait DLSResult[Action] { def actions: List[Action] } final case class Solution[Action](actions: List[Action]) extends DLSResult[Action] final case class CutOff[Action](actions: List[Action]) extends DLSResult[Action] trait DepthLimitedTreeSearch[State, Action] extends ProblemSearch[State, Action, StateNode[State, Action]] { type Node = StateNode[State, Action] def search(problem: Problem[State, Action], initialLimit: Int, noAction: Action): Try[DLSResult[Action]] = Try { def recursiveDLS(node: Node, currentLimit: Int): Try[DLSResult[Action]] = Try { if (problem.isGoalState(node.state)) { Success(Solution(solution(node))) } else if (currentLimit == 0) { Success(CutOff(solution(node))) } else { val childNodes = for { action <- problem.actions(node.state) } yield newChildNode(problem, node, action) @tailrec def shortCircuitChildSearch(children: List[Node]): Try[DLSResult[Action]] = { children match { case Nil => Failure[DLSResult[Action]](new Exception("Exhausted child nodes")) case lastChild :: Nil => recursiveDLS(lastChild, currentLimit - 1) case firstChild :: rest => recursiveDLS(firstChild, currentLimit - 1) match { case result @ Success(Solution(_)) => result case _ => shortCircuitChildSearch(rest) } } } shortCircuitChildSearch(childNodes) } }.flatten recursiveDLS(makeNode(problem.initialState, noAction), initialLimit) }.flatten def makeNode(state: State, noAction: Action): Node = StateNode(state, noAction, None) def newChildNode(problem: Problem[State, Action], parent: Node, action: Action): Node = { val childState = problem.result(parent.state, action) StateNode(childState, action, Some(parent)) } }
Example 72
Source File: UniformCostSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.uninformed import aima.core.search._ import scala.annotation.tailrec trait UniformCostSearch[State, Action] extends ProblemSearch[State, Action, CostNode[State, Action]] with FrontierSearch[State, Action, CostNode[State, Action]] { type Node = CostNode[State, Action] def search(problem: Problem[State, Action], noAction: Action): List[Action] = { val initialFrontier = newFrontier(problem.initialState, noAction) @tailrec def searchHelper( frontier: Frontier[State, Action, Node], exploredSet: Set[State] = Set.empty[State] ): List[Action] = { frontier.removeLeaf match { case None => List.empty[Action] case Some((leaf, _)) if problem.isGoalState(leaf.state) => solution(leaf) case Some((leaf, updatedFrontier)) => val updatedExploredSet = exploredSet + leaf.state val childNodes = for { action <- problem.actions(leaf.state) } yield newChildNode(problem, leaf, action) val frontierWithChildNodes = childNodes.foldLeft(updatedFrontier) { (accFrontier, childNode) => if (!(updatedExploredSet.contains(childNode.state) || accFrontier.contains(childNode.state))) { accFrontier.add(childNode) } else if (accFrontier .getNode(childNode.state) .exists(existingNode => existingNode.cost > childNode.cost)) { accFrontier.replaceByState(childNode) } else { accFrontier } } searchHelper(frontierWithChildNodes, updatedExploredSet) } } searchHelper(initialFrontier) } def newFrontier(state: State, noAction: Action) = { val costNodeOrdering: Ordering[Node] = new Ordering[Node] { def compare(n1: Node, n2: Node): Int = Ordering.Int.reverse.compare(n1.cost, n2.cost) } new PriorityQueueHashSetFrontier[State, Action, Node](CostNode(state, 0, noAction, None), costNodeOrdering) } def newChildNode(problem: Problem[State, Action], parent: Node, action: Action): Node = { val childState = problem.result(parent.state, action) CostNode( state = childState, cost = parent.cost + problem.stepCost(parent.state, action, childState), action = action, parent = Some(parent) ) } }
Example 73
Source File: GraphSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.uninformed import aima.core.search._ import scala.annotation.tailrec import scala.reflect.ClassTag trait GraphSearch[State, Action] extends ProblemSearch[State, Action, StateNode[State, Action]] with FrontierSearch[State, Action, StateNode[State, Action]] { implicit val sCT: ClassTag[State] implicit val aCT: ClassTag[Action] type Node = StateNode[State, Action] def search(problem: Problem[State, Action], noAction: Action): List[Action] = { val initialFrontier = newFrontier(problem.initialState, noAction) @tailrec def searchHelper( frontier: Frontier[State, Action, Node], exploredSet: Set[State] = Set.empty[State] ): List[Action] = { frontier.removeLeaf match { case None => List.empty[Action] case Some((leaf, _)) if problem.isGoalState(leaf.state) => solution(leaf) case Some((leaf, updatedFrontier)) => val updatedExploredSet = exploredSet + leaf.state val childNodes = for { action <- problem.actions(leaf.state) childNode = newChildNode(problem, leaf, action) if !(updatedExploredSet.contains(childNode.state) || updatedFrontier.contains(childNode.state)) } yield childNode val frontierWithChildNodes = updatedFrontier.addAll(childNodes) searchHelper(frontierWithChildNodes, updatedExploredSet) } } searchHelper(initialFrontier) } def newChildNode(problem: Problem[State, Action], parent: Node, action: Action): Node = { val childState = problem.result(parent.state, action) StateNode(childState, action, Some(parent)) } }
Example 74
Source File: RecursiveBestFirstSearch.scala From aima-scala with MIT License | 5 votes |
package aima.core.search.informed import aima.core.search.{HeuristicsNode, Problem, ProblemSearch} import Ordering.Double.TotalOrdering import scala.annotation.tailrec import scala.reflect.ClassTag sealed trait RBFSearchResult final case class Solution[Action](actions: List[Action]) extends RBFSearchResult final case class SearchFailure(updatedFCost: Double) extends RBFSearchResult trait RecursiveBestFirstSearch[State, Action] extends ProblemSearch[State, Action, HeuristicsNode[State, Action]] { implicit val sCT: ClassTag[State] implicit val aCT: ClassTag[Action] type Node = HeuristicsNode[State, Action] type Heuristic = Node => Double def search(problem: Problem[State, Action], noAction: Action, h: Heuristic): RBFSearchResult = { def rbfs(node: Node, fLimit: Double): RBFSearchResult = { if (problem.isGoalState(node.state)) Solution(solution(node)) else { val successors = for { action <- problem.actions(node.state) } yield newChildNode(problem, node, action) if (successors.isEmpty) SearchFailure(Double.PositiveInfinity) else { val updated = successors.collect { case s @ HeuristicsNode(_, gValue, Some(hValue), _, _, _) => val updatedFValue = node.fValue.map(nodeFValue => math.max(gValue + hValue, nodeFValue)) s.copy(fValue = updatedFValue) } @tailrec def getBestFValue(updatedSuccessors: List[Node]): RBFSearchResult = { val sortedSuccessors = updatedSuccessors.sortBy(_.fValue.getOrElse(Double.MaxValue)) sortedSuccessors match { case HeuristicsNode(_, _, _, Some(fValue), _, _) :: _ if fValue > fLimit => SearchFailure(fValue) case best :: (second @ HeuristicsNode(_, _, _, Some(fValue), _, _)) :: rest => val result = rbfs(best, math.min(fLimit, fValue)) result match { case s: Solution[Action] => s case SearchFailure(updatedFValue) => getBestFValue(best.copy(fValue = Some(updatedFValue)) :: second :: rest) } } } getBestFValue(updated) } } } rbfs(makeNode(problem.initialState, noAction, h), Double.PositiveInfinity) } def makeNode(state: State, noAction: Action, h: Heuristic): Node = { val basic = HeuristicsNode(state = state, gValue = 0, hValue = None, fValue = None, noAction, None) val hValue: Double = h(basic) val fValue: Double = basic.gValue + hValue basic.copy(hValue = Some(hValue), fValue = Some(fValue)) } }
Example 75
Source File: ReflexVacuumAgentProgramSpec.scala From aima-scala with MIT License | 5 votes |
package aima.core.environment.vacuum import aima.core.agent.{Actuator, Agent, AgentProgram, Environment, Sensor} import org.scalacheck.Arbitrary import org.specs2.ScalaCheck import org.specs2.mutable.Specification import scala.annotation.tailrec class ReflexVacuumAgentProgramSpec extends Specification with ScalaCheck { implicit val arbVacuumEnvironment = Arbitrary(VacuumEnvironment()) "should eventually clean environment" in prop { env: VacuumEnvironment => val agent = new Agent[VacuumEnvironment, VacuumPercept, VacuumAction] { val agentProgram = new SimpleReflexVacuumAgentProgram val actuators = List[Actuator[VacuumEnvironment, VacuumAction]](new SuckerActuator(this), new MoveActuator(this)) lazy val sensors = List[Sensor[VacuumEnvironment, VacuumPercept]]( new DirtSensor(this), new AgentLocationSensor(this) ) } @tailrec def eventuallyClean(currentEnv: VacuumEnvironment): Boolean = { currentEnv match { case ve: VacuumEnvironment if ve.isClean() => true case _ => eventuallyClean(agent.run(currentEnv)._1) } } eventuallyClean(env.addAgent(agent)) must beTrue } }
Example 76
Source File: Program.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.vm import akka.util.ByteString import io.iohk.ethereum.crypto.kec256 import scala.annotation.tailrec @tailrec private def validJumpDestinationsAfterPosition(pos: Int, accum: Set[Int] = Set.empty): Set[Int] = { if(pos < 0 || pos >= length) accum else { val byte = code(pos) val opCode = frontierConfig.byteToOpCode.get(byte) // we only need to check PushOp and JUMPDEST, they are both present in Frontier opCode match { case Some(pushOp: PushOp) => validJumpDestinationsAfterPosition(pos + pushOp.i + 2, accum) case Some(JUMPDEST) => validJumpDestinationsAfterPosition(pos + 1, accum + pos) case _ => validJumpDestinationsAfterPosition(pos + 1, accum) } } } lazy val codeHash: ByteString = kec256(code) }
Example 77
package io.iohk.ethereum.vm import io.iohk.ethereum.utils.Logger import scala.annotation.tailrec def run[W <: WorldStateProxy[W, S], S <: Storage[S]](context: ProgramContext[W, S]): ProgramResult[W, S] = { PrecompiledContracts.runOptionally(context).getOrElse { val finalState = run(ProgramState[W, S](context)) ProgramResult[W, S]( finalState.returnData, finalState.gas, finalState.world, finalState.addressesToDelete, finalState.logs, finalState.internalTxs, finalState.gasRefund, finalState.error) } } @tailrec private def run[W <: WorldStateProxy[W, S], S <: Storage[S]](state: ProgramState[W, S]): ProgramState[W, S] = { val byte = state.program.getByte(state.pc) state.config.byteToOpCode.get(byte) match { case Some(opCode) => val newState = opCode.execute(state) import newState._ log.trace(s"$opCode | pc: $pc | depth: ${env.callDepth} | gas: $gas | stack: $stack") if (newState.halted) newState else run[W, S](newState) case None => state.withError(InvalidOpCode(byte)).halt } } } object VM extends VM
Example 78
Source File: LedgerUtils.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.ledger import scala.annotation.tailrec object LedgerUtils { @tailrec private[ledger] def binaryChop[Error](min: BigInt, max: BigInt)(f: BigInt => Option[Error]): BigInt = { assert(min <= max) if (min == max) max else { val mid = min + (max - min) / 2 val possibleError = f(mid) if (possibleError.isEmpty) binaryChop(min, mid)(f) else binaryChop(mid + 1, max)(f) } } }
Example 79
Source File: ColorBlender.scala From scalismo-faces with Apache License 2.0 | 5 votes |
package scalismo.faces.color import scalismo.color.ColorSpaceOperations import scalismo.geometry._ import scala.annotation.tailrec implicit def optionBlender[A](implicit blender: ColorBlender[A]) = new ColorBlender[Option[A]] { override def blend(obj1: Option[A], obj2: Option[A], l: Double): Option[A] = { (obj1, obj2) match { case (Some(o1), Some(o2)) => Some(blender.blend(o1, o2, l)) case (Some(o1), None) => Some(o1) case (None, Some(o2)) => Some(o2) case (None, None) => None } } } }
Example 80
Source File: WarpFieldInversion.scala From scalismo-faces with Apache License 2.0 | 5 votes |
package scalismo.faces.warp import scalismo.faces.image.PixelImage import scalismo.geometry.{EuclideanVector, _2D} import scala.annotation.tailrec def fixedPointInversion(warpField: PixelImage[EuclideanVector[_2D]], iterations: Int = 5): PixelImage[EuclideanVector[_2D]] = { val contField = warpField.interpolate // iteration: iw(x) = -w( x + iw(x) ) @tailrec def fixedPointIteration(invW: PixelImage[EuclideanVector[_2D]], iterations: Int): PixelImage[EuclideanVector[_2D]] = { if (iterations > 0) { // for each position pull back the correct warp field value val invField = PixelImage(warpField.domain, (x, y) => { val v = EuclideanVector(x, y) + invW(x, y) -contField(v.x, v.y): EuclideanVector[_2D] }) fixedPointIteration(invField, iterations - 1) } else invW } // initial: iw(x) = 0 val init: PixelImage[EuclideanVector[_2D]] = warpField.map(v => EuclideanVector(0f, 0f)) // iterate: fixed-point iterations fixedPointIteration(init, iterations) } }
Example 81
Source File: MenuIO.scala From hacktoberfest-scala-algorithms with GNU General Public License v3.0 | 5 votes |
package io.github.sentenza.hacktoberfest import java.lang.System.out.println import java.lang.reflect.Method import java.util.concurrent.atomic.AtomicInteger import io.github.sentenza.hacktoberfest.algos.{ImmutableSorting, MutableSorting, Sorting} import scala.annotation.tailrec import scala.util.{Success, Try} def printDisclaimer() { println(heading + gplDisclaimer) } private val noOp = () => () def readNumberInputs = scala.io.StdIn.readLine().split(",").map(_.toInt) case class MenuEntry(selector: Int, display: String, code: () => Unit) private val entries = List( MenuEntry(1, "Sorting algorithms", () => { println("You chose sorting\n") renderInteractiveMenu(List( MenuEntry(2, "MutableSorting", () => { println("You chose mutable sorting.") renderInteractiveMenu(createMethodMenuEntries(MutableSorting)) }), MenuEntry(1, "ImmutableSorting", () => { println("You chose immutable sorting.") renderInteractiveMenu(createMethodMenuEntries(ImmutableSorting)) }), MenuEntry(0, "Quit sorting", () => noOp) )) }), MenuEntry(0, "Quit the program",() => System.exit(0)) ) private def createMethodMenuEntries(sorting: Sorting[_,_]) = { val count = new AtomicInteger() retrieveMethodNames(sorting) .map(mName => MenuEntry(count.incrementAndGet(), mName, () => executeSortMethod(sorting, mName)) ).toList } private def retrieveMethodNames(sorting:Sorting[_,_]) = sorting.getClass.getMethods.map(_.getName).filter(_.endsWith("Sort")).distinct private def executeSortMethod(sorting: Sorting[_,_], method: String) = { println("You've chosen " + method + "! Please enter a list of comma separated integers.") val numberInputs = readNumberInputs println(s"You entered:${numberInputs.mkString(",")}. They are going to be sorted by $method.\n Sorting...") val sorted = execute(sorting, method, numberInputs) println(s"Your number entries sorted are: ${sorted.mkString(",")}") } private def execute[F[_],T](sorting: Sorting[_,_], method: String, numberInputs: F[_]) = { findMethod(sorting, method) match { case Some(m:Method) => m.invoke(sorting, numberInputs).asInstanceOf[F[_]] case None => throw new RuntimeException(s"Method $method not found in $sorting") } } private def findMethod(sorting: Sorting[_,_], method: String) = sorting.getClass.getMethods.find(m => m.getName.compare(method) == 0) @tailrec def renderInteractiveMenu(entries:List[MenuEntry]=entries): Unit = { println("Please choose:") entries.foreach { case MenuEntry(num, label, _) => println(s"$num: $label") } Try(scala.io.StdIn.readInt()) match { case Success(0) => () case Success(choice) if entries.exists(_.selector == choice) => entries.find(_.selector == choice).foreach{ case MenuEntry(_, _, code) => code() } renderInteractiveMenu() case _ => println("Invalid selection\n") renderInteractiveMenu() } } }
Example 82
Source File: SparkFunSuite.scala From spark-alchemy with Apache License 2.0 | 5 votes |
package org.apache.spark // scalastyle:off import java.io.File import scala.annotation.tailrec import org.apache.log4j.{Appender, Level, Logger} import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, BeforeAndAfterEach, FunSuite, Outcome, Suite} import org.apache.spark.internal.Logging import org.apache.spark.internal.config.Tests.IS_TESTING import org.apache.spark.util.{AccumulatorContext, Utils} protected def withLogAppender( appender: Appender, loggerName: Option[String] = None, level: Option[Level] = None)( f: => Unit): Unit = { val logger = loggerName.map(Logger.getLogger).getOrElse(Logger.getRootLogger) val restoreLevel = logger.getLevel logger.addAppender(appender) if (level.isDefined) { logger.setLevel(level.get) } try f finally { logger.removeAppender(appender) if (level.isDefined) { logger.setLevel(restoreLevel) } } } }
Example 83
Source File: RefTypeMonadErrorSpec.scala From refined with MIT License | 5 votes |
package eu.timepit.refined.cats import _root_.cats.MonadError import eu.timepit.refined.types.numeric.PosInt import org.scalacheck.Prop._ import org.scalacheck.Properties import scala.annotation.tailrec import scala.util.{Failure, Success, Try} trait Decoder[A] { def decode(s: String): Either[String, A] } object Decoder { def apply[A](implicit d: Decoder[A]): Decoder[A] = d def instance[A](f: String => Either[String, A]): Decoder[A] = new Decoder[A] { override def decode(s: String): Either[String, A] = f(s) } implicit val decoderMonadError: MonadError[Decoder, String] = new MonadError[Decoder, String] { override def flatMap[A, B](fa: Decoder[A])(f: A => Decoder[B]): Decoder[B] = instance { s => fa.decode(s) match { case Right(a) => f(a).decode(s) case Left(err) => Left(err) } } override def tailRecM[A, B](a: A)(f: A => Decoder[Either[A, B]]): Decoder[B] = { @tailrec def step(s: String, a1: A): Either[String, B] = f(a1).decode(s) match { case Right(Right(b)) => Right(b) case Right(Left(a2)) => step(s, a2) case Left(err) => Left(err) } instance(s => step(s, a)) } override def raiseError[A](e: String): Decoder[A] = instance(_ => Left(e)) override def handleErrorWith[A](fa: Decoder[A])(f: String => Decoder[A]): Decoder[A] = instance { s => fa.decode(s) match { case Right(a) => Right(a) case Left(err) => f(err).decode(s) } } override def pure[A](x: A): Decoder[A] = instance(_ => Right(x)) } implicit val intDecoder: Decoder[Int] = instance(s => Try(s.toInt) match { case Success(i) => Right(i) case Failure(t) => Left(t.getMessage) } ) } class RefTypeMonadErrorSpec extends Properties("MonadError") { property("Decoder[Int]") = secure { Decoder[Int].decode("1") ?= Right(1) } property("derive Decoder[PosInt] via MonadError[Decoder, String]") = { // This import is needed because of https://github.com/scala/bug/issues/10753 import Decoder.decoderMonadError import eu.timepit.refined.cats.derivation._ val decoder = Decoder[PosInt] (decoder.decode("1") ?= Right(PosInt.unsafeFrom(1))) && (decoder.decode("-1") ?= Left("Predicate failed: (-1 > 0).")) } }
Example 84
Source File: Retry.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.counter.util import scala.annotation.tailrec import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success, Try} object Retry { @tailrec def apply[T](n: Int, withSleep: Boolean = true, tryCount: Int = 0)(fn: => T): T = { Try { fn } match { case Success(x) => x case Failure(e) if e.isInstanceOf[RetryStopException] => throw e.getCause case _ if n > 1 => // backoff if (withSleep) Thread.sleep(tryCount * 1000) apply(n - 1, withSleep, tryCount + 1)(fn) case Failure(e) => throw e } } } object RetryAsync { def apply[T](n: Int, withSleep: Boolean = true, tryCount: Int = 0)(fn: => Future[T])(implicit ex: ExecutionContext): Future[T] = { val promise = Promise[T]() fn onComplete { case Success(x) => promise.success(x) case Failure(e) if e.isInstanceOf[RetryStopException] => promise.failure(e.getCause) case _ if n > 1 => // backoff if (withSleep) Thread.sleep(tryCount * 1000) apply(n - 1, withSleep, tryCount + 1)(fn) case Failure(e) => promise.failure(e) } promise.future } } class RetryStopException(message: String, cause: Throwable) extends Exception(message, cause) { def this(message: String) = this(message, null) def this(cause: Throwable) = this(cause.toString, cause) }
Example 85
Source File: NodeRestConnector.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.tool.connectors import cats.syntax.either._ import com.wavesplatform.dex.cli.ErrorOr import com.wavesplatform.dex.tool.connectors.Connector.RepeatRequestOptions import com.wavesplatform.dex.tool.connectors.RestConnector.ErrorOrJsonResponse import com.wavesplatform.wavesj.Transaction import com.wavesplatform.wavesj.json.WavesJsonMapper import play.api.libs.json.jackson.PlayJsonModule import play.api.libs.json.{JsValue, JsonParserSettings} import sttp.client._ import sttp.model.MediaType import scala.annotation.tailrec import scala.concurrent.duration._ case class NodeRestConnector(target: String, chainId: Byte) extends RestConnector { override implicit val repeatRequestOptions: RepeatRequestOptions = { val blocksCount = 5 ( for { currentHeight <- getCurrentHeight blocksTs <- getBlockHeadersAtHeightRange((currentHeight - blocksCount).max(0), currentHeight) .map(_.map(json => (json \ "timestamp").as[Long])) .ensure("0 or 1 blocks have been forged at the moment, try again later")(_.size > 1) } yield { val timeBetweenBlocks = ((blocksTs.last - blocksTs.head) / blocksCount * 1.5 / 1000).toInt RepeatRequestOptions(timeBetweenBlocks, 1.second) } ).fold(ex => throw new RuntimeException(s"Could not construct repeat request options: $ex"), identity) } private val mapper: WavesJsonMapper = new WavesJsonMapper(chainId); mapper.registerModule(new PlayJsonModule(JsonParserSettings())) def broadcastTx(tx: Transaction): ErrorOrJsonResponse = mkResponse { _.post(uri"$targetUri/transactions/broadcast").body(mapper writeValueAsString tx).contentType(MediaType.ApplicationJson) } def getTxInfo(txId: String): ErrorOrJsonResponse = mkResponse { _.get(uri"$targetUri/transactions/info/$txId") } def getTxInfo(tx: JsValue): ErrorOrJsonResponse = getTxInfo { (tx \ "id").as[String] } def getTxInfo(tx: Transaction): ErrorOrJsonResponse = getTxInfo(tx.getId.toString) def getCurrentHeight: ErrorOr[Long] = mkResponse { _.get(uri"$targetUri/blocks/height") }.map(json => (json \ "height").as[Long]) def getBlockHeadersAtHeightRange(from: Long, to: Long): ErrorOr[Seq[JsValue]] = mkResponse { _.get(uri"$targetUri/blocks/headers/seq/$from/$to") }.map(_.as[Seq[JsValue]]) @tailrec final def waitForHeightArise(): ErrorOr[Long] = getCurrentHeight match { case Right(origHeight) => repeatRequest(getCurrentHeight) { _.exists(_ > origHeight) } case Left(_) => waitForHeightArise() } }
Example 86
Source File: Connector.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.tool.connectors import cats.syntax.either._ import cats.syntax.option._ import com.wavesplatform.dex.cli.ErrorOr import com.wavesplatform.dex.tool.connectors.Connector.RepeatRequestOptions import scala.annotation.tailrec import scala.concurrent.duration._ trait Connector extends AutoCloseable { protected val target: String implicit val repeatRequestOptions: RepeatRequestOptions = RepeatRequestOptions.default final def repeatRequest[A](sendRequest: => ErrorOr[A])(test: ErrorOr[A] => Boolean)(implicit repeatRequestOptions: RepeatRequestOptions = repeatRequestOptions): ErrorOr[A] = { @tailrec def go(ro: RepeatRequestOptions, lastResponse: Option[ErrorOr[A]]): ErrorOr[A] = { if (ro.attemptsLeft == 0) s"All attempts are out! ${lastResponse.fold("")(lr => s"Last response: ${lr.fold(identity, _.toString)}")}".asLeft else { val response = sendRequest if (test(response)) response else { Thread.sleep(ro.delay.toMillis) go(ro.decreaseAttempts, response.some) } } } go(repeatRequestOptions, None) } } object Connector { final case class RepeatRequestOptions(attemptsLeft: Int, delay: FiniteDuration) { def decreaseAttempts: RepeatRequestOptions = copy(attemptsLeft = attemptsLeft - 1) override def toString: String = s"max attempts = $attemptsLeft, interval = $delay" } object RepeatRequestOptions { val default: RepeatRequestOptions = RepeatRequestOptions(10, 1.second) } }
Example 87
Source File: ReadOnlyDB.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.db.leveldb import com.wavesplatform.dex.metrics.LevelDBStats import com.wavesplatform.dex.metrics.LevelDBStats.DbHistogramExt import org.iq80.leveldb.{DB, DBIterator, ReadOptions} import scala.annotation.tailrec class ReadOnlyDB(db: DB, readOptions: ReadOptions) { def get[V](key: Key[V]): V = { val bytes = db.get(key.keyBytes, readOptions) LevelDBStats.read.recordTagged(key, bytes) key.parse(bytes) } def has[V](key: Key[V]): Boolean = { val bytes = db.get(key.keyBytes, readOptions) LevelDBStats.read.recordTagged(key, bytes) bytes != null } def iterator: DBIterator = db.iterator(readOptions) def iterateOver(prefix: Short)(f: DBEntry => Unit): Unit = db.iterateOver(prefix)(f) def read[T](keyName: String, prefix: Array[Byte], seek: Array[Byte], n: Int)(deserialize: DBEntry => T): Vector[T] = { val iter = iterator @tailrec def loop(aux: Vector[T], restN: Int, totalBytesRead: Long): (Vector[T], Long) = { if (restN > 0 && iter.hasNext) { val elem = iter.next() if (elem.getKey.startsWith(prefix)) loop(aux :+ deserialize(elem), restN - 1, totalBytesRead + elem.getValue.length) else (aux, totalBytesRead) } else (aux, totalBytesRead) } try { iter.seek(seek) val (r, totalBytesRead) = loop(Vector.empty, n, 0) LevelDBStats.read.recordTagged(keyName, totalBytesRead) r } finally iter.close() } }
Example 88
Source File: Util.scala From nyaya with GNU Lesser General Public License v2.1 | 5 votes |
package nyaya.util import scala.annotation.tailrec import scala.collection.Iterable object Util { @inline def quickSB(f: StringBuilder => Unit): String = { val sb = new StringBuilder f(sb) sb.toString } @inline def quickSB(start: String, f: StringBuilder => Unit): String = { val sb = new StringBuilder(start) f(sb) sb.toString } def escapeString(s: String): String = s.toCharArray.map { case '\n' => "\\n" case '\r' => "\\r" case '\t' => "\\t" case '\\' => "\\" case '"' => "\\\"" case n if n >= 32 && n <= 127 => n.toString //case n if n < 256 => "\\x%02x" format n.toInt case n => "\\u%04x" format n.toLong }.mkString def asciiTree[N](root: Iterable[N])(leaves: N => Iterable[N], show: N => String, indent: String = ""): String = quickSB(asciiTreeSB(root)(_, leaves, show, indent)) def asciiTreeSB[N](root: Iterable[N])(sb: StringBuilder, leaves: N => Iterable[N], show: N => String, indent: String = ""): Unit = { val pm = "│ " val pl = " " val cm = "├─ " val cl = "└─ " var first = true @inline def loop2 = loop(_, _, _) @tailrec def loop(parentLvlLast: Vector[Boolean], fs: List[N], root: Boolean): Unit = fs match { case Nil => case h :: t => def indentPrefix(): Unit = { sb append indent for (b <- parentLvlLast) sb.append(if (b) pl else pm) } if (first) first = false else sb append '\n' var indentlen = sb.length indentPrefix() val last = t.isEmpty if (!root) sb.append(if (last) cl else cm) indentlen = sb.length - indentlen var firstLine = true for (l <- show(h).split("\n")) { if (firstLine) firstLine = false else { sb append '\n' indentPrefix() sb append " " } sb append l } val nextLvl = if (root) Vector.empty[Boolean] else parentLvlLast :+ last loop2(nextLvl, leaves(h).toList, false) loop(parentLvlLast, t, root) } loop(Vector.empty, root.toList, true) } }
Example 89
Source File: CycleDetector.scala From nyaya with GNU Lesser General Public License v2.1 | 5 votes |
package nyaya.prop import scala.annotation.tailrec import scala.collection.Iterable import scalaz.{\/-, -\/, \/} final case class CycleFree[A](value: A) case class CycleDetector[A, B](extract: A => Iterator[B], check: (A, Iterator[B]) => Option[(B, B)]) { @inline final def hasCycle(a: A): Boolean = findCycle(a).isDefined @inline final def noCycle(a: A): Boolean = findCycle(a).isEmpty final def findCycle(a: A): Option[(B, B)] = check(a, extract(a)) def cycleFree(a: A): (B, B) \/ CycleFree[A] = findCycle(a).fold[(B, B) \/ CycleFree[A]](\/-(CycleFree(a)))(-\/.apply) def contramap[Z](f: Z => A) = new CycleDetector[Z, B](extract compose f, (z, b) => check(f(z), b)) def noCycleProp(name: => String): Prop[A] = Prop.atom[A](name, findCycle(_).map{ case (b1,b2) => s"Cycle detected: [$b1] → [$b2]" }) } object CycleDetector { object Undirected extends GraphType { override def check[A, B, I](vertices: (A, B) => Iterator[B], id: B => I): (A, Iterator[B]) => Option[(B, B)] = (a, input) => { def outer(prev: B, is0: Set[I]): Set[I] \/ (B, B) = { val it = vertices(a, prev) @tailrec def inner(is: Set[I]): Set[I] \/ (B, B) = if (it.hasNext) { val b = it.next() val i = id(b) if (is contains i) \/-((prev, b)) else outer(b, is + i) match { case -\/(is2) => inner(is2) case r@ \/-(_) => r } } else -\/(is) inner(is0) } input .map(b => outer(b, Set(id(b)))) .collectFirst { case \/-(v) => v } } } object Directed extends GraphType { override def check[A, B, I](vertices: (A, B) => Iterator[B], id: B => I): (A, Iterator[B]) => Option[(B, B)] = (a, input) => { def loop(prev: B, is: Set[I]): Option[(B, B)] = vertices(a, prev) .map { b => val i = id(b) if (is contains i) Some((prev, b)) else loop(b, is + i) } .collectFirst { case Some(v) => v } input .map(b => loop(b, Set(id(b)))) .collectFirst { case Some(v) => v } } } sealed abstract class GraphType { def check[A, B, I](vertices: (A, B) => Iterator[B], id: B => I): (A, Iterator[B]) => Option[(B, B)] def tree[A, I](vertices: A => Iterator[A], id: A => I) = CycleDetector[Iterator[A], A]( identity, check((_, a) => vertices(a), id)) def map[A, I](id: A => I) = CycleDetector[Map[A, A], A]( _.keys.iterator, check(_.get(_).iterator, id)) def multimap[V[_], A, I](id: A => I, empty: V[A])(implicit ev: V[A] <:< Iterable[A]) = CycleDetector[Map[A, V[A]], A]( _.keys.iterator, check(_.getOrElse(_, empty).iterator, id)) } }
Example 90
Source File: ColumnFragmentsSpec.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
package daf.dataset.query.jdbc import daf.dataset.query._ import org.scalatest.{ MustMatchers, WordSpec } import doobie.implicits.toSqlInterpolator import scala.annotation.tailrec import scala.util.Success class SelectFragmentSpec extends WordSpec with MustMatchers { "A [select] fragment writer" must { "serialize a [select] clause in SQL" in { ColumnFragments.select { SelectClauses.simple }.run.map { _._1.toString } must be { Success { fr"SELECT col1, col2 AS alias1, 1, 'string' AS alias2, MAX(col3) AS alias3, SUM(true)".toString } } } "create a column/alias reference set" in { ColumnFragments.select { SelectClauses.simple }.run.get._2 must have ( ColumnReferenceMatchers hasColumn "col1", ColumnReferenceMatchers hasColumn "col2", ColumnReferenceMatchers hasColumn "1", ColumnReferenceMatchers hasColumn "'string'", ColumnReferenceMatchers hasColumn "col3", ColumnReferenceMatchers hasColumn "true", ColumnReferenceMatchers hasAlias "alias1", ColumnReferenceMatchers hasAlias "alias2", ColumnReferenceMatchers hasAlias "alias3" ) } "serialize a very long [select] without stack overflow" in { ColumnFragments.select { SelectClauses.nested }.run must be { 'Success } } "fail serialization when sql is injected in a column name" in { ColumnFragments.select { SelectClauses.injectNamed }.run must be { 'Failure } } "escape quotes in value strings" in { ColumnFragments.select { SelectClauses.injectValue }.run.map { _._1.toString } must be { Success { fr"""SELECT '\' SELECT col2 FROM table WHERE \'\' == \''""".toString } } } } } object SelectClauses { val simple = SelectClause { Seq( NamedColumn("col1"), NamedColumn("col2") as "alias1", ValueColumn(1), ValueColumn("string") as "alias2", Max(NamedColumn("col3")) as "alias3", Sum(ValueColumn(true)) ) } val injectNamed = SelectClause { Seq( NamedColumn("SELECT col2 FROM table") ) } val injectValue = SelectClause { Seq( ValueColumn("' SELECT col2 FROM table WHERE '' == '") ) } @tailrec private def nest(column: Column, n: Int = 10000): Column = if (n == 0) column else nest(Sum(column), n - 1) val nested = SelectClause { Seq { nest(ValueColumn(true)) } } }
Example 91
Source File: Measures.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package sensor import scala.annotation.tailrec import scala.util.matching.Regex import cats.instances.char._ import cats.kernel.Eq import cats.syntax.eq._ import scalariform.lexer.{Token, TokenType, Tokens} object Measures { implicit val tokenTypeEq = Eq.fromUniversalEquals[TokenType] val NewLineRegex: Regex = "(\r\n)|\r|\n".r def countClasses(tokens: List[Token]): Int = { tokens.foldLeft(0) { // scalastyle:ignore org.scalastyle.scalariform.NamedArgumentChecker case (acc, token) => val tokenType = token.tokenType if (tokenType === Tokens.CLASS || tokenType === Tokens.OBJECT) acc + 1 else acc } } def countMethods(tokens: List[Token]): Int = { tokens.foldLeft(0) { // scalastyle:ignore org.scalastyle.scalariform.NamedArgumentChecker case (acc, token) => if (token.tokenType === Tokens.DEF) acc + 1 else acc } } @tailrec def countCommentLines(tokens: List[Token], i: Int = 0): Int = { tokens match { case Nil => i case token :: tail if token.tokenType.isComment => token.tokenType match { case Tokens.LINE_COMMENT => countCommentLines(tail, i + 1) case Tokens.MULTILINE_COMMENT => countCommentLines(tail, i + token.rawText.count(_ === '\n') + 1) case Tokens.XML_COMMENT => new scala.NotImplementedError("XML ?!"); i case _ => i // Not a comment! } case _ :: tail => countCommentLines(tail, i) } } @tailrec def countNonCommentLines(tokens: List[Token], i: Int = 0): Int = { @tailrec def getNextLine(tokens: List[Token]): List[Token] = { tokens match { case Nil => Nil case token :: tail if token.tokenType === Tokens.WS && NewLineRegex.findFirstIn(token.text).nonEmpty => tail case token :: tail if token.tokenType === Tokens.LINE_COMMENT => tail case _ :: tail => getNextLine(tail) } } tokens match { case Nil => i case token :: tail if token.tokenType === Tokens.WS => countNonCommentLines(tail, i) case token :: _ if token.tokenType === Tokens.EOF => i case token :: tail => if (!token.tokenType.isNewline & !token.tokenType.isComment) countNonCommentLines(getNextLine(tail), i + 1) else countNonCommentLines(tail, i) } } }
Example 92
Source File: _11_Example_Replicate.scala From LearningScala with Apache License 2.0 | 5 votes |
package _040_pattern_matching object _11_Example_Replicate extends App { def replicate[A](count: Int, a: A): List[A] = count match { case 0 => Nil case _ => a :: replicate(count - 1, a) } println("replicate:") println(replicate(5, "yay")) println(replicate(5, 10)) println("=============================\n") def replicateTailRecursive[A](count: Int, a: A): List[A] = { import scala.annotation.tailrec @tailrec def replHelper(c: Int, acc: List[A]): List[A] = c match { case 0 => acc case _ => replHelper(c - 1, a :: acc) } replHelper(count, Nil) } println("replicateTailRecursive:") println(replicateTailRecursive(5, "yay")) println(replicateTailRecursive(5, 10)) println("=============================\n") }
Example 93
Source File: SplitMethodsOnUnionTypes.scala From Converter with GNU General Public License v3.0 | 5 votes |
package org.scalablytyped.converter.internal package ts package transforms import scala.annotation.tailrec object SplitMethodsOnUnionTypes extends TransformMembers with TransformClassMembers { override def newClassMembers(scope: TsTreeScope, x: HasClassMembers): IArray[TsMember] = x.members flatMap { case x: TsMemberFunction if !x.isOptional && x.name =/= TsIdent.Apply => RemoveComment.keepFirstOnly(split(x.signature).map(sig => x.copy(signature = sig))) case x: TsMemberCall => RemoveComment.keepFirstOnly(split(x.signature).map(sig => x.copy(signature = sig))) case x: TsMemberCtor => RemoveComment.keepFirstOnly(split(x.signature).map(sig => x.copy(signature = sig))) case other => IArray(other) } override def newMembers(scope: TsTreeScope, x: TsContainer): IArray[TsContainerOrDecl] = x.members flatMap { case x: TsDeclFunction => RemoveComment.keepFirstOnly(split(x.signature).map(sig => x.copy(signature = sig))) case other => IArray(other) } private def split(origin: TsFunSig): IArray[TsFunSig] = { if (!origin.params.exists(_.tpe.fold(false)(_.isInstanceOf[TsTypeUnion]))) //optimization return IArray(origin) val parameterPossibilitiesPerIndex: IArray[IArray[TsFunParam]] = origin.params.foldLeft(Empty: IArray[IArray[TsFunParam]]) { case (params, fp @ TsFunParam(_, _, Some(TsTypeUnion(types)), _)) if types.length < 10 => params :+ (types map (tpe => fp.copy(tpe = Some(tpe)))) case (params, normalParam) => params :+ IArray(normalParam) } val count = parameterPossibilitiesPerIndex.foldLeft(1)(_ * _.length) if (count > 20) IArray(origin) else generateNewSignatures(origin, IArray(Empty), parameterPossibilitiesPerIndex) } @tailrec private def generateNewSignatures( origin: TsFunSig, newParamss: IArray[IArray[TsFunParam]], remaining: IArray[IArray[TsFunParam]], ): IArray[TsFunSig] = remaining match { case Empty => newParamss.map(currents => origin.copy(params = currents)) case IArray.headTail(heads, tail) => generateNewSignatures( origin, heads.flatMap(head => newParamss.map(_ :+ head)), tail, ) } }
Example 94
Source File: MergeSort.scala From functional-way with GNU General Public License v3.0 | 5 votes |
package divideandconquer import scala.annotation.tailrec def mergeSort(array : Array[Int]) : Array[Int] = array match case Array() => array //empty array case Array(_) => array //array with 1 element case _ => //else val midPoint = array.length/2 val leftArray = mergeSort(array.take(midPoint)) val rightArray = mergeSort(array.drop(midPoint)) merge(leftArray, rightArray) @tailrec def merge(array1 : Array[Int], array2 : Array[Int], result : Array[Int] = Array.emptyIntArray) : Array[Int] = (array1, array2) match case (Array(),Array()) => result //Both arrays empty case (Array(),b) => result ++ b //array1 empty case (a,Array()) => result ++ a //array2 empty case (a,b) => //array1 & array2 non-empty val (firstElement,a1, b1) = if(a.head < b. head) (a.head,a.tail,b) else (b.head, b.tail, a) merge(a1,b1, result :+ firstElement) @main def mergeSorter = val array = Array(1,2,4,2,100,44,1,2,1000,999,47) println(mergeSort(array).mkString(" "))
Example 95
Source File: CCGraphXDriver.scala From connected-component with MIT License | 5 votes |
package com.kwartile.lib.cc import org.apache.spark.graphx.{Edge, Graph} import org.apache.spark.{SparkConf, SparkContext} import scala.annotation.tailrec object CCGraphXDriver { @tailrec private def buildEdges(node: Long, neighbors:List[Long], partialPairs: List[Edge[Int]]) : List[Edge[Int]] = { if (neighbors.length == 0) { if (partialPairs != null) List(Edge(node, node, 1)) ::: partialPairs else List(Edge(node, node, 1)) } else if (neighbors.length == 1) { val neighbor = neighbors(0) if (node > neighbor) if (partialPairs != null) List(Edge(node, neighbor, 1)) ::: partialPairs else List(Edge(node, neighbor, 1)) else if (partialPairs != null) List(Edge(neighbor, node, 1)) ::: partialPairs else List(Edge(neighbor, node, 1)) } else { val newPartialPairs = neighbors.map(neighbor => { if (node > neighbor) List(Edge(node, neighbor, 1)) else List(Edge(neighbor, node, 1)) }).flatMap(x=>x) if (partialPairs != null) buildEdges(neighbors.head, neighbors.tail, newPartialPairs ::: partialPairs) else buildEdges(neighbors.head, neighbors.tail, newPartialPairs) } } private def buildEdges(nodes:List[Long]) : List[Edge[Int]] = { buildEdges(nodes.head, nodes.tail, null.asInstanceOf[List[Edge[Int]]]) } def main(args: Array[String]) = { val sparkConf = new SparkConf().setAppName("GraphXConnectedComponent") val sc = new SparkContext(sparkConf) val cliqueFile = args(0) val cliquesRec = sc.textFile(args(0)) val cliques = cliquesRec.map(x => { val nodes = x.split("\\s+").map(y => y.toLong).toList nodes }) val edges = cliques.map(aClique => { buildEdges(aClique) }).flatMap(x=>x) val graph = Graph.fromEdges(edges, 1) val cc = graph.connectedComponents().vertices println ("Count of Connected component: " + cc.count) } }
Example 96
Source File: CalculateMedian.scala From akka_streams_tutorial with MIT License | 5 votes |
package sample.stream import java.util.concurrent.ThreadLocalRandom import akka.actor.ActorSystem import akka.stream.ThrottleMode import akka.stream.scaladsl.Source import scala.annotation.tailrec import scala.concurrent.duration._ //noinspection LanguageFeature object CalculateMedian { implicit val system = ActorSystem("CalculateMedian") implicit val ec = system.dispatcher def main(args: Array[String]) = { val maxRandomNumber = 100 val source = Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current().nextDouble(maxRandomNumber))) source .throttle(1, 10.millis, 1, ThrottleMode.shaping) .groupedWithin(100, 1.second) //.map{each => println(each); each} .map(each => medianOfMedians(each.toArray)) .runForeach(result => println(s"Median of Median (grouped by 5) over the last 100 elements: $result")) .onComplete(_ => system.terminate()) } @tailrec def findKMedian(arr: Array[Double], k: Int)(implicit choosePivot: Array[Double] => Double): Double = { val a = choosePivot(arr) val (s, b) = arr partition (a >) if (s.length == k) a // The following test is used to avoid infinite repetition else if (s.isEmpty) { val (s, b) = arr partition (a ==) if (s.length > k) a else findKMedian(b, k - s.length) } else if (s.length < k) findKMedian(b, k - s.length) else findKMedian(s, k) } def medianUpTo5(five: Array[Double]): Double = { def order2(a: Array[Double], i: Int, j: Int) = { if (a(i) > a(j)) { val t = a(i); a(i) = a(j); a(j) = t } } def pairs(a: Array[Double], i: Int, j: Int, k: Int, l: Int) = { if (a(i) < a(k)) { order2(a, j, k); a(j) } else { order2(a, i, l); a(i) } } if (five.length < 2) { return five(0) } order2(five, 0, 1) if (five.length < 4) return if (five.length == 2 || five(2) < five(0)) five(0) else if (five(2) > five(1)) five(1) else five(2) order2(five, 2, 3) if (five.length < 5) pairs(five, 0, 1, 2, 3) else if (five(0) < five(2)) { order2(five, 1, 4); pairs(five, 1, 4, 2, 3) } else { order2(five, 3, 4); pairs(five, 0, 1, 3, 4) } } def medianOfMedians(arr: Array[Double]): Double = { val medians = arr grouped 5 map medianUpTo5 toArray; if (medians.length <= 5) medianUpTo5(medians) else medianOfMedians(medians) } }
Example 97
Source File: Replica.scala From Principles-of-Reactive-Programming with GNU General Public License v3.0 | 5 votes |
package kvstore import akka.actor.{ OneForOneStrategy, Props, ActorRef, Actor } import kvstore.Arbiter._ import scala.collection.immutable.Queue import akka.actor.SupervisorStrategy.Restart import scala.annotation.tailrec import akka.pattern.{ ask, pipe } import akka.actor.Terminated import scala.concurrent.duration._ import akka.actor.PoisonPill import akka.actor.OneForOneStrategy import akka.actor.SupervisorStrategy import akka.util.Timeout object Replica { sealed trait Operation { def key: String def id: Long } case class Insert(key: String, value: String, id: Long) extends Operation case class Remove(key: String, id: Long) extends Operation case class Get(key: String, id: Long) extends Operation sealed trait OperationReply case class OperationAck(id: Long) extends OperationReply case class OperationFailed(id: Long) extends OperationReply case class GetResult(key: String, valueOption: Option[String], id: Long) extends OperationReply def props(arbiter: ActorRef, persistenceProps: Props): Props = Props(new Replica(arbiter, persistenceProps)) } class Replica(val arbiter: ActorRef, persistenceProps: Props) extends Actor { import Replica._ import Replicator._ import Persistence._ import context.dispatcher val replica: Receive = { case _ => } }
Example 98
Source File: SPTree.scala From spark-tsne with Apache License 2.0 | 5 votes |
package com.github.saurfang.spark.tsne.tree import breeze.linalg._ import breeze.numerics._ import scala.annotation.tailrec class SPTree private[tree](val dimension: Int, val corner: DenseVector[Double], val width: DenseVector[Double]) extends Serializable { private[this] val childWidth: DenseVector[Double] = width :/ 2.0 lazy val radiusSq: Double = sum(pow(width, 2)) private[tree] val totalMass: DenseVector[Double] = DenseVector.zeros(dimension) private var count: Int = 0 private var leaf: Boolean = true val center: DenseVector[Double] = DenseVector.zeros(dimension) lazy val children: Array[SPTree] = { (0 until pow(2, dimension)).toArray.map { i => val bits = DenseVector(s"%0${dimension}d".format(i.toBinaryString.toInt).toArray.map(_.toDouble - '0'.toDouble)) val childCorner: DenseVector[Double] = corner + (bits :* childWidth) new SPTree(dimension, childCorner, childWidth) } } final def insert(vector: DenseVector[Double], finalize: Boolean = false): SPTree = { totalMass += vector count += 1 if(leaf) { if(count == 1) { // first to leaf center := vector } else if(!vector.equals(center)) { (1 until count).foreach(_ => getCell(center).insert(center, finalize)) //subdivide leaf = false } } if(finalize) computeCenter(false) if(leaf) this else getCell(vector).insert(vector, finalize) } def computeCenter(recursive: Boolean = true): Unit = { if(count > 0) { center := totalMass / count.toDouble if(recursive) children.foreach(_.computeCenter()) } } def getCell(vector: DenseVector[Double]): SPTree = { val idx = ((vector - corner) :/ childWidth).data children(idx.foldLeft(0)((acc, i) => acc * 2 + min(max(i.ceil.toInt - 1, 0), 1))) } def getCount: Int = count def isLeaf: Boolean = leaf } object SPTree { def apply(Y: DenseMatrix[Double]): SPTree = { val d = Y.cols val minMaxs = minMax(Y(::, *)).t val mins = minMaxs.mapValues(_._1) val maxs = minMaxs.mapValues(_._2) val tree = new SPTree(Y.cols, mins, maxs - mins) // insert points but wait till end to compute all centers //Y(*, ::).foreach(tree.insert(_, finalize = false)) (0 until Y.rows).foreach(i => tree.insert(Y(i, ::).t, finalize = false)) // compute all center of mass tree.computeCenter() tree } }
Example 99
Source File: Execution.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.libs.iteratee import java.util.ArrayDeque import scala.annotation.tailrec import scala.concurrent.{ ExecutionContextExecutor, ExecutionContext } @tailrec private def executeScheduled(): Unit = { local.get match { case Empty => // Nothing to run () case next: Runnable => // Mark the queue of Runnables after this one as empty local.set(Empty) // Run the only scheduled Runnable next.run() // Recurse in case more Runnables were added executeScheduled() case arrayDeque: ArrayDeque[_] => val runnables = arrayDeque.asInstanceOf[ArrayDeque[Runnable]] // Rather than recursing, we can use a more efficient // while loop. The value of the ThreadLocal will stay as // an ArrayDeque until all the scheduled Runnables have been // run. while (!runnables.isEmpty) { val runnable = runnables.removeFirst() runnable.run() } case illegal => throw new IllegalStateException(s"Unsupported trampoline ThreadLocal value: $illegal") } } def reportFailure(t: Throwable): Unit = t.printStackTrace() } }
Example 100
Source File: NonBlockingMutex.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.libs.concurrent import scala.annotation.tailrec import java.util.concurrent.atomic.AtomicReference def exclusive(body: => Unit): Unit = { schedule(() => body) } private type Op = () => Unit private val state = new AtomicReference[Vector[Op]](null) @tailrec private def schedule(op: Op): Unit = { val prevState = state.get val newState = prevState match { case null => Vector.empty // This is very cheap because Vector.empty is only allocated once case pending => pending :+ op } if (state.compareAndSet(prevState, newState)) { prevState match { case null => // We've update the state to say that we're running an op, // so we need to actually start it running. executeAll(op) case _ => } } else schedule(op) // Try again } @tailrec private def executeAll(op: Op): Unit = { op.apply() val nextOp = dequeueNextOpToExecute() nextOp match { case None => () case Some(op) => executeAll(op) } } @tailrec private def dequeueNextOpToExecute(): Option[Op] = { val prevState = state.get val (newState, nextOp) = prevState match { case null => throw new IllegalStateException("When executing, must have a queue of pending elements") case pending if pending.isEmpty => (null, None) case pending => (pending.tail, Some(pending.head)) } if (state.compareAndSet(prevState, newState)) nextOp else dequeueNextOpToExecute() } }
Example 101
Source File: VectorTime.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate import scala.annotation.tailrec import scalaz._ import Scalaz._ def gt(that: VectorTime): Boolean = >(that) override def toString: String = s"VectorTime(${value.mkString(",")})" } object VectorTime { val Zero: VectorTime = VectorTime() def apply(entries: (String, Long)*): VectorTime = VectorTime(Map(entries: _*)) val Ordering = new PartialOrdering[VectorTime] { def lteq(x: VectorTime, y: VectorTime): Boolean = { tryCompare(x, y) match { case None => false case Some(r) if r > 0 => false case other => true } } def tryCompare(x: VectorTime, y: VectorTime): Option[Int] = { val xValue = x.value.withDefaultValue(0L) val yValue = y.value.withDefaultValue(0L) @tailrec def go(keys: List[String], current: Long): Option[Long] = keys match { case Nil => Some(current) case k :: ks => val s = math.signum(xValue(k) - yValue(k)) if (current == 0) go(ks, s) else if (current == -1) if (s == +1) None else go(ks, current) else // current == +1 if (s == -1) None else go(ks, current) } go(xValue.keySet.union(yValue.keySet).toList, 0).map(_.toInt) } } }
Example 102
Source File: LeveldbDeletionActor.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.log.leveldb import java.io.Closeable import akka.actor.Actor import akka.actor.PoisonPill import akka.actor.Props import com.rbmhtechnology.eventuate.log.leveldb.LeveldbEventLog._ import org.iq80.leveldb.DB import org.iq80.leveldb.ReadOptions import org.iq80.leveldb.WriteOptions import scala.annotation.tailrec import scala.concurrent.Promise private object LeveldbDeletionActor { case object DeleteBatch def props(leveldb: DB, leveldbReadOptions: ReadOptions, leveldbWriteOptions: WriteOptions, batchSize: Int, toSequenceNr: Long, promise: Promise[Unit]): Props = Props(new LeveldbDeletionActor(leveldb, leveldbReadOptions, leveldbWriteOptions, batchSize, toSequenceNr, promise)) } private class LeveldbDeletionActor( val leveldb: DB, val leveldbReadOptions: ReadOptions, val leveldbWriteOptions: WriteOptions, batchSize: Int, toSequenceNr: Long, promise: Promise[Unit]) extends Actor with WithBatch { import LeveldbDeletionActor._ val eventKeyIterator: CloseableIterator[EventKey] = newEventKeyIterator override def preStart() = self ! DeleteBatch override def postStop() = eventKeyIterator.close() override def receive = { case DeleteBatch => withBatch { batch => eventKeyIterator.take(batchSize).foreach { eventKey => batch.delete(eventKeyBytes(eventKey.classifier, eventKey.sequenceNr)) } } if (eventKeyIterator.hasNext) { self ! DeleteBatch } else { promise.success(()) self ! PoisonPill } } private def newEventKeyIterator: CloseableIterator[EventKey] = { new Iterator[EventKey] with Closeable { val iterator = leveldb.iterator(leveldbReadOptions.snapshot(leveldb.getSnapshot)) iterator.seek(eventKeyBytes(EventKey.DefaultClassifier, 1L)) @tailrec override def hasNext: Boolean = { val key = eventKey(iterator.peekNext().getKey) key != eventKeyEnd && (key.sequenceNr <= toSequenceNr || { iterator.seek(eventKeyBytes(key.classifier + 1, 1L)) hasNext }) } override def next() = eventKey(iterator.next().getKey) override def close() = { iterator.close() leveldbReadOptions.snapshot().close() } } } }
Example 103
Source File: FilteringGlobalExclusionDependencyResolver.scala From exodus with MIT License | 5 votes |
package com.wixpress.build.maven import scala.annotation.tailrec class FilteringGlobalExclusionDependencyResolver(resolver: MavenDependencyResolver, globalExcludes: Set[Coordinates]) extends MavenDependencyResolver { val filterer = new GlobalExclusionFilterer(globalExcludes) override def managedDependenciesOf(artifact: Coordinates): List[Dependency] = resolver.managedDependenciesOf(artifact) override def dependencyClosureOf(baseDependencies: List[Dependency], withManagedDependencies: List[Dependency], ignoreMissingDependenciesFlag: Boolean = true): Set[DependencyNode] = filterer.filterGlobalsFromDependencyNodes(resolver.dependencyClosureOf(baseDependencies, withManagedDependencies)) override def directDependenciesOf(coordinates: Coordinates): List[Dependency] = { filterGlobalsFromDependencies(coordinates, resolver.directDependenciesOf(coordinates)).distinct } private def filterGlobalsFromDependencies(coordinates: Coordinates, dependencies: List[Dependency]): List[Dependency] = { val excludedDependencies = dependencies.filter(excluded) val nodes: Set[DependencyNode] = if (excludedDependencies.isEmpty) Set.empty else { val managedDependencies = resolver.managedDependenciesOf(coordinates) resolver.dependencyClosureOf(excludedDependencies, managedDependencies) } filterer.filterGlobalsFromDependencies(dependencies, nodes).distinct } private def excluded(dependency: Dependency) = { globalExcludes.exists(dependency.coordinates.equalsOnGroupIdAndArtifactId) } } class GlobalExclusionFilterer(globalExcludes: Set[Coordinates]) { def filterGlobalsFromDependencyNodes(dependencyNodes: Set[DependencyNode]): Set[DependencyNode] = { dependencyNodes.filterNot(dependencyNode => excluded(dependencyNode.baseDependency)) .map(filterGlobalsFromDependencies(dependencyNodes)) } private def filterGlobalsFromDependencies(dependencyNodes: Set[DependencyNode])(depNode: DependencyNode): DependencyNode = depNode.copy(dependencies = filterGlobalsFromDependencies(depNode.dependencies.toList, dependencyNodes).toSet) @tailrec final def filterGlobalsFromDependencies(dependencies: List[Dependency], dependencyGraph: Set[DependencyNode]): List[Dependency] = { val (excludedDependencies, includedDependencies) = dependencies.partition(excluded) if (excludedDependencies.isEmpty) includedDependencies else { val transitiveDependencies = excludedDependencies .flatMap(retainTransitiveDependencies(dependencyGraph)) .map(preferOriginalVersionAsFoundIn(includedDependencies)) filterGlobalsFromDependencies(includedDependencies ++ transitiveDependencies, dependencyGraph) } } private def preferOriginalVersionAsFoundIn(previouslyFoundDependencies: List[Dependency])(newlyFoundDependency: Dependency) = { val resolvedVersion = previouslyFoundDependencies .find(_.coordinates.equalsIgnoringVersion(newlyFoundDependency.coordinates)) .map(_.version) .getOrElse(newlyFoundDependency.coordinates.version) newlyFoundDependency.withVersion(resolvedVersion) } private def retainTransitiveDependencies(dependencyGraph: Set[DependencyNode])(excluded: Dependency): Set[Dependency] = { val originalScope = excluded.scope // hard assumption that excluded dependency is in the given nodes dependencyGraph.find(_.baseDependency.coordinates.equalsIgnoringVersion(excluded.coordinates)) .getOrElse(throw new RuntimeException(s"Could not find dependency node for the excluded ${excluded.coordinates}")) .dependencies .map(updateDependencyScopeAccordingTo(originalScope)) } private def updateDependencyScopeAccordingTo(originalScope: MavenScope)(transitive: Dependency) = { val transitiveScope = transitive.scope val newScope = (originalScope, transitiveScope) match { case (MavenScope.Compile, MavenScope.Runtime) => MavenScope.Runtime case (scope, _) => scope } transitive.copy(scope = newScope) } private def excluded(dependency: Dependency) = { globalExcludes.exists(dependency.coordinates.equalsOnGroupIdAndArtifactId) } }
Example 104
Source File: PurchaseLogGenerator.scala From gihyo-spark-book-example with Apache License 2.0 | 5 votes |
package jp.gihyo.spark.ch08 import scala.annotation.tailrec object PurchaseLogGenerator { // 商品ノードのデータを作成する関数 def genProductList(implicit recOpts: RecommendLogOptions): List[VertexProperty] = (1 to recOpts.numProducts).map { id => VertexProperty(id.toLong, "product", s"product${id}") }.toList // ユーザノードのデータを作成する関数 def genUserList(implicit recOpts: RecommendLogOptions): List[VertexProperty] = (1 to recOpts.numUsers).map { // ユーザノードの頂点IDは、商品ノードのIDとかぶらないよう、商品数で底上げする id => VertexProperty((recOpts.numProducts + id).toLong, "user", s"user${id}") }.toList // 引数で受け取ったユーザのリストに対して、 genUserPurchaseLog を再帰的に適用する関数 @tailrec def genPurchaseLog(users: List[VertexProperty], purchaseLog: List[Purchase] = Nil) (implicit recOpts: RecommendLogOptions, pidGenerator: ProductIdGenerator): List[Purchase] = { users match { case Nil => purchaseLog.reverse case _ => genPurchaseLog(users.tail, genUserPurchaseLog(users.head, purchaseLog)) } } @tailrec private def genUserPurchaseLog( user: VertexProperty, purchaseLog: List[Purchase], products: Set[Long] = Set() )(implicit recOpts: RecommendLogOptions, pidGenerator: ProductIdGenerator): List[Purchase] = { products.size match { case recOpts.numProductsPerUser => products.map(pid => Purchase(user.id, pid)).toList ++ purchaseLog case _ => genUserPurchaseLog( user, purchaseLog, products + pidGenerator.getNextPid(recOpts, purchaseLog)) } } }
Example 105
Source File: StaxXmlParserUtils.scala From spark-xml with Apache License 2.0 | 5 votes |
package com.databricks.spark.xml.parsers import java.io.StringReader import javax.xml.stream.{EventFilter, XMLEventReader, XMLInputFactory, XMLStreamConstants} import javax.xml.stream.events._ import scala.annotation.tailrec import scala.collection.JavaConverters._ import com.databricks.spark.xml.XmlOptions private[xml] object StaxXmlParserUtils { private val factory: XMLInputFactory = { val factory = XMLInputFactory.newInstance() factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false) factory.setProperty(XMLInputFactory.IS_COALESCING, true) factory } def filteredReader(xml: String): XMLEventReader = { val filter = new EventFilter { override def accept(event: XMLEvent): Boolean = // Ignore comments and processing instructions event.getEventType match { case XMLStreamConstants.COMMENT | XMLStreamConstants.PROCESSING_INSTRUCTION => false case _ => true } } // It does not have to skip for white space, since `XmlInputFormat` // always finds the root tag without a heading space. val eventReader = factory.createXMLEventReader(new StringReader(xml)) factory.createFilteredReader(eventReader, filter) } def gatherRootAttributes(parser: XMLEventReader): Array[Attribute] = { val rootEvent = StaxXmlParserUtils.skipUntil(parser, XMLStreamConstants.START_ELEMENT) rootEvent.asStartElement.getAttributes.asScala.map(_.asInstanceOf[Attribute]).toArray } def skipChildren(parser: XMLEventReader): Unit = { var shouldStop = checkEndElement(parser) while (!shouldStop) { parser.nextEvent match { case _: StartElement => val e = parser.peek if (e.isCharacters && e.asCharacters.isWhiteSpace) { // There can be a `Characters` event between `StartElement`s. // So, we need to check further to decide if this is a data or just // a whitespace between them. parser.next } if (parser.peek.isStartElement) { skipChildren(parser) } case _: EndElement => shouldStop = checkEndElement(parser) case _: XMLEvent => // do nothing } } } }
Example 106
Source File: QuickNearestNeighbor.scala From watr-works with Apache License 2.0 | 5 votes |
package edu.umass.cs.iesl.watr package utils import ExactFloats._ object QuickNearestNeighbors { import TypeTags._ import utils.SlicingAndDicing._ import scala.annotation.tailrec case class Bin( centroid: DataPoint, neighbors: Seq[DataPoint] ) { def size(): Int = neighbors.map(_.len).sum + centroid.len def range(): (Int@@FloatRep, Int@@FloatRep) = { val values = (centroid.value.unwrap +: neighbors.map(_.value.unwrap)) (FloatRep(values.min), FloatRep(values.max)) } def maxValue(): Int@@FloatRep = { val max = (centroid.value.unwrap +: neighbors.map(_.value.unwrap)).max FloatRep(max) } def minValue(): Int@@FloatRep = { val min = (centroid.value.unwrap +: neighbors.map(_.value.unwrap)).min FloatRep(min) } override def toString(): String = { val cstr = centroid.toString() val nstr = neighbors.map(_.toString()).mkString(", ") s"{size=${size()}: ${cstr} + [ ${nstr} ]}" } def toCentroidRangeString(): String = { val r = range() s"{n=${size()}: (${r._1.pp}-${r._2.pp})}" } } case class DataPoint(value: Int@@FloatRep, len: Int) { override def toString(): String = { val cpp = value.pp val clen = len s"[${cpp} x ${clen}]" } } def qnn(in: Seq[Int@@FloatRep], tolerance: Double = 0.5d): Seq[Bin] = { @tailrec def loop(init: List[(Int@@FloatRep, Int)], groups: List[Bin]): List[Bin] = { if (init.isEmpty) groups else { val (dmax, dmaxCount) = init.head val (grouped, others) = init.tail.partition { case (d, dcount) => val inRange = d - tolerance < dmax && dmax < d + tolerance inRange } val bin = Bin( DataPoint(dmax, dmaxCount), grouped.map(g => DataPoint(g._1, g._2)) ) loop(others, bin::groups) } } val distsSortByCount: List[(Int@@FloatRep, Int)] = in .sorted.toList .groupByPairs(_ == _) .map(p => (p.head, p.length)) .sortBy(_._2) .reverse .toList val binned = loop(distsSortByCount, List()) binned.sortBy(_.size()).reverse } }
Example 107
Source File: IterativeBroadcastJoin.scala From iterative-broadcast-join with Apache License 2.0 | 5 votes |
package com.godatadriven.join import com.godatadriven.SparkUtil import com.godatadriven.common.Config import org.apache.spark.sql.functions._ import org.apache.spark.sql.{DataFrame, SparkSession} import scala.annotation.tailrec object IterativeBroadcastJoin extends JoinStrategy { @tailrec private def iterativeBroadcastJoin(spark: SparkSession, result: DataFrame, broadcast: DataFrame, iteration: Int = 0): DataFrame = if (iteration < Config.numberOfBroadcastPasses) { val tableName = s"tmp_broadcast_table_itr_$iteration.parquet" val out = result.join( broadcast.filter(col("pass") === lit(iteration)), Seq("key"), "left_outer" ).select( result("key"), // Join in the label coalesce( result("label"), broadcast("label") ).as("label") ) SparkUtil.dfWrite(out, tableName) iterativeBroadcastJoin( spark, SparkUtil.dfRead(spark, tableName), broadcast, iteration + 1 ) } else result override def join(spark: SparkSession, dfLarge: DataFrame, dfMedium: DataFrame): DataFrame = { broadcast(dfMedium) iterativeBroadcastJoin( spark, dfLarge .select("key") .withColumn("label", lit(null)), dfMedium ) } }
Example 108
Source File: ReplaceExceptWithFilter.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.catalyst.optimizer import scala.annotation.tailrec import org.apache.spark.sql.catalyst.expressions._ import org.apache.spark.sql.catalyst.plans.logical._ import org.apache.spark.sql.catalyst.rules.Rule object ReplaceExceptWithFilter extends Rule[LogicalPlan] { def apply(plan: LogicalPlan): LogicalPlan = { if (!plan.conf.replaceExceptWithFilter) { return plan } plan.transform { case e @ Except(left, right, false) if isEligible(left, right) => val filterCondition = combineFilters(skipProject(right)).asInstanceOf[Filter].condition if (filterCondition.deterministic) { transformCondition(left, filterCondition).map { c => Distinct(Filter(Not(c), left)) }.getOrElse { e } } else { e } } } private def transformCondition(plan: LogicalPlan, condition: Expression): Option[Expression] = { val attributeNameMap: Map[String, Attribute] = plan.output.map(x => (x.name, x)).toMap if (condition.references.forall(r => attributeNameMap.contains(r.name))) { val rewrittenCondition = condition.transform { case a: AttributeReference => attributeNameMap(a.name) } // We need to consider as False when the condition is NULL, otherwise we do not return those // rows containing NULL which are instead filtered in the Except right plan Some(Coalesce(Seq(rewrittenCondition, Literal.FalseLiteral))) } else { None } } // TODO: This can be further extended in the future. private def isEligible(left: LogicalPlan, right: LogicalPlan): Boolean = (left, right) match { case (_, right @ (Project(_, _: Filter) | Filter(_, _))) => verifyConditions(left, right) case _ => false } private def verifyConditions(left: LogicalPlan, right: LogicalPlan): Boolean = { val leftProjectList = projectList(left) val rightProjectList = projectList(right) left.output.size == left.output.map(_.name).distinct.size && left.find(_.expressions.exists(SubqueryExpression.hasSubquery)).isEmpty && right.find(_.expressions.exists(SubqueryExpression.hasSubquery)).isEmpty && Project(leftProjectList, nonFilterChild(skipProject(left))).sameResult( Project(rightProjectList, nonFilterChild(skipProject(right)))) } private def projectList(node: LogicalPlan): Seq[NamedExpression] = node match { case p: Project => p.projectList case x => x.output } private def skipProject(node: LogicalPlan): LogicalPlan = node match { case p: Project => p.child case x => x } private def nonFilterChild(plan: LogicalPlan) = plan.find(!_.isInstanceOf[Filter]).getOrElse { throw new IllegalStateException("Leaf node is expected") } private def combineFilters(plan: LogicalPlan): LogicalPlan = { @tailrec def iterate(plan: LogicalPlan, acc: LogicalPlan): LogicalPlan = { if (acc.fastEquals(plan)) acc else iterate(acc, CombineFilters(acc)) } iterate(plan, CombineFilters(plan)) } }
Example 109
Source File: binaryExpressions.scala From HANAVora-Extensions with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.catalyst.expressions import org.apache.spark.sql.types._ import scala.annotation.tailrec private[sql] object NumericLiteral { def unapply(a: Any): Option[BigDecimal] = a match { case Literal(value: Int, _) => Some(value) case Literal(value: Long, _) => Some(value) case Literal(value: Double, _) => Some(value) case Literal(value: Float, _) => Some(value.toDouble) case Literal(value: Short, _) => Some(value.toInt) case Literal(value: Decimal, _) => Some(value.toBigDecimal) case Literal(value: java.math.BigDecimal, _) => Some(value) case Literal(value: BigDecimal, _) => Some(value) case _ => None } } // TODO optimize this. maybe we can substitute it completely with its logic. object BinarySymbolExpression { def isBinaryExpressionWithSymbol(be: BinaryExpression): Boolean = be.isInstanceOf[BinaryArithmetic] || be.isInstanceOf[BinaryComparison] def getBinaryExpressionSymbol(be: BinaryExpression): String = be match { case be: BinaryComparison => be.symbol case be:BinaryArithmetic => be.symbol case _ => sys.error(s"${be.getClass.getName} has no symbol attribute") } def unapply(any: Any): Option[(Expression, String, Expression)] = any match { case be: BinaryExpression if isBinaryExpressionWithSymbol(be) => Some(be.left, getBinaryExpressionSymbol(be), be.right) case _ => None } }
Example 110
Source File: SQLRunner.scala From HANAVora-Extensions with Apache License 2.0 | 5 votes |
package com.sap.spark.cli import java.io._ import org.apache.spark.sql.{DataFrame, Row, SQLContext} import org.apache.spark.{Logging, SparkContext} import scala.annotation.tailrec protected[cli] case class CLIOptions( sqlFiles: List[String] = Nil, output: Option[String] = None) def main(args: Array[String]): Unit = { def fail(msg: String = USAGE): Unit = { logError(msg) System.exit(1) } val opts = parseOpts(args.toList) val outputStream: OutputStream = opts.output match { case Some(filename) => new FileOutputStream(new File(filename)) case None => System.out } opts.sqlFiles .map((string: String) => new FileInputStream(new File(string))) .foreach(sql(_, outputStream)) } }
Example 111
Source File: Chunk.scala From paiges with Apache License 2.0 | 5 votes |
package org.typelevel.paiges import scala.annotation.tailrec private[paiges] object Chunk { if (fits(pos, first)) first else loop(pos, (i, y) :: z) } def cheat(pos: Int, lst: List[(Int, Doc)]) = loop(pos, lst) val stream = loop(0, (0, d) :: Nil) if (trim) new TrimChunkIterator(stream) else new ChunkIterator(stream) } //$COVERAGE-OFF$ // code of the form `final val x = ...` is inlined. we never // access this field at runtime, but it is still used. final private[this] val indentMax = 100 //$COVERAGE-ON$ private[this] def makeIndentStr(i: Int): String = "\n" + (" " * i) private[this] val indentTable: Array[String] = (0 to indentMax).iterator .map(makeIndentStr) .toArray def lineToStr(indent: Int): String = if (indent <= indentMax) indentTable(indent) else makeIndentStr(indent) }
Example 112
Source File: RangeLookup.scala From maha with Apache License 2.0 | 5 votes |
// Copyright 2017, Yahoo Holdings Inc. // Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms. package com.yahoo.maha.core.lookup import scala.annotation.tailrec trait Range[T] { def from: T def to: T def inRange(value: T) : Boolean def isBefore(value: T) : Boolean } trait RangeInclusive[T] extends Range[T] { require(inRange(from)) require(inRange(to)) } case class LongRange(from: Long, to: Long) extends RangeInclusive[Long] { require(from < to) def inRange(value: Long) : Boolean = value >= from && value <= to def isBefore(value: Long) : Boolean = value < from } object RangeLookup { def binarySearch[T, U <: RangeInclusive[T], V](key: T, list: IndexedSeq[(U, V)]): Option[V] = { @tailrec def go(lo: Int, hi: Int): Option[V] = { if (lo > hi) None else { val mid: Int = lo + (hi - lo) / 2 val elem = list(mid) if(elem._1.inRange(key)) { Option(elem._2) } else { if(elem._1.isBefore(key)) { go(lo, mid - 1) } else { go(mid + 1, hi) } } } } go(0, list.size - 1) } } trait RangeLookup[T, U <: RangeInclusive[T], V] { val list : IndexedSeq[(U, V)] def find(key: T) : Option[V] = RangeLookup.binarySearch(key, list) } class LongRangeLookup[V] private[lookup](val list: IndexedSeq[(LongRange, V)]) extends RangeLookup[Long, LongRange, V] object LongRangeLookup { def apply[V](list: IndexedSeq[(LongRange, V)]) : LongRangeLookup[V] = { val builder = new LongRangeLookupBuilder[V] list.foreach { case (range, value) => builder.add(range, value) } builder.build } def full[V](value : V) : LongRangeLookup[V] = { val builder = new LongRangeLookupBuilder[V] builder.add(LongRange(0, Long.MaxValue), value) builder.build } } class LongRangeLookupBuilder[V] { val list = new collection.mutable.ArrayBuffer[(LongRange, V)]() def add(range: LongRange, value: V) : LongRangeLookupBuilder[V] = { val fromExists = RangeLookup.binarySearch(range.from, list) require(fromExists.isEmpty, s"range overlap, existing range : ${fromExists.get} , new range : $range") val toExists = RangeLookup.binarySearch(range.to, list) require(toExists.isEmpty, s"range overlap, existing range : ${toExists.get} , new range : $range") list += range -> value this } def build : LongRangeLookup[V] = { new LongRangeLookup(list.toIndexedSeq) } }
Example 113
Source File: MerkleTree.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db.merkletrees import scala.annotation.tailrec import scala.util.Try case class MerkleNodeId(id: Int) extends AnyVal sealed trait MerkleTree { def nodeId: MerkleNodeId def digest: Digest } case class MerkleHashNode(nodeId: MerkleNodeId, digest: Digest, left: MerkleTree, right: MerkleTree) extends MerkleTree case class MerkleLeaf(nodeId: MerkleNodeId, digest: Digest) extends MerkleTree object MerkleTree { def unapply(blocks: Seq[Block])(implicit ev: MerkleDigest[Block]): Option[MerkleTree] = unapply(blocks.toArray) def unapply(blocks: Array[Block])(implicit ev: MerkleDigest[Block]): Option[MerkleTree] = { sealed trait TempMerkleTree { def digest: Digest } case class TempMerkleHashNode(digest: Digest, left: TempMerkleTree, right: TempMerkleTree) extends TempMerkleTree case class TempMerkleLeaf(digest: Digest) extends TempMerkleTree def blockToLeaf(b: Block): TempMerkleLeaf = TempMerkleLeaf(ev.digest(b)) def buildTree(blocks: Array[Block]) = Try { val leafs = blocks.map(blockToLeaf) var trees: Seq[TempMerkleTree] = leafs.toSeq while (trees.length > 1) { trees = trees.grouped(2) .map(x => mergeTrees(x(0), x(1))) .toSeq } trees.head } def mergeTrees(n1: TempMerkleTree, n2: TempMerkleTree) = { val mergedDigest = n1.digest + n2.digest val hash = ev.digest(mergedDigest.hash) TempMerkleHashNode(hash, n1, n2) } def toFinalForm(tmt: TempMerkleTree): MerkleTree = { var counter = -1 def toMerkle(mt: TempMerkleTree): MerkleTree = { counter += 1 mt match { case TempMerkleHashNode(digest, left, right) => MerkleHashNode(MerkleNodeId(counter), digest, toMerkle(left), toMerkle(right)) case TempMerkleLeaf(digest) => MerkleLeaf(MerkleNodeId(counter), digest) } } toMerkle(tmt) } buildTree(blocks ++ zeroed(blocks)) .toOption .map(toFinalForm) } def zeroed(blocks: Seq[Block]): Array[Array[Byte]] = { def zero(i: Int): Int = { val factor = 2 var x = factor while(x < i) x *= factor x - i } Array.fill(zero(blocks.length))(Array[Byte](0)) } @tailrec def findNode(nodeId: MerkleNodeId, merkleTree: MerkleTree): Option[MerkleTree] = { merkleTree match { case _ if merkleTree.nodeId == nodeId => Option(merkleTree) case MerkleHashNode(nId, _, _, right) if nodeId.id >= right.nodeId.id => findNode(nodeId, right) case MerkleHashNode(nId, _, left, _) => findNode(nodeId, left) case _ => None } } }
Example 114
Source File: Utils.scala From finagle-postgres with Apache License 2.0 | 5 votes |
package com.twitter.finagle.postgres.values import java.nio.charset.Charset import java.security.MessageDigest import io.netty.buffer.ByteBuf import scala.annotation.tailrec object Charsets { val Utf8 = Charset.forName("UTF-8") } object Buffers { @throws(classOf[IllegalArgumentException]) def encrypt(user: Array[Byte], password: Array[Byte], salt: Array[Byte]): Array[Byte] = { require(user != null && user.length > 0, "user should not be empty") require(password != null && password.length > 0, "password should not be empty") require(salt != null && salt.length > 0, "salt should not be empty") val inner = MessageDigest.getInstance("MD5") inner.update(password) inner.update(user) val outer = MessageDigest.getInstance("MD5") outer.update(Hex.valueOf(inner.digest).getBytes) outer.update(salt) ("md5" + Hex.valueOf(outer.digest)).getBytes } } object Hex { def valueOf(buf: Array[Byte]): String = buf.map("%02X" format _).mkString.toLowerCase } object Convert { def asShort(i : Int) = i.asInstanceOf[Short] } object Strings { val empty = new String }
Example 115
Source File: PlanResultInterpreter.scala From rug with GNU General Public License v3.0 | 5 votes |
package com.atomist.rug.runtime.plans import com.atomist.rug.spi.Handlers.Status.{Failure, Success} import com.atomist.rug.spi.Handlers._ import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.DurationInt object PlanResultInterpreter { def interpret(planResult: PlanResult): Response = { if (hasLogFailure(planResult.log)) { Response(Failure) } else { Response(Success) } } @tailrec def hasLogFailure(log: Seq[PlanLogEvent]): Boolean = { log.headOption match { case Some(head) => head match { case _: PlanLogError => true case result: InstructionResult if result.response.status == Failure => true case result: NestedPlanRun => val planResult = result.planResult hasLogFailure(log.tail ++ planResult.log) case _ => hasLogFailure(log.tail) } case None => false } } }
Example 116
Source File: PlanResultLogger.scala From rug with GNU General Public License v3.0 | 5 votes |
package com.atomist.rug.runtime.plans import com.atomist.rug.spi.Handlers.Status.Failure import com.atomist.rug.spi.Handlers._ import org.slf4j.Logger import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.DurationInt class PlanResultLogger(val logger: Logger) { def log(planResult: PlanResult): Unit = { logEvents(planResult.log) } @tailrec private def logEvents(log: Seq[PlanLogEvent]): Unit = { log.headOption match { case Some(head) => val remainingEvents = head match { case logError: PlanLogError => logger.error("Error running plan.", logError.error) log.tail case result: InstructionResult if result.response.status == Failure => logger.error("Failure running plan.", result) log.tail case result: NestedPlanRun => val planResult = result.planResult log.tail ++ planResult.log case _ => log.tail } logEvents(remainingEvents) case None => } } }
Example 117
Source File: JsonTransformSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalatest.FlatSpec import org.scalatest.prop.GeneratorDrivenPropertyChecks._ import play.api.libs.json._ import play.api.libs.json.scalacheck.JsValueGenerators import scala.annotation.tailrec import scala.util.Random class JsonTransformSpec extends FlatSpec with CompatibilityImplicits with JsValueGenerators { @tailrec private def verifyAllRedacted(all: Seq[(JsPath, JsValue)]): Unit = { val invalid = all collect { case (path, value) if value != JsonTransform.RedactedValue => path } assert(invalid.isEmpty, s"The following paths are invalid: ${invalid.mkString(", ")}") val nextGen = all flatMap { case (path, JsArray(items)) => items.zipWithIndex map { case (item, i) => (JsPath(path.path :+ IdxPathNode(i)), item) } case (path, JsObject(fields)) => fields map { case (k, v) => (path \ k, v) } case _ => Nil } if (nextGen.nonEmpty) { verifyAllRedacted(nextGen) } } "redactPaths" should "redact selected fields by path at the top level" in { forAll { obj: JsObject => val topLevelPaths: Seq[JsPath] = obj.fields.map(__ \ _._1) whenever(topLevelPaths.nonEmpty) { val redactedPaths: Seq[JsPath] = Random.shuffle(topLevelPaths) take Random.nextInt(topLevelPaths.size) implicit val redactor: JsonTransform[Any] = JsonTransform.redactPaths[Any](redactedPaths) val redacted = obj.transformAs[Any] // Useful for debugging // if (redactedPaths.nonEmpty) { // println(Json.prettyPrint(obj)) // println(s"with redacted paths (${redactedPaths.mkString(", ")}):") // println(Json.prettyPrint(redacted)) // } for (path <- redactedPaths) { assertResult(JsonTransform.RedactedValue) { path.asSingleJson(redacted).get } } } } } "redactAll" should "redact all fields of all paths" in { implicit val redactor: JsonTransform[Any] = JsonTransform.redactAll[Any]() forAll { obj: JsObject => val redacted = obj.transformAs[Any] verifyAllRedacted(Seq(__ -> redacted)) } } }
Example 118
Source File: JsonTransformSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalatest.flatspec.AnyFlatSpec import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import play.api.libs.json._ import play.api.libs.json.scalacheck.JsValueGenerators import scala.annotation.tailrec import scala.util.Random class JsonTransformSpec extends AnyFlatSpec with CompatibilityImplicits with JsValueGenerators { @tailrec private def verifyAllRedacted(all: Seq[(JsPath, JsValue)]): Unit = { val invalid = all collect { case (path, value) if value != JsonTransform.RedactedValue => path } assert(invalid.isEmpty, s"The following paths are invalid: ${invalid.mkString(", ")}") val nextGen = all flatMap { case (path, JsArray(items)) => items.zipWithIndex map { case (item, i) => (JsPath(path.path :+ IdxPathNode(i)), item) } case (path, JsObject(fields)) => fields map { case (k, v) => (path \ k, v) } case _ => Nil } if (nextGen.nonEmpty) { verifyAllRedacted(nextGen) } } "redactPaths" should "redact selected fields by path at the top level" in { forAll { obj: JsObject => val topLevelPaths: Seq[JsPath] = obj.fields.map(__ \ _._1) whenever(topLevelPaths.nonEmpty) { val redactedPaths: Seq[JsPath] = Random.shuffle(topLevelPaths) take Random.nextInt(topLevelPaths.size) implicit val redactor: JsonTransform[Any] = JsonTransform.redactPaths[Any](redactedPaths) val redacted = obj.transformAs[Any] // Useful for debugging // if (redactedPaths.nonEmpty) { // println(Json.prettyPrint(obj)) // println(s"with redacted paths (${redactedPaths.mkString(", ")}):") // println(Json.prettyPrint(redacted)) // } for (path <- redactedPaths) { assertResult(JsonTransform.RedactedValue) { path.asSingleJson(redacted).get } } } } } "redactAll" should "redact all fields of all paths" in { implicit val redactor: JsonTransform[Any] = JsonTransform.redactAll[Any]() forAll { obj: JsObject => val redacted = obj.transformAs[Any] verifyAllRedacted(Seq(__ -> redacted)) } } }
Example 119
Source File: JsonTransformSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalatest.flatspec.AnyFlatSpec import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import play.api.libs.json._ import play.api.libs.json.scalacheck.JsValueGenerators import scala.annotation.tailrec import scala.util.Random class JsonTransformSpec extends AnyFlatSpec with CompatibilityImplicits with JsValueGenerators { @tailrec private def verifyAllRedacted(all: Seq[(JsPath, JsValue)]): Unit = { val invalid = all collect { case (path, value) if value != JsonTransform.RedactedValue => path } assert(invalid.isEmpty, s"The following paths are invalid: ${invalid.mkString(", ")}") val nextGen = all flatMap { case (path, JsArray(items)) => items.zipWithIndex map { case (item, i) => (JsPath(path.path :+ IdxPathNode(i)), item) } case (path, JsObject(fields)) => fields map { case (k, v) => (path \ k, v) } case _ => Nil } if (nextGen.nonEmpty) { verifyAllRedacted(nextGen) } } "redactPaths" should "redact selected fields by path at the top level" in { forAll { obj: JsObject => val topLevelPaths: Seq[JsPath] = obj.fields.map(__ \ _._1).toSeq whenever(topLevelPaths.nonEmpty) { val redactedPaths: Seq[JsPath] = Random.shuffle(topLevelPaths) take Random.nextInt(topLevelPaths.size) implicit val redactor: JsonTransform[Any] = JsonTransform.redactPaths[Any](redactedPaths) val redacted = obj.transformAs[Any] // Useful for debugging // if (redactedPaths.nonEmpty) { // println(Json.prettyPrint(obj)) // println(s"with redacted paths (${redactedPaths.mkString(", ")}):") // println(Json.prettyPrint(redacted)) // } for (path <- redactedPaths) { assertResult(JsonTransform.RedactedValue) { path.asSingleJson(redacted).get } } } } } "redactAll" should "redact all fields of all paths" in { implicit val redactor: JsonTransform[Any] = JsonTransform.redactAll[Any]() forAll { obj: JsObject => val redacted = obj.transformAs[Any] verifyAllRedacted(Seq(__ -> redacted)) } } }
Example 120
Source File: events.scala From levsha with Apache License 2.0 | 5 votes |
package levsha import scala.annotation.tailrec object events { import EventPhase._ def calculateEventPropagation(target: Id, `type`: String): Seq[EventId] = { @tailrec def capture(acc: List[EventId], i: Int, v: Id): List[EventId] = { if (i == v.level) { acc } else { val id = v.take(i) val eh = EventId(id, `type`, Capturing) capture(eh :: acc, i + 1, v) } } val capturing = capture(Nil, 1, target).reverse val atTarget = EventId(target, `type`, AtTarget) val bubbling = { val xs = capturing.reverse.map(_.copy(phase = Bubbling)) EventId(target, `type`, Bubbling) :: xs } capturing ::: (atTarget :: bubbling) } sealed trait EventPhase object EventPhase { case object Capturing extends EventPhase case object AtTarget extends EventPhase case object Bubbling extends EventPhase } case class EventId(target: Id, `type`: String, phase: EventPhase) }
Example 121
Source File: IntStringMap.scala From levsha with Apache License 2.0 | 5 votes |
package levsha.impl.internal.debox import java.util import scala.annotation.tailrec import scala.{specialized => sp} import IntStringMap.Unit2 private[debox] def ofAllocatedSize(n: Int) = { val sz = nextPowerOfTwo(n) match { case n if n < 0 => throw new Exception(s"DeboxOverflowError $n")//throw DeboxOverflowError(n) case 0 => 8 case n => n } new IntStringMap(new Array(sz), new Array(sz), new Array[Byte](sz), 0, 0) } private[debox] def nextPowerOfTwo(n: Int): Int = { val x = java.lang.Integer.highestOneBit(n) if (x == n) n else x * 2 } }
Example 122
Source File: NestedJsonReads.scala From vat-api with Apache License 2.0 | 5 votes |
package utils import play.api.libs.functional.syntax._ import play.api.libs.json._ import scala.annotation.tailrec import scala.concurrent.Future trait NestedJsonReads { type ServiceResponse[T, E] = Future[Either[T, E]] implicit class JsPathOps(jsPath: JsPath) { def readNestedNullable[T](implicit rds: Reads[T]): Reads[Option[T]] = Reads[Option[T]] { json => applyTillLastNested(json).fold( jsErr => jsErr, jsRes => jsRes.fold( invalid = _ => JsSuccess(None), valid = { case JsNull => JsSuccess(None) case js => rds.reads(js).repath(jsPath).map(Some(_)) } ) ) } def applyTillLastNested(json: JsValue): Either[JsError, JsResult[JsValue]] = { def singleJsError(msg: String) = JsError(Seq(jsPath -> Seq(JsonValidationError(msg)))) @tailrec def step(pathNodes: List[PathNode], json: JsValue): Either[JsError, JsResult[JsValue]] = pathNodes match { case Nil => Left(singleJsError("error.path.empty")) case node :: Nil => node(json) match { case Nil => Right(singleJsError("error.path.missing")) case js :: Nil => Right(JsSuccess(js)) case _ :: _ => Right(singleJsError("error.path.result.multiple")) } case head :: tail => head(json) match { case Nil => Right(singleJsError("error.path.missing")) case js :: Nil => step(tail, js) case _ :: _ => Left(singleJsError("error.path.result.multiple")) } } step(jsPath.path, json) } } def emptyIfNotPresent[A: Reads](path: JsPath): Reads[Option[A]] = path.readNestedNullable[JsValue].filter(_.isEmpty).map(_ => None) or JsPath.readNullable[A] } object NestedJsonReads extends NestedJsonReads
Example 123
package benchmarks import scala.annotation.tailrec import scala.util.Random import java.util.concurrent.ExecutorService trait Gen[T] { def apply(dist: List[(Gen.Op, Int)])(implicit s: ExecutorService): T = { val depth = 100 val rnd = new Random(1) import rnd._ val values = dist.collect { case (g: Gen.Value, i) => (g, i) } val transforms = dist.collect { case (g: Gen.Transform, i) => (g, i) } require(values.nonEmpty) def choose[O <: Gen.Op](l: List[(O, Int)]): O = { @tailrec def find(n: Int, prev: Int, l: List[(O, Int)]): O = { l match { case Nil => ??? case (o, i) :: tail => if (prev + i > n) o else find(n, prev + i, tail) } } val max = l.map(_._2).sum find(nextInt(max), 0, l) } val ex = new Exception def genValue: T = choose(values) match { case Gen.Async => async(s.submit(_)) case Gen.Sync => sync case Gen.Failure => failure(ex) } def genTransform(depth: Int, t: T): T = depth match { case 0 => t case _ => choose(transforms) match { case Gen.Map => val i = nextInt genTransform(depth - 1, map(t, _ + i)) case Gen.FlatMap => val d = nextInt(depth) val n = genTransform(depth - d, genValue) genTransform(d, flatMap(t, n)) case Gen.Handle => val i = nextInt genTransform(depth - 1, handle(t, i)) } } genTransform(depth, genValue) } def sync: T def async(schedule: Runnable => Unit): T def failure(ex: Throwable): T def map(t: T, f: Int => Int): T def flatMap(t: T, f: T): T def handle(t: T, i: Int): T } object Gen { sealed trait Op sealed trait Value extends Op case object Async extends Value case object Sync extends Value case object Failure extends Value sealed trait Transform extends Op case object Map extends Transform case object FlatMap extends Transform case object Handle extends Transform }
Example 124
Source File: CssView.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash package css import io.udash.bindings.modifiers.{Binding, EmptyModifier} import org.scalajs.dom.Element import scalatags.JsDom.all.Modifier import scala.annotation.tailrec trait CssView extends CssText { import CssView._ implicit def style2Mod(style: CssStyle): Modifier = new StyleModifier(style) implicit def elementOps(element: Element): ElementOps = new ElementOps(element) implicit def styleOps(style: CssStyle): StyleOps = new StyleOps(style) implicit def styleFactoryOps[T](factory: T => CssStyle): StyleFactoryOps[T] = new StyleFactoryOps[T](factory) } object CssView extends CssView { private final class StyleModifier(styles: CssStyle*) extends Modifier { override def applyTo(t: Element): Unit = styles.foreach(_.addTo(t)) } final class ElementOps(private val element: Element) extends AnyVal { def styles(styles: CssStyle*): Element = { styles.foreach(_.addTo(element)) element } } final class StyleOps(private val style: CssStyle) extends AnyVal { def addTo(element: Element): Unit = style.classNames.foreach(element.classList.add) def removeFrom(element: Element): Unit = { val cl = element.classList cl.remove(style.className) style.commonPrefixClass.foreach { prefixClass => @tailrec def removePrefix(i: Int = 0): Boolean = i >= cl.length || (!cl(i).startsWith(s"$prefixClass-") && removePrefix(i + 1)) if (removePrefix()) { cl.remove(prefixClass) } } } def styleIf(property: ReadableProperty[Boolean]): Binding = property.reactiveApply( (elem, value) => if (value) addTo(elem) else removeFrom(elem) ) def styleIf(condition: Boolean): Modifier = { if (condition) new StyleModifier(style) else new EmptyModifier[Element] } } final class StyleFactoryOps[T](private val factory: T => CssStyle) extends AnyVal { def reactiveApply(property: ReadableProperty[T]): Binding = reactiveOptionApply(property.transform(Option.apply)) def reactiveOptionApply(property: ReadableProperty[Option[T]]): Binding = new Binding { private var prevStyle: CssStyle = _ override def applyTo(el: Element): Unit = { propertyListeners += property.listen(t => { if (prevStyle != null) { prevStyle.classNames.foreach(el.classList.remove) } t match { case Some(t) => val newStyle = factory(t) newStyle.classNames.foreach(el.classList.add) prevStyle = newStyle case None => prevStyle = null } }, initUpdate = true) } } } }
Example 125
Source File: RoutingEngine.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.routing import com.avsystem.commons._ import com.avsystem.commons.misc.AbstractCase import com.github.ghik.silencer.silent import io.udash._ import io.udash.logging.CrossLogging import io.udash.properties.PropertyCreator import io.udash.utils.CallbacksHandler import io.udash.utils.FilteringUtils._ import io.udash.view.ViewRenderer import scala.annotation.tailrec final case class StateChangeEvent[S <: State](currentState: S, oldState: S) extends AbstractCase def currentStateProperty: ReadableProperty[HierarchyRoot] = currentStateProp.readable @tailrec private def getStatePath(forState: Option[HierarchyRoot], acc: List[HierarchyRoot] = Nil): List[HierarchyRoot] = forState match { case Some(state) => getStatePath(state.parentState, state :: acc) case None => acc } private def getUpdatablePathSize(path: Iterator[HierarchyRoot], oldPath: Iterator[HierarchyRoot]): Int = path.zip(oldPath).takeWhile { case (h1, h2) => viewFactoryRegistry.matchStateToResolver(h1) == viewFactoryRegistry.matchStateToResolver(h2) }.length private def cleanup(state: Iterator[(View, Presenter[_])]): Unit = { state.foreach { case (view, presenter) => Try(view.onClose()).failed.foreach(logger.warn("Error closing view.", _)) Try(presenter.onClose()).failed.foreach(logger.warn("Error closing presenter.", _)) } } }
Example 126
Source File: DerivationPath.scala From ledger-manager-chrome with MIT License | 5 votes |
package co.ledger.wallet.core.utils import scala.annotation.tailrec class DerivationPath(p: DerivationPath, val childNum: Long) { val parent = if (p == null) this else p val depth: Int = parent.depth + 1 val length = depth + 1 def /(child: DerivationPath): DerivationPath = { new DerivationPath(this, child.childNum) } def ++(child: DerivationPath): DerivationPath = { var path = this for (i <- 0 to child.depth) { path = new DerivationPath(path, child(i).get.childNum) } path } def apply(depth: Int): Option[DerivationPath] = { var root = this while (root.parent != DerivationPath.Root && root.depth > depth) { root = root.parent } if (root == DerivationPath.Root) None else Some(root) } def isHardened = childNum >= 0x80000000L lazy val index = if (isHardened) childNum - 0x80000000L else childNum override def toString: String = { if (parent == this) "m" else s"${parent.toString}/${if (isHardened) index + "'" else index}" } } object DerivationPath { object Root extends DerivationPath(null, -1) { override val depth = -1 } def apply(path: String): DerivationPath = parse(path.split('/'), 0, Root) def parse(path: String): DerivationPath = parse(path.split('/'), 0, Root) @tailrec def parse(parts: Array[String], offset: Int, node: DerivationPath): DerivationPath = { if (offset >= parts.length) node else if (parts(offset) == "m" && offset == 0) parse(parts, offset + 1, Root) else { val num = parts(offset).takeWhile(_.isDigit) if (num.length == 0) throw new Exception(s"Unable to parse path ${parts.mkString("/")}") val hardened = parts(offset).endsWith("'") || parts(offset).endsWith("h") val childNum = num.toLong + (if (hardened) 0x80000000L else 0L) parse(parts, offset + 1, new DerivationPath(node, childNum)) } } object dsl { implicit class DPInt(val num: Int) { def h: DerivationPath = new DerivationPath(Root, num.toLong + 0x80000000L) } implicit def Int2DerivationPath(num: Int): DerivationPath = new DerivationPath(Root, num) } }
Example 127
Source File: PutRecordAction.scala From aws-kinesis-scala with Apache License 2.0 | 5 votes |
package jp.co.bizreach.kinesisfirehose.action import com.amazonaws.retry.PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY import com.amazonaws.services.kinesisfirehose.model.ServiceUnavailableException import jp.co.bizreach.kinesisfirehose._ import org.slf4j.LoggerFactory import scala.annotation.tailrec import scala.collection.mutable.ArrayBuffer import scala.math._ import scala.util.Random trait PutRecordAction { private val logger = LoggerFactory.getLogger(getClass) def withPutBatchRetry(records: Seq[Array[Byte]], retryLimit: Int = DEFAULT_MAX_ERROR_RETRY) (f: Seq[Array[Byte]] => PutRecordBatchResult): Seq[Either[PutRecordBatchResponseEntry, PutRecordBatchResponseEntry]] = { val buffer = ArrayBuffer[Either[PutRecordBatchResponseEntry, PutRecordBatchResponseEntry]](Nil.padTo(records.size, null): _*) @tailrec def put0(records: Seq[(Array[Byte], Int)], retry: Int = 0): Unit = { val result = f(records.map(_._1)) val failed = records zip result.records flatMap { case ((_, i), entry) if Option(entry.errorCode).isEmpty => buffer(i) = Right(entry) None case ((record, i), entry) => buffer(i) = Left(entry) Some(record -> i) } // success, or exceed the upper limit of the retry if (failed.isEmpty || retry >= retryLimit) () // retry else { Thread.sleep(sleepDuration(retry, retryLimit)) logger.warn(s"Retrying the put requests. Retry count: ${retry + 1}") put0(failed, retry + 1) } } put0(records.zipWithIndex) buffer.toList } def withPutRetry(retryLimit: Int = DEFAULT_MAX_ERROR_RETRY) (f: => PutRecordResult): Either[Throwable, PutRecordResult] = { @tailrec def put0(retry: Int = 0): Either[Throwable, PutRecordResult] = { try Right(f) catch { case e: ServiceUnavailableException => if (retry >= retryLimit) Left(e) else { Thread.sleep(sleepDuration(retry, retryLimit)) logger.warn(s"Retrying the put request. Retry count: ${retry + 1}") put0(retry + 1) } } } put0() } protected def sleepDuration(retry: Int, retryLimit: Int): Long = { // scaling factor val d = 0.5 + Random.nextDouble() * 0.1 // possible seconds val durations = (0 until retryLimit).map(n => pow(2, n) * d) (durations(retry) * 1000).toLong } }
Example 128
Source File: Optimize.scala From skeuomorph with Apache License 2.0 | 5 votes |
package higherkindness.skeuomorph.openapi import higherkindness.droste._ import cats.data.State import cats.implicits._ import scala.annotation.tailrec object Optimize { def namedTypesTrans[T](name: String): Trans[JsonSchemaF, JsonSchemaF, T] = Trans { case JsonSchemaF.ObjectF(_, _) => JsonSchemaF.reference[T](name) case JsonSchemaF.EnumF(_) => JsonSchemaF.reference[T](name) case other => other } def namedTypes[T: Basis[JsonSchemaF, ?]](name: String): T => T = scheme.cata(namedTypesTrans(name).algebra) type NestedTypesState[T, O] = State[(Map[String, T], Long), O] def nestedTypesTrans[T: Basis[JsonSchemaF, ?]]: TransM[NestedTypesState[T, ?], JsonSchemaF, JsonSchemaF, T] = TransM { case JsonSchemaF.ArrayF(x) if isNestedType(x) => extractNestedTypes("AnonymousObject", x).map { case (n, t) => JsonSchemaF.ArrayF(namedTypes(n).apply(t)) } case JsonSchemaF.ObjectF(fields, required) => fields .traverse[NestedTypesState[T, ?], JsonSchemaF.Property[T]] { case p if isNestedType(p.tpe) => extractNestedTypes(p.name.capitalize, p.tpe).map { //TODO Maybe we should normalize case (n, t) => p.copy(tpe = namedTypes[T](n).apply(t)) } case p => State.pure(p) } .map(JsonSchemaF.ObjectF(_, required)) case other => State.pure(other) } def nestedTypes[T: Basis[JsonSchemaF, ?]]: T => NestedTypesState[T, T] = scheme.anaM(nestedTypesTrans.coalgebra) private def isNestedType[T: Project[JsonSchemaF, ?]](t: T): Boolean = { import JsonSchemaF._ import higherkindness.droste.syntax.project.toProjectSyntaxOps t.project match { case ObjectF(properties, _) if properties.nonEmpty => true case EnumF(_) => true case _ => false } } private def extractNestedTypes[T: Basis[JsonSchemaF, ?]](name: String, tpe: T): NestedTypesState[T, (String, T)] = { def addType(items: (String, T)): NestedTypesState[T, Unit] = State.modify { case (x, y) => (x + items) -> y } def generateName(originalName: String, previousOccurrences: Long): String = originalName.capitalize + (if (previousOccurrences > 0) previousOccurrences.toString else "") @tailrec def findNameAndIndex(ix: Long, x: Map[String, _]): (String, Long) = { val n = generateName(name, ix) if (x.contains(n)) findNameAndIndex(ix + 1, x) else n -> ix } def findName: NestedTypesState[T, String] = State { case (x, i) => val (name, nextI) = findNameAndIndex(i, x) (x, nextI) -> name } for { newType <- nestedTypes.apply(tpe) newName <- findName _ <- addType(newName -> newType) } yield newName -> newType } }
Example 129
Source File: package.scala From zio with Apache License 2.0 | 5 votes |
package zio.test import scala.annotation.tailrec import zio.{ UIO, URIO } package object sbt { type SendSummary = URIO[Summary, Unit] object SendSummary { def fromSend(send: Summary => Unit): SendSummary = URIO.fromFunctionM(summary => URIO.effectTotal(send(summary))) def fromSendM(send: Summary => UIO[Unit]): SendSummary = URIO.fromFunctionM(send) def noop: SendSummary = UIO.unit } private[sbt] def colored(s: String): String = { @tailrec def loop(s: String, i: Int, color: Option[String]): String = if (i >= s.length) s else { val s1 = s.slice(i, i + 5) val isColor = s1 == Console.BLUE || s1 == Console.CYAN || s1 == Console.GREEN || s1 == Console.RED || s1 == Console.YELLOW if (isColor) loop(s, i + 5, Some(s1)) else if (s.slice(i, i + 4) == Console.RESET) loop(s, i + 4, None) else if (s.slice(i, i + 1) == "\n" && color.isDefined) loop(s.patch(i + 1, color.get, 0), i + 6, color) else loop(s, i + 1, color) } loop(s, 0, None) } }
Example 130
Source File: ZIOBaseSpec.scala From zio with Apache License 2.0 | 5 votes |
package zio import scala.annotation.tailrec import zio.duration._ import zio.test._ trait ZIOBaseSpec extends DefaultRunnableSpec { override def aspects = List(TestAspect.timeout(60.seconds)) sealed trait ZIOTag { val value: String val subTags: List[ZIOTag] = Nil } object ZIOTag { case object errors extends ZIOTag { override val value = "errors" } case object future extends ZIOTag { override val value = "future" } case object interop extends ZIOTag { override val value = "interop" override val subTags: List[ZIOTag] = List(future) } case object interruption extends ZIOTag { override val value = "interruption" } case object regression extends ZIOTag { override val value = "regression" } case object supervision extends ZIOTag { override val value = "supervision" } } def zioTag(zioTag: ZIOTag, zioTags: ZIOTag*): TestAspectPoly = { val tags = zioTags.map(_.value) ++ getSubTags(zioTag) ++ zioTags.flatMap(getSubTags) TestAspect.tag(zioTag.value, tags.distinct: _*) } private def getSubTags(zioTag: ZIOTag): List[String] = { @tailrec def loop(currentZioTag: ZIOTag, remainingZioTags: List[ZIOTag], result: List[String]): List[String] = (currentZioTag.subTags, remainingZioTags) match { case (Nil, Nil) => currentZioTag.value :: result case (Nil, t :: ts) => loop(t, ts, currentZioTag.value :: result) case (st :: sts, ts) => loop(st, sts ++ ts, currentZioTag.value :: result) } zioTag.subTags match { case t :: ts => loop(t, ts, Nil) case Nil => Nil } } }
Example 131
Source File: PolySpec.scala From zio with Apache License 2.0 | 5 votes |
package zio.test.poly import scala.annotation.tailrec import zio.random._ import zio.test.Assertion._ import zio.test._ object PolySpec extends DefaultRunnableSpec { sealed trait Expr[+A] final case class Value[+A](a: A) extends Expr[A] final case class Mapping[A, +B](expr: Expr[A], f: A => B) extends Expr[B] def eval[A](expr: Expr[A]): A = expr match { case Value(a) => a case Mapping(x, f) => f(eval(x)) } @tailrec def fuse[A](expr: Expr[A]): Expr[A] = expr match { case Mapping(Mapping(x, f), g) => fuse(Mapping(x, f andThen g)) case x => x } def genValue(t: GenPoly): Gen[Random with Sized, Expr[t.T]] = t.genT.map(Value(_)) def genMapping(t: GenPoly): Gen[Random with Sized, Expr[t.T]] = Gen.suspend { GenPoly.genPoly.flatMap { t0 => genExpr(t0).flatMap { expr => val genFunction: Gen[Random with Sized, t0.T => t.T] = Gen.function(t.genT) val genExpr1: Gen[Random with Sized, Expr[t.T]] = genFunction.map(f => Mapping(expr, f)) genExpr1 } } } def genExpr(t: GenPoly): Gen[Random with Sized, Expr[t.T]] = Gen.oneOf(genMapping(t), genValue(t)) def spec = suite("PolySpec")( testM("map fusion") { check(GenPoly.genPoly.flatMap(genExpr(_)))(expr => assert(eval(fuse(expr)))(equalTo(eval(expr)))) } ) }
Example 132
Source File: ZTrace.scala From zio with Apache License 2.0 | 5 votes |
package zio import scala.annotation.tailrec import zio.internal.stacktracer.ZTraceElement final case class ZTrace( fiberId: Fiber.Id, executionTrace: List[ZTraceElement], stackTrace: List[ZTraceElement], parentTrace: Option[ZTrace] ) { def prettyPrint: String = { val execTrace = this.executionTrace.nonEmpty val stackTrace = this.stackTrace.nonEmpty val stackPrint = if (stackTrace) s"Fiber:$fiberId was supposed to continue to:" :: this.stackTrace.map(loc => s" a future continuation at " + loc.prettyPrint) else s"Fiber:$fiberId was supposed to continue to: <empty trace>" :: Nil val execPrint = if (execTrace) s"Fiber:$fiberId execution trace:" :: executionTrace.map(loc => " at " + loc.prettyPrint) else s"Fiber:$fiberId ZIO Execution trace: <empty trace>" :: Nil val ancestry: List[String] = parentTrace .map(trace => s"Fiber:$fiberId was spawned by:\n" :: trace.prettyPrint :: Nil) .getOrElse(s"Fiber:$fiberId was spawned by: <empty trace>" :: Nil) (stackPrint ++ ("" :: execPrint) ++ ("" :: ancestry)).mkString("\n") } def parents: List[ZTrace] = { val builder = List.newBuilder[ZTrace] var parent = parentTrace.orNull while (parent ne null) { builder += parent parent = parent.parentTrace.orNull } builder.result() } def ancestryLength: Int = { @tailrec def go(i: Int, trace: ZTrace): Int = trace.parentTrace match { case Some(parent) => go(i + 1, parent) case None => i } go(0, this) } } object ZTrace { def truncatedParentTrace(trace: ZTrace, maxAncestors: Int): Option[ZTrace] = if (trace.ancestryLength > maxAncestors) trace.parents.iterator .take(maxAncestors) .foldRight(Option.empty[ZTrace])((trace, parent) => Some(trace.copy(parentTrace = parent))) else trace.parentTrace }
Example 133
Source File: MetricFlow.scala From akka-visualmailbox with Apache License 2.0 | 5 votes |
package de.aktey.akka.visualmailbox import akka.actor.{ActorRef, Props} import akka.stream.actor.ActorPublisher import akka.stream.scaladsl.Source import scala.annotation.tailrec object MetricFlow { // subscriber as flow source // that registrates itself to a router class MetricsSubscriber(router: ActorRef) extends ActorPublisher[VisualMailboxMetric] { import akka.stream.actor.ActorPublisherMessage._ val MaxBufferSize = 100 var buf = Vector.empty[VisualMailboxMetric] router ! self def receive = { case metric: VisualMailboxMetric if buf.size == MaxBufferSize => case metric: VisualMailboxMetric => if (buf.isEmpty && totalDemand > 0) onNext(metric) else { buf :+= metric deliverBuf() } case Request(_) => deliverBuf() case Cancel => context.stop(self) } @tailrec private def deliverBuf(): Unit = if (totalDemand > 0) { if (totalDemand <= Int.MaxValue) { val (use, keep) = buf.splitAt(totalDemand.toInt) buf = keep use foreach onNext } else { val (use, keep) = buf.splitAt(Int.MaxValue) buf = keep use foreach onNext deliverBuf() } } } object MetricsSubscriber { def props(router: ActorRef) = Props(new MetricsSubscriber(router)) } def metricSource(router: ActorRef): Source[String, ActorRef] = Source.actorPublisher[VisualMailboxMetric](MetricsSubscriber.props(router)).map { case VisualMailboxMetric(sender, receiver, receiverMailBoxSize, meassureTimeMillies) => s"""{ | "sender": "$sender", | "receiver": "$receiver", | "receiverMailBoxSize": $receiverMailBoxSize, | "meassureTimeMillies": $meassureTimeMillies |}""".stripMargin } }
Example 134
Source File: TimeParser.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airframe.metrics import java.time._ import java.time.format.DateTimeFormatter import wvlet.log.LogSupport import scala.annotation.tailrec import scala.util.{Failure, Success, Try} object TimeParser extends LogSupport { val localDatePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd") val localDateTimePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]") val zonedDateTimePatterns: List[DateTimeFormatter] = List( "yyyy-MM-dd HH:mm:ss[.SSS][ z][XXXXX][XXXX]['['VV']']", "yyyy-MM-dd'T'HH:mm:ss[.SSS][ z][XXXXX][XXXX]['['VV']']" ).map(DateTimeFormatter.ofPattern(_)) case class TimeParseResult(dateTime: ZonedDateTime, unit: TimeWindowUnit) def parseLocalDateTime(s: String, zone: ZoneOffset): Option[TimeParseResult] = { Try(LocalDateTime.parse(s, localDateTimePattern)) .map { d => TimeParseResult(ZonedDateTime.of(d, zone), TimeWindowUnit.Second) } .orElse { Try(LocalDate.parse(s, localDatePattern)) .map { d => TimeParseResult(d.atStartOfDay(zone), TimeWindowUnit.Day) } } .toOption } def parseZonedDateTime(s: String): Option[TimeParseResult] = { @tailrec def loop(lst: List[DateTimeFormatter]): Option[TimeParseResult] = { if (lst.isEmpty) { None } else { val formatter = lst.head Try(ZonedDateTime.parse(s, formatter)) match { case Success(dt) => Some(TimeParseResult(dt, TimeWindowUnit.Second)) case Failure(e) => loop(lst.tail) } } } loop(zonedDateTimePatterns.toList) } def parseAtLocalTimeZone(s: String): Option[ZonedDateTime] = parse(s, systemTimeZone) def parse(s: String, zone: ZoneOffset): Option[ZonedDateTime] = { parseTimeAndUnit(s, zone).map(_.dateTime) } def parseTimeAndUnit(s: String, zone: ZoneOffset): Option[TimeParseResult] = { parseLocalDateTime(s, zone) .orElse(parseZonedDateTime(s)) } }
Example 135
Source File: Count.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airframe.metrics import wvlet.airframe.metrics.Count.CountUnit import wvlet.airframe.surface.{Surface, Zero} import scala.annotation.tailrec import scala.util.{Failure, Success, Try} object Count { Zero.register(Surface.of[CountUnit], ONE) Zero.register(Surface.of[Count], Count(0)) val units = List(ONE, THOUSAND, MILLION, BILLION, TRILLION, QUADRILLION) private val unitTable = units.map(x => x.unitString -> x).toMap[String, CountUnit] sealed class CountUnit private[metrics] (val factor: Long, val unitString: String) { override def toString = unitString } case object ONE extends CountUnit(1L, "") case object THOUSAND extends CountUnit(1000L, "K") case object MILLION extends CountUnit(1000000L, "M") case object BILLION extends CountUnit(1000000000L, "B") case object TRILLION extends CountUnit(1000000000000L, "T") case object QUADRILLION extends CountUnit(1000000000000000L, "Q") object CountUnit { // deserializer for airframe-codec def unapply(unitString: String): Option[CountUnit] = units.find(_.unitString == unitString) } def succinct(x: Long): Count = { Count(x, ONE).mostSuccinctCount } def apply(value: Long): Count = Count(value, ONE) def unapply(countStr: String): Option[Count] = Try(apply(countStr)).toOption private val countPattern = """^\s*((?:-?)?[\d,]+(?:\.\d+)?)\s*([a-zA-Z])\s*$""".r("num", "unit") def apply(countStr: String): Count = { countPattern.findFirstMatchIn(countStr) match { case None => // When no unit string is found val normalized = countStr.replaceAll(",", "") Try(normalized.toLong) match { case Success(v) => Count(v) case Failure(e) => // Try parsing as double Try(normalized.toDouble) match { case Success(d) => Count(d.toLong) case Failure(e) => throw new IllegalArgumentException(s"Invalid count string: ${countStr}") } } case Some(m) => val num = m.group("num").replaceAll(",", "").toDouble val unit = m.group("unit") unitTable.get(unit) match { case None => throw new IllegalArgumentException(s"Invalid count unit ${unit} in ${countStr}") case Some(u) => Count((num * u.factor).round, u) } } } }
Example 136
Source File: Router.scala From naptime with Apache License 2.0 | 5 votes |
package org.coursera.naptime.router2 import javax.inject.Inject import com.google.inject.Injector import org.coursera.naptime.resources.CollectionResource import play.api.mvc.RequestHeader import scala.annotation.tailrec import scala.collection.immutable import language.experimental.macros object Router { def build[T <: CollectionResource[_, _, _]]: ResourceRouterBuilder = macro MacroImpls.build[T] @tailrec def routeRequestHelper( resourceRouters: Seq[ResourceRouter], requestHeader: RequestHeader, path: String): Option[RouteAction] = { if (resourceRouters.isEmpty) { None } else { val first = resourceRouters.head val firstResult = first.routeRequest(path, requestHeader) if (firstResult.isDefined) { firstResult } else { routeRequestHelper(resourceRouters.tail, requestHeader, path) } } } routeRequestHelper(resourceRouters, requestHeader, path) } else { None } } // TODO(saeta): add additional functionality (i.e. listing all resources / metadata, etc.) // TODO(saeta): performance test the new router implementation & do reasonable optimizations. }
Example 137
Source File: Slug.scala From naptime with Apache License 2.0 | 5 votes |
package org.coursera.naptime.actions import org.coursera.common.jsonformat.JsonFormats import org.coursera.common.stringkey.StringKeyFormat import play.api.libs.json.Format import scala.annotation.tailrec def slugify(input: String): Slug = { // Convert the unicode input to ascii. This ensures that non Latin based languages have // reasonable conversions. val slugString = input //Junidecode.unidecode(input) .replaceAll("([a-z])'s([^a-z])", "$1s$2") // Convert apostrophes. .replaceAll("[^a-zA-Z0-9]", "-") // Convert all non alphanumeric characters with hyphen. .replaceAll("-{2,}", "-") // Collapse multiple hyphens into one .stripPrefix("-") .stripSuffix("-") .toLowerCase val words = slugString.split("-").toList if (words.head.length() > MaxSlugLength) { Slug(words.head.take(MaxSlugLength)) } else { @tailrec def buildTruncatedSlug(currentWord: String, wordList: List[String]): String = { wordList match { case firstWord :: tail => val candidate = currentWord + "-" + firstWord if (candidate.length() > MaxSlugLength) { currentWord } else { buildTruncatedSlug(candidate, tail) } case Nil => currentWord } } Slug(buildTruncatedSlug(words.head, words.drop(1))) } } }
Example 138
Source File: DependencyResolver.scala From dependency with MIT License | 5 votes |
package dependency.resolver import scala.annotation.tailrec case class DependencyResolution( resolved: List[Seq[ProjectInfo]], unresolved: Seq[ProjectInfo], ) { private[this] lazy val allResolvedLibraries: Set[String] = resolved.flatMap(_.flatMap { p => p.provides.map(_.identifier) }).toSet def isLibraryResolved(identifier: String): Boolean = allResolvedLibraries.contains(identifier) def allUnknownLibraries: Seq[LibraryReference] = resolved.flatMap(_.flatMap(_.unknownLibraries)).distinct.sortBy(_.identifier) } object DependencyResolution { val empty: DependencyResolution = DependencyResolution(resolved = Nil, unresolved = Nil) } case class DependencyResolver(projects: Seq[ProjectInfo]) { lazy val resolution: DependencyResolution = resolve(DependencyResolution.empty, projects.toList) private[this] def areDependenciesSatisfied(resolution: DependencyResolution, libraries: Seq[LibraryReference]): Boolean = { libraries.forall { l => resolution.isLibraryResolved(l.identifier) } } @tailrec private[this] def resolve(resolution: DependencyResolution, remainingProjects: List[ProjectInfo]): DependencyResolution = { val (resolved, remaining) = remainingProjects.partition { p => areDependenciesSatisfied(resolution, p.dependsOn) } resolved match { case Nil => { resolution.copy( unresolved = resolution.unresolved ++ remaining.map { p => val (resolvedDependencies, unresolvedDependencies) = p.dependsOn.partition { l => areDependenciesSatisfied(resolution, Seq(l)) } p.copy( resolvedDependencies = resolvedDependencies, unresolvedDependencies = unresolvedDependencies, ) }, ) } case _ => { resolve(resolution.copy( resolved = resolution.resolved ++ Seq(resolved), ), remaining) } } } }
Example 139
package flaky.history import java.io.File import org.eclipse.jgit.api.{Git => JGit} import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.storage.file.FileRepositoryBuilder import scala.annotation.tailrec import scala.collection.JavaConverters._ import scala.util.{Failure, Success, Try} object Git { def apply(workTree: File): Git = { @tailrec def findGitRoot(dir: File, upLevels: Int): File = { if (upLevels > 0) { val git = new GitUsingJgit((new FileRepositoryBuilder).setWorkTree(dir).build()) val triedString = git.currentId() triedString match { case Success(_) => dir case Failure(_) if dir.getParentFile != null => findGitRoot(dir.getParentFile, upLevels - 1) case Failure(_) => dir } } else { dir } } val gitRoot = findGitRoot(workTree, 3) new GitUsingJgit((new FileRepositoryBuilder).setWorkTree(gitRoot).build()) } } trait Git { def currentId(): Try[String] def history(): Try[List[GitCommit]] def commitsList(previous: String, current: String): Try[List[GitCommit]] = { history().map(_.dropWhile(_.id != current).takeWhile(_.id != previous)) } def remoteUrl(): Try[String] } class GitUsingJgit(repository: Repository) extends Git { private val jgit = new JGit(repository) def currentId(): Try[String] = Try { jgit.log .setMaxCount(1) .call() .asScala .map(_.getId.abbreviate(7).name) .head } def history(): Try[List[GitCommit]] = Try { jgit.log .add(repository.resolve("master")) .call() .asScala .toList .map(fullDesc) } private def fullDesc(commit: RevCommit) = GitCommit( commit.getId.abbreviate(7).name, commit.getAuthorIdent.getEmailAddress, commit.getShortMessage, commit.getCommitTime ) override def remoteUrl(): Try[String] = { Try { repository.getConfig.getString("remote", "origin", "url") } match { case Success(null) => Failure(new Exception("No remote repo found")) case Success(s) => Success(s) case Failure(ex) => Failure(ex) } } } case class GitCommit(id: String, author: String, shortMsg: String, commitTime: Int)
Example 140
Source File: FreeMonad.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch10 import ch08.Functor import scala.annotation.tailrec import scala.language.higherKinds object FreeMonad extends App { case class Bait(name: String) extends AnyVal case class Line(length: Int) extends AnyVal case class Fish(name: String) extends AnyVal sealed trait Action[A] final case class BuyBait[A](name: String, f: Bait => A) extends Action[A] final case class CastLine[A](bait: Bait, f: Line => A) extends Action[A] final case class HookFish[A](line: Line, f: Fish => A) extends Action[A] // assessment final case class ReleaseFish[A](fish: Fish, f: Unit => A) extends Action[A] final case class Done[F[_]: Functor, A](a: A) extends Free[F, A] final case class Join[F[_]: Functor, A](action: F[Free[F, A]]) extends Free[F, A] class Free[F[_]: Functor, A] { def flatMap[B](f: A => Free[F, B]): Free[F, B] = this match { case Done(a) => f(a) case Join(a) => Join(implicitly[Functor[F]].map(a)(_.flatMap(f))) } def map[B](f: A => B): Free[F, B] = flatMap(a => Done(f(a))) } implicit lazy val actionFunctor: Functor[Action] = new Functor[Action] { override def map[A, B](in: Action[A])(f: A => B): Action[B] = in match { case BuyBait(name, a) => BuyBait(name, x => f(a(x))) case CastLine(bait, a) => CastLine(bait, x => f(a(x))) case HookFish(line, a) => HookFish(line, x => f(a(x))) // assessment case ReleaseFish(fish, a) => ReleaseFish(fish, x => f(a(x))) } } def buyBait(name: String): Free[Action, Bait] = Join(BuyBait(name, bait => Done(bait))) def castLine(bait: Bait): Free[Action, Line] = Join(CastLine(bait, line => Done(line))) def hookFish(line: Line): Free[Action, Fish] = Join(HookFish(line, fish => Done(fish))) // assessment def releaseFish(fish: Fish): Free[Action, Unit] = Join(ReleaseFish(fish, _ => Done(()))) def catchFish(baitName: String): Free[Action, _] = for { bait <- buyBait(baitName) line <- castLine(bait) fish <- hookFish(line) _ <- releaseFish(fish) } yield () def log[A](a: A): Unit = println(a) @tailrec def goFishingLogging[A](actions: Free[Action, A], unit: Unit): A = actions match { case Join(BuyBait(name, f)) => goFishingLogging(f(Bait(name)), log(s"Buying bait $name")) case Join(CastLine(bait, f)) => goFishingLogging(f(Line(bait.name.length)), log(s"Casting line with ${bait.name}")) case Join(HookFish(line, f)) => goFishingLogging(f(Fish("CatFish")), log(s"Hooking fish from ${line.length} feet")) case Done(fish) => fish // assessment case Join(ReleaseFish(fish, f)) => goFishingLogging(f(()), log(s"Releasing the fish $fish")) } println(goFishingLogging(catchFish("Crankbait"), ())) @tailrec def goFishingAcc[A](actions: Free[Action, A], log: List[AnyVal]): List[AnyVal] = actions match { case Join(BuyBait(name, f)) => val bait = Bait(name) goFishingAcc(f(bait), bait :: log) case Join(CastLine(bait, f)) => val line = Line(bait.name.length) goFishingAcc(f(line), line :: log) case Join(HookFish(line, f)) => val fish = Fish(s"CatFish from ($line)") goFishingAcc(f(fish), fish :: log) case Done(_) => log.reverse // assessment case Join(ReleaseFish(fish, f)) => goFishingAcc(f(()), fish.copy(name = fish.name + " released") :: log) } lazy val log = goFishingAcc(catchFish("Crankbait"), Nil) println(log) }
Example 141
Source File: InputStreamLineReader.scala From perf_tester with Apache License 2.0 | 5 votes |
package org.perftester.sbtbot.process import java.io._ import akka.util.ByteString import scala.annotation.tailrec import scala.util.Try class InputStreamLineReader(inputStream: InputStream, name: String) { var bs: ByteString = ByteString() val bis = new BufferedInputStream(inputStream) var limit = 2048 val byteBuffer = new Array[Byte](limit) var isClosed = false def close(): Option[String] = { val res = if (isClosed) None else { val remainingOutput = bs.decodeString("UTF-8") if (remainingOutput.length > 0) Some(remainingOutput) else None } bs = ByteString.empty res } def read(): List[String] = { if (isClosed) Nil else { val rawRes = performRead(Nil) rawRes.reverse } } @tailrec private final def performRead(cur: List[String]): List[String] = { val actualRead = if (bis.available > 0) bis.read(byteBuffer) else 0 if (actualRead == -1) { Try(inputStream.close()) isClosed = true val remainingOutput = bs.decodeString("UTF-8") if (remainingOutput.length > 0) { remainingOutput :: cur } else cur } else if (actualRead > 0) { val byteString = ByteString.fromArray(byteBuffer, 0, actualRead) bs ++= byteString var activeCurrent = cur var offset = bs.indexWhere(_ == '\n') while (offset != -1) { val (init, rem) = bs.splitAt(offset) val lineStr = init.decodeString("UTF-8") activeCurrent ::= lineStr bs = rem.drop(1) // drop the newline offset = bs.indexWhere(_ == '\n') } performRead(activeCurrent) } else { cur } } }
Example 142
Source File: nat.scala From perf_tester with Apache License 2.0 | 5 votes |
package shapeless import scala.language.experimental.macros import scala.annotation.tailrec import scala.reflect.macros.whitebox type _0 = shapeless._0 val _0: _0 = new _0 def toInt[N <: Nat](implicit toIntN : ToInt[N]) = toIntN() def toInt(n : Nat)(implicit toIntN : ToInt[n.N]) = toIntN() implicit def natOps[N <: Nat](n : N) : NatOps[N] = new NatOps(n) } @macrocompat.bundle class NatMacros(val c: whitebox.Context) extends NatMacroDefns { import c.universe._ def materializeWidened(i: Tree): Tree = i match { case NatLiteral(n) => mkNatValue(n) case _ => c.abort(c.enclosingPosition, s"Expression $i does not evaluate to a non-negative Int literal") } } @macrocompat.bundle trait NatMacroDefns { val c: whitebox.Context import c.universe._ object NatLiteral { def unapply(i: Tree): Option[Int] = i match { case Literal(Constant(n: Int)) if n >= 0 => Some(n) case _ => None } } def mkNatTpt(i: Int): Tree = { val succSym = typeOf[Succ[_]].typeConstructor.typeSymbol val _0Sym = typeOf[_0].typeSymbol @tailrec def loop(i: Int, acc: Tree): Tree = { if(i == 0) acc else loop(i-1, AppliedTypeTree(Ident(succSym), List(acc))) } loop(i, Ident(_0Sym)) } def mkNatTpe(i: Int): Type = { val succTpe = typeOf[Succ[_]].typeConstructor val _0Tpe = typeOf[_0] @tailrec def loop(i: Int, acc: Type): Type = { if(i == 0) acc else loop(i-1, appliedType(succTpe, acc)) } loop(i, _0Tpe) } def mkNatValue(i: Int): Tree = q""" new ${mkNatTpt(i)} """ }
Example 143
Source File: MethodCache.scala From perf_tester with Apache License 2.0 | 5 votes |
package scala package runtime import java.lang.reflect.{ Method => JMethod } import java.lang.{ Class => JClass } import scala.annotation.tailrec @tailrec private def findInternal(forReceiver: JClass[_]): JMethod = if (forReceiver eq receiver) method else next match { case x: PolyMethodCache => x findInternal forReceiver case _ => next find forReceiver } def find(forReceiver: JClass[_]): JMethod = findInternal(forReceiver) // TODO: come up with a more realistic number final private val MaxComplexity = 160 def add(forReceiver: JClass[_], forMethod: JMethod): MethodCache = if (complexity < MaxComplexity) new PolyMethodCache(this, forReceiver, forMethod, complexity + 1) else new MegaMethodCache(forMethod.getName, forMethod.getParameterTypes) }
Example 144
Source File: AddressTerminatedTopic.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.event import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec import akka.actor.ActorRef import akka.actor.ActorSystem import akka.actor.AddressTerminated import akka.actor.ExtendedActorSystem import akka.actor.Extension import akka.actor.ExtensionId import akka.actor.ExtensionIdProvider private[akka] final class AddressTerminatedTopic extends Extension { private val subscribers = new AtomicReference[Set[ActorRef]](Set.empty[ActorRef]) @tailrec def subscribe(subscriber: ActorRef): Unit = { val current = subscribers.get if (!subscribers.compareAndSet(current, current + subscriber)) subscribe(subscriber) // retry } @tailrec def unsubscribe(subscriber: ActorRef): Unit = { val current = subscribers.get if (!subscribers.compareAndSet(current, current - subscriber)) unsubscribe(subscriber) // retry } def publish(msg: AddressTerminated): Unit = { subscribers.get foreach { _.tell(msg, ActorRef.noSender) } } }
Example 145
Source File: WildcardIndex.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.util import scala.annotation.tailrec import scala.collection.immutable.HashMap private[akka] final case class WildcardIndex[T](wildcardTree: WildcardTree[T] = WildcardTree[T](), doubleWildcardTree: WildcardTree[T] = WildcardTree[T]()) { def insert(elems: Array[String], d: T): WildcardIndex[T] = elems.lastOption match { case Some("**") ⇒ copy(doubleWildcardTree = doubleWildcardTree.insert(elems.iterator, d)) case Some(_) ⇒ copy(wildcardTree = wildcardTree.insert(elems.iterator, d)) case _ ⇒ this } def find(elems: Iterable[String]): Option[T] = (if (wildcardTree.isEmpty) { if (doubleWildcardTree.isEmpty) { WildcardTree[T]() // empty } else { doubleWildcardTree.findWithTerminalDoubleWildcard(elems.iterator) } } else { val withSingleWildcard = wildcardTree.findWithSingleWildcard(elems.iterator) if (withSingleWildcard.isEmpty) { doubleWildcardTree.findWithTerminalDoubleWildcard(elems.iterator) } else { withSingleWildcard } }).data def isEmpty: Boolean = wildcardTree.isEmpty && doubleWildcardTree.isEmpty } private[akka] object WildcardTree { private val empty = new WildcardTree[Nothing]() def apply[T](): WildcardTree[T] = empty.asInstanceOf[WildcardTree[T]] } private[akka] final case class WildcardTree[T](data: Option[T] = None, children: Map[String, WildcardTree[T]] = HashMap[String, WildcardTree[T]]()) { def isEmpty: Boolean = data.isEmpty && children.isEmpty def insert(elems: Iterator[String], d: T): WildcardTree[T] = if (!elems.hasNext) { copy(data = Some(d)) } else { val e = elems.next() copy(children = children.updated(e, children.getOrElse(e, WildcardTree[T]()).insert(elems, d))) } @tailrec def findWithSingleWildcard(elems: Iterator[String]): WildcardTree[T] = if (!elems.hasNext) this else { children.get(elems.next()) match { case Some(branch) ⇒ branch.findWithSingleWildcard(elems) case None ⇒ children.get("*") match { case Some(branch) ⇒ branch.findWithSingleWildcard(elems) case None ⇒ WildcardTree[T]() } } } @tailrec def findWithTerminalDoubleWildcard(elems: Iterator[String], alt: WildcardTree[T] = WildcardTree[T]()): WildcardTree[T] = { if (!elems.hasNext) this else { val newAlt = children.getOrElse("**", alt) children.get(elems.next()) match { case Some(branch) ⇒ branch.findWithTerminalDoubleWildcard(elems, newAlt) case None ⇒ children.get("*") match { case Some(branch) ⇒ branch.findWithTerminalDoubleWildcard(elems, newAlt) case None ⇒ newAlt } } } } }
Example 146
Source File: SerializedSuspendableExecutionContext.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.util import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.ExecutionContext import scala.util.control.NonFatal import scala.annotation.{ tailrec, switch } import akka.dispatch.AbstractNodeQueue private[akka] object SerializedSuspendableExecutionContext { final val Off = 0 final val On = 1 final val Suspended = 2 def apply(throughput: Int)(implicit context: ExecutionContext): SerializedSuspendableExecutionContext = new SerializedSuspendableExecutionContext(throughput)(context match { case s: SerializedSuspendableExecutionContext ⇒ s.context case other ⇒ other }) } final def size(): Int = count() override final def toString: String = (state.get: @switch) match { case 0 ⇒ "Off" case 1 ⇒ "On" case 2 ⇒ "Off & Suspended" case 3 ⇒ "On & Suspended" } }
Example 147
Source File: Collections.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.util import scala.collection.immutable import scala.annotation.tailrec private[akka] object Collections { case object EmptyImmutableSeq extends immutable.Seq[Nothing] { override final def iterator = Iterator.empty override final def apply(idx: Int): Nothing = throw new java.lang.IndexOutOfBoundsException(idx.toString) override final def length: Int = 0 } abstract class PartialImmutableValuesIterable[From, To] extends immutable.Iterable[To] { def isDefinedAt(from: From): Boolean def apply(from: From): To def valuesIterator: Iterator[From] final def iterator: Iterator[To] = { val superIterator = valuesIterator new Iterator[To] { private[this] var _next: To = _ private[this] var _hasNext = false @tailrec override final def hasNext: Boolean = if (!_hasNext && superIterator.hasNext) { // If we need and are able to look for the next value val potentiallyNext = superIterator.next() if (isDefinedAt(potentiallyNext)) { _next = apply(potentiallyNext) _hasNext = true true } else hasNext //Attempt to find the next } else _hasNext // Return if we found one override final def next(): To = if (hasNext) { val ret = _next _next = null.asInstanceOf[To] // Mark as consumed (nice to the GC, don't leak the last returned value) _hasNext = false // Mark as consumed (we need to look for the next value) ret } else throw new java.util.NoSuchElementException("next") } } override lazy val size: Int = iterator.size override def foreach[C](f: To ⇒ C) = iterator foreach f } }
Example 148
Source File: SimpleDnsCache.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.io import java.util.concurrent.atomic.AtomicReference import akka.io.Dns.Resolved import scala.annotation.tailrec import scala.collection.immutable private[io] sealed trait PeriodicCacheCleanup { def cleanup(): Unit } class SimpleDnsCache extends Dns with PeriodicCacheCleanup { import akka.io.SimpleDnsCache._ private val cache = new AtomicReference(new Cache( immutable.SortedSet()(ExpiryEntryOrdering), immutable.Map(), clock _)) private val nanoBase = System.nanoTime() override def cached(name: String): Option[Resolved] = { cache.get().get(name) } protected def clock(): Long = { val now = System.nanoTime() if (now - nanoBase < 0) 0 else (now - nanoBase) / 1000000 } @tailrec private[io] final def put(r: Resolved, ttlMillis: Long): Unit = { val c = cache.get() if (!cache.compareAndSet(c, c.put(r, ttlMillis))) put(r, ttlMillis) } @tailrec override final def cleanup(): Unit = { val c = cache.get() if (!cache.compareAndSet(c, c.cleanup())) cleanup() } } object SimpleDnsCache { private class Cache(queue: immutable.SortedSet[ExpiryEntry], cache: immutable.Map[String, CacheEntry], clock: () ⇒ Long) { def get(name: String): Option[Resolved] = { for { e ← cache.get(name) if e.isValid(clock()) } yield e.answer } def put(answer: Resolved, ttlMillis: Long): Cache = { val until0 = clock() + ttlMillis val until = if (until0 < 0) Long.MaxValue else until0 new Cache( queue + new ExpiryEntry(answer.name, until), cache + (answer.name → CacheEntry(answer, until)), clock) } def cleanup(): Cache = { val now = clock() var q = queue var c = cache while (q.nonEmpty && !q.head.isValid(now)) { val minEntry = q.head val name = minEntry.name q -= minEntry if (c.get(name).filterNot(_.isValid(now)).isDefined) c -= name } new Cache(q, c, clock) } } private case class CacheEntry(answer: Dns.Resolved, until: Long) { def isValid(clock: Long): Boolean = clock < until } private class ExpiryEntry(val name: String, val until: Long) extends Ordered[ExpiryEntry] { def isValid(clock: Long): Boolean = clock < until override def compare(that: ExpiryEntry): Int = -until.compareTo(that.until) } private object ExpiryEntryOrdering extends Ordering[ExpiryEntry] { override def compare(x: ExpiryEntry, y: ExpiryEntry): Int = { x.until.compareTo(y.until) } } }
Example 149
Source File: P61a.scala From S99 with MIT License | 5 votes |
package jp.co.dwango.s99 import binary_trees._ import scala.annotation.tailrec object P61a { implicit class RichTree[T](tree: Tree[T]) { def leafList: List[T] = { @tailrec def loop(queries: List[Tree[T]], acc: List[T]): List[T] = queries match { case Nil => acc case head :: tail => head match { case End => loop(tail, acc) case Node(v, End, End) => loop(tail, v :: acc) case Node(_, left, right) => loop(left :: right :: tail, acc) } } loop(tree :: Nil, Nil) } } }
Example 150
package jp.co.dwango.s99 import scala.annotation.tailrec object P05 { def reverse[T](list: List[T]): List[T] = { @tailrec def go(rem: List[T], acc: List[T]): List[T] = rem match { case Nil => acc case x :: xs => go(xs, x :: acc) } go(list, Nil) } }
Example 151
package jp.co.dwango.s99 import scala.annotation.tailrec object P04 { def length[T](list: List[T]): Int = { @tailrec def go(lst: List[T], n: Int): Int = lst match { case x :: xs => go(xs, n + 1) case Nil => n } go(list, 0) } }
Example 152
package io.catbird.util import cats.{ Applicative, CoflatMap, Eq, Eval, MonadError, Monoid, Semigroup, Traverse } import com.twitter.util.{ Return, Throw, Try } import java.lang.Throwable import scala.{ Boolean, inline } import scala.annotation.tailrec import scala.util.{ Either, Left, Right } trait TryInstances extends TryInstances1 { implicit final def twitterTryEq[A](implicit A: Eq[A], T: Eq[Throwable]): Eq[Try[A]] = new Eq[Try[A]] { def eqv(x: Try[A], y: Try[A]): Boolean = (x, y) match { case (Throw(xError), Throw(yError)) => T.eqv(xError, yError) case (Return(xValue), Return(yValue)) => A.eqv(xValue, yValue) case _ => false } } implicit final def twitterTrySemigroup[A](implicit A: Semigroup[A]): Semigroup[Try[A]] = new TrySemigroup[A] implicit final val twitterTryInstance: MonadError[Try, Throwable] with CoflatMap[Try] with Traverse[Try] = new MonadError[Try, Throwable] with CoflatMap[Try] with Traverse[Try] { final def pure[A](x: A): Try[A] = Return(x) final def flatMap[A, B](fa: Try[A])(f: A => Try[B]): Try[B] = fa.flatMap(f) override final def map[A, B](fa: Try[A])(f: A => B): Try[B] = fa.map(f) final def handleErrorWith[A](fa: Try[A])(f: Throwable => Try[A]): Try[A] = fa.rescue { case e => f(e) } final def raiseError[A](e: Throwable): Try[A] = Throw(e) final def coflatMap[A, B](ta: Try[A])(f: Try[A] => B): Try[B] = Try(f(ta)) final def foldLeft[A, B](fa: Try[A], b: B)(f: (B, A) => B): B = fa match { case Return(a) => f(b, a) case Throw(_) => b } final def foldRight[A, B](fa: Try[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = fa match { case Return(a) => f(a, lb) case Throw(_) => lb } final def traverse[G[_], A, B](fa: Try[A])(f: A => G[B])(implicit G: Applicative[G]): G[Try[B]] = fa match { case Return(a) => G.map(f(a))(Return(_)) case t: Throw[_] => G.pure(TryInstances.castThrow[B](t)) } @tailrec final def tailRecM[A, B](a: A)(f: A => Try[Either[A, B]]): Try[B] = f(a) match { case t: Throw[_] => TryInstances.castThrow[B](t) case Return(Left(a1)) => tailRecM(a1)(f) case Return(Right(b)) => Return(b) } } } private[util] final object TryInstances { @inline final def castThrow[A](t: Throw[_]): Try[A] = t.asInstanceOf[Try[A]] } private[util] trait TryInstances1 { implicit final def twitterTryMonoid[A](implicit A: Monoid[A]): Monoid[Try[A]] = new TrySemigroup[A] with Monoid[Try[A]] { final def empty: Try[A] = Return(A.empty) } } private[util] class TrySemigroup[A](implicit A: Semigroup[A]) extends Semigroup[Try[A]] { final def combine(fx: Try[A], fy: Try[A]): Try[A] = fx.flatMap(x => fy.map(y => A.combine(x, y))) }
Example 153
Source File: ProofOfWork.scala From scalachain with MIT License | 5 votes |
package com.elleflorio.scalachain.proof import com.elleflorio.scalachain.crypto.Crypto import spray.json.DefaultJsonProtocol._ import spray.json._ import scala.annotation.tailrec object ProofOfWork { def proofOfWork(lastHash: String): Long = { @tailrec def powHelper(lastHash: String, proof: Long): Long = { if (validProof(lastHash, proof)) proof else powHelper(lastHash, proof + 1) } val proof = 0 powHelper(lastHash, proof) } def validProof(lastHash: String, proof: Long): Boolean = { val guess = (lastHash ++ proof.toString).toJson.toString() val guessHash = Crypto.sha256Hash(guess) (guessHash take 4) == "0000" } }
Example 154
Source File: AwsClientForName.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion.client import scala.annotation.tailrec import scala.collection.JavaConverters._ import com.amazonaws.services.datapipeline.DataPipeline import com.amazonaws.services.datapipeline.model.ListPipelinesRequest import com.krux.hyperion.DataPipelineDefGroup case class AwsClientForName( client: DataPipeline, pipelineName: String, override val maxRetry: Int, nameKeySeparator: String = DataPipelineDefGroup.DefaultNameKeySeparator ) extends AwsClient { lazy val pipelineIdNames: Map[String, String] = getPipelineIdNames() private def getPipelineIdNames(): Map[String, String] = { def inGroup(actualName: String): Boolean = ( actualName == pipelineName || actualName.startsWith(pipelineName + nameKeySeparator) ) @tailrec def queryPipelines( idNames: Map[String, String] = Map.empty, request: ListPipelinesRequest = new ListPipelinesRequest() ): Map[String, String] = { val response = client.listPipelines(request).retry() val theseIdNames = response.getPipelineIdList .asScala .collect { case idName if inGroup(idName.getName) => (idName.getId, idName.getName) } .toMap if (response.getHasMoreResults) queryPipelines( idNames ++ theseIdNames, new ListPipelinesRequest().withMarker(response.getMarker) ) else idNames ++ theseIdNames } def checkResults(idNames: Map[String, String]): Unit = { val names = idNames.values.toSet // if using Hyperion for all DataPipeline management, this should never happen if (names.size != idNames.size) throw new RuntimeException("Duplicated pipeline name") if (idNames.isEmpty) log.debug(s"Pipeline $pipelineName does not exist") } val result = queryPipelines() checkResults(result) result } def forId(): Option[AwsClientForId] = if (pipelineIdNames.isEmpty) None else Option(AwsClientForId(client, pipelineIdNames.keySet, maxRetry)) }
Example 155
Source File: Escapable.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion.common import scala.annotation.tailrec trait Escapable { @tailrec private def seekEndOfExpr( exp: String, quote: Option[Char] = None, expPart: StringBuilder = StringBuilder.newBuilder ): (String, String) = { if (exp.isEmpty) { throw new RuntimeException("Expression started but not ended") } else { val curChar = exp.head val next = exp.tail quote match { case Some(quoteChar) => // if is in quote seekEndOfExpr(next, quote.filter(_ != curChar), expPart += curChar) case _ => curChar match { case '}' => ((expPart += curChar).result, next) case '\'' | '"' => seekEndOfExpr(next, Option(curChar), expPart += curChar) case _ => seekEndOfExpr(next, None, expPart += curChar) } } } } def escape(s: String, c: Char): String = { def escapeChar(cc: Char): String = if (cc == c) s"\\\\$c" else cc.toString @tailrec def escapeRec( s: String, hashSpotted: Boolean = false, result: StringBuilder = StringBuilder.newBuilder ): String = { if (s.isEmpty) { result.toString } else { val curChar = s.head val sTail = s.tail if (!hashSpotted) { // outside an expression block escapeRec(sTail, curChar == '#', result ++= escapeChar(curChar)) } else { // the previous char is '#' if (curChar == '{') { // start of an expression val (blockBody, rest) = seekEndOfExpr(sTail) escapeRec(rest, false, result += curChar ++= blockBody) } else { // not start of an expression escapeRec(sTail, false, result ++= escapeChar(curChar)) } } } } escapeRec(s) } } object Escapable extends Escapable
Example 156
Source File: RawModel.scala From sparta with Apache License 2.0 | 5 votes |
package com.stratio.models.benchmark.generator.models import org.joda.time.DateTime import org.joda.time.format.DateTimeFormat import scala.annotation.tailrec import scala.io.Source import scala.util.Random case class RawModel (order_id: String, timestamp: String, client_id: Integer, latitude: Double, longitude: Double, payment_method: String, credit_card: String, shopping_center: String, employee: Integer) {} object RawModel { val Range_client_id = (1, 300) val Range_payment_method = Source.fromInputStream( this.getClass.getClassLoader.getResourceAsStream("payment-methods.txt")).getLines().toSeq val Range_shopping_center = Source.fromInputStream( this.getClass.getClassLoader.getResourceAsStream("shopping-centers.txt")).getLines().toSeq val Range_employee = (1, 300) val Range_quantity = (1, 30) val Range_timestap = (0, 60) val Range_creditCard = (0, 9) val R = Random val DigitsCreditCard = 16 val Range_family_product: Map[String, Map[String,Float]] = Source.fromInputStream( this.getClass.getClassLoader.getResourceAsStream("family-products.csv")).getLines().map(x => { val splitted = x.split(",") (splitted(0), Map(splitted(1) -> splitted(2).toFloat)) }).toMap def generateShoppingCenter(): String = { Range_shopping_center(generateRandomInt(0, Range_shopping_center.length - 1)) } def generatePaymentMethod(): String = { Range_payment_method(generateRandomInt(0, Range_payment_method.length - 1)) } def generateTimestamp(): String = { val datetime = new DateTime().minusDays(generateRandomInt(Range_timestap._1, Range_timestap._2)) DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(datetime) } def generateRandomInt(min: Int, max: Int): Int = { R.nextInt((max -min) + 1) + min } @tailrec def generateCreditCard(current: String): String = { if(current.length != DigitsCreditCard) generateCreditCard(current + generateRandomInt(Range_creditCard._1, Range_creditCard._2)) else current } } trait RawModelCommonData { val geolocations = initGeolocations() val clientIdCreditCard: Map[Int, String] = initClientIdCreditCard((1 to RawModel.Range_client_id._2).toSeq, Map()) val clientIdGeo: Map[Int, (Double, Double)] = initClientIdGeo(clientIdCreditCard, geolocations) def initGeolocations() : Seq[String] = { Source.fromInputStream( this.getClass.getClassLoader.getResourceAsStream("geolocations.csv")).getLines().toSeq } def initClientIdCreditCard(idClients: Seq[Int], clientIdCreditCard: Map[Int, String]): Map[Int, String] = { if(idClients.size == 0) { clientIdCreditCard } else { val newIdClients = idClients.init val newClientIdCreditCard = clientIdCreditCard + (idClients.last -> RawModel.generateCreditCard("")) initClientIdCreditCard(newIdClients, newClientIdCreditCard) } } def initClientIdGeo(clientIdCreditCard: Map[Int, String], geolocations: Seq[String]) :Map[Int, (Double, Double)] = { clientIdCreditCard.map(x => { val index = RawModel.generateRandomInt(0, geolocations.size - 1) x._1 -> ((geolocations(index)).split(":")(0).toDouble, (geolocations(index)).split(":")(1).toDouble) }) } }
Example 157
Source File: LoggerMacros.scala From log4cats with Apache License 2.0 | 5 votes |
package io.chrisdavenport.log4cats.slf4j.internal import scala.annotation.tailrec import scala.reflect.macros.blackbox loggerBySymbolName(s) } else { val typeArgs = List.fill(typeParams.length)(WildcardType) val typeConstructor = tq"$typeSymbol[..${typeArgs}]" loggerByParam(q"_root_.scala.Predef.classOf[$typeConstructor]") } } } @inline def isInnerClass(s: Symbol) = s.isClass && !(s.owner.isPackage) val instanceByName = Slf4jLoggerInternal.singletonsByName && (cls.isModule || cls.isModuleClass) || cls.isClass && isInnerClass( cls ) if (instanceByName) { loggerBySymbolName(cls) } else { loggerByType(cls) } } }
Example 158
Source File: TestingLogger.scala From log4cats with Apache License 2.0 | 5 votes |
package io.chrisdavenport.log4cats.testing import io.chrisdavenport.log4cats.{SelfAwareLogger} import cats.effect.Sync import cats.implicits._ import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec trait TestingLogger[F[_]] extends SelfAwareLogger[F] { import TestingLogger.LogMessage def logged: F[Vector[LogMessage]] } object TestingLogger { sealed trait LogMessage { def message: String def throwOpt: Option[Throwable] } final case class TRACE(message: String, throwOpt: Option[Throwable]) extends LogMessage final case class DEBUG(message: String, throwOpt: Option[Throwable]) extends LogMessage final case class INFO(message: String, throwOpt: Option[Throwable]) extends LogMessage final case class WARN(message: String, throwOpt: Option[Throwable]) extends LogMessage final case class ERROR(message: String, throwOpt: Option[Throwable]) extends LogMessage def impl[F[_]: Sync]( traceEnabled: Boolean = true, debugEnabled: Boolean = true, infoEnabled: Boolean = true, warnEnabled: Boolean = true, errorEnabled: Boolean = true ): TestingLogger[F] = { val ar = new AtomicReference(Vector.empty[LogMessage]) def appendLogMessage(m: LogMessage): F[Unit] = Sync[F].delay { @tailrec def mod(): Unit = { val c = ar.get val u = c :+ m if (!ar.compareAndSet(c, u)) mod else () } mod() } new TestingLogger[F] { def logged: F[Vector[LogMessage]] = Sync[F].delay(ar.get) def isTraceEnabled: F[Boolean] = Sync[F].pure(traceEnabled) def isDebugEnabled: F[Boolean] = Sync[F].pure(debugEnabled) def isInfoEnabled: F[Boolean] = Sync[F].pure(infoEnabled) def isWarnEnabled: F[Boolean] = Sync[F].pure(warnEnabled) def isErrorEnabled: F[Boolean] = Sync[F].pure(errorEnabled) def error(message: => String): F[Unit] = if (errorEnabled) appendLogMessage(ERROR(message, None)) else Sync[F].pure(()) def error(t: Throwable)(message: => String): F[Unit] = if (errorEnabled) appendLogMessage(ERROR(message, t.some)) else Sync[F].pure(()) def warn(message: => String): F[Unit] = if (warnEnabled) appendLogMessage(WARN(message, None)) else Sync[F].pure(()) def warn(t: Throwable)(message: => String): F[Unit] = if (warnEnabled) appendLogMessage(WARN(message, t.some)) else Sync[F].pure(()) def info(message: => String): F[Unit] = if (infoEnabled) appendLogMessage(INFO(message, None)) else Sync[F].pure(()) def info(t: Throwable)(message: => String): F[Unit] = if (infoEnabled) appendLogMessage(INFO(message, t.some)) else Sync[F].pure(()) def debug(message: => String): F[Unit] = if (debugEnabled) appendLogMessage(DEBUG(message, None)) else Sync[F].pure(()) def debug(t: Throwable)(message: => String): F[Unit] = if (debugEnabled) appendLogMessage(DEBUG(message, t.some)) else Sync[F].pure(()) def trace(message: => String): F[Unit] = if (traceEnabled) appendLogMessage(TRACE(message, None)) else Sync[F].pure(()) def trace(t: Throwable)(message: => String): F[Unit] = if (traceEnabled) appendLogMessage(TRACE(message, t.some)) else Sync[F].pure(()) } } }
Example 159
Source File: ByteArrays.scala From scala-stellar-sdk with Apache License 2.0 | 5 votes |
package stellar.sdk.util import java.security.MessageDigest import okio.ByteString import org.apache.commons.codec.binary.Base64 import scala.annotation.tailrec import scala.language.implicitConversions object ByteArrays { object Implicits { implicit def byteArrayToByteString(arr: Array[Byte]): ByteString = new ByteString(arr) implicit def byteStringToByteArray(byteString: ByteString): Array[Byte] = byteString.toByteArray } def paddedByteArray(bs: Array[Byte], length: Int): Array[Byte] = { val padded = Array.ofDim[Byte](math.max(length, bs.length)) System.arraycopy(bs, 0, padded, 0, bs.length) padded } def paddedByteArray(s: String, length: Int): Array[Byte] = paddedByteArray(s.getBytes("US-ASCII"), length) def paddedByteArrayToString(bs: Array[Byte]): String = new String(bs, "US-ASCII").split("\u0000")(0) def trimmedByteArray(bs: Array[Byte]): Seq[Byte] = trimmedByteArray(bs.toIndexedSeq) def trimmedByteArray(bs: Seq[Byte]): Seq[Byte] = bs.reverse.dropWhile(_ == 0).reverse def sha256(bs: Array[Byte]): Array[Byte] = sha256(bs.toIndexedSeq) def sha256(bs: Seq[Byte]): Array[Byte] = { val md = MessageDigest.getInstance("SHA-256") md.update(bs.toArray) md.digest } def base64(bs: Seq[Byte]): String = base64(bs.toArray) def base64(bs: Array[Byte]): String = Base64.encodeBase64String(bs) def base64(s: String): Array[Byte] = Base64.decodeBase64(s) def bytesToHex(bs: Array[Byte]): String = bytesToHex(bs.toIndexedSeq) def bytesToHex(bs: Seq[Byte]): String = bs.map("%02X".format(_)).mkString def hexToBytes(hex: String): Seq[Byte] = hex.toSeq.sliding(2, 2).map(_.unwrap).map(Integer.parseInt(_, 16).toByte).toIndexedSeq def checksum(bytes: Array[Byte]): Array[Byte] = { // This code calculates CRC16-XModem checksum // Ported from https://github.com/alexgorbatchev/node-crc, via https://github.com/stellar/java-stellar-sdk @tailrec def loop(bs: Seq[Byte], crc: Int): Int = { bs match { case h +: t => var code = crc >>> 8 & 0xFF code ^= h & 0xFF code ^= code >>> 4 var crc_ = crc << 8 & 0xFFFF crc_ ^= code code = code << 5 & 0xFFFF crc_ ^= code code = code << 7 & 0xFFFF crc_ ^= code loop(t, crc_) case Nil => crc } } val crc = loop(bytes.toIndexedSeq, 0x0000) Array(crc.toByte, (crc >>> 8).toByte) } }
Example 160
Source File: MagicManager.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.magic import org.apache.toree.plugins.{Plugin, PluginMethodResult, PluginManager} import org.slf4j.LoggerFactory import scala.annotation.tailrec import scala.language.dynamics import scala.runtime.BoxedUnit import scala.util.{Try, Failure, Success} class MagicManager(private val pluginManager: PluginManager) extends Dynamic { protected val logger = LoggerFactory.getLogger(this.getClass.getName) @throws[MagicNotFoundException] def findMagic(name: String): Magic = { @tailrec def inheritsMagic(klass: Class[_]): Boolean = { if (klass == null) false else if (klass.getInterfaces.exists(classOf[Magic].isAssignableFrom)) true else inheritsMagic(klass.getSuperclass) } val magics = pluginManager.plugins .filter(p => inheritsMagic(p.getClass)) .filter(_.simpleName.split("\\.").last.toLowerCase == name.toLowerCase) if (magics.size <= 0){ logger.error(s"No magic found for $name!") throw new MagicNotFoundException(name) } else if (magics.size > 1) { logger.warn(s"More than one magic found for $name!") } magics.head.asInstanceOf[Magic] } @throws[MagicNotFoundException] def applyDynamic(name: String)(args: Any*): MagicOutput = { val arg = args.headOption.map(_.toString).getOrElse("") import org.apache.toree.plugins.Implicits._ val result = pluginManager.fireEventFirstResult( name.toLowerCase(), "input" -> arg ) result match { case Some(r: PluginMethodResult) => handleMagicResult(name, r.toTry) case None => throw new MagicNotFoundException(name) } } private def handleMagicResult(name: String, result: Try[Any]): MagicOutput = result match { case Success(magicOutput) => magicOutput match { case out: MagicOutput => out case null | _: BoxedUnit => MagicOutput() case cmo: Map[_, _] if cmo.keys.forall(_.isInstanceOf[String]) && cmo.values.forall(_.isInstanceOf[String]) => MagicOutput(cmo.asInstanceOf[Map[String, String]].toSeq:_*) case unknown => val message = s"""Magic $name did not return proper magic output |type. Expected ${classOf[MagicOutput].getName}, but found |type of ${unknown.getClass.getName}.""".trim.stripMargin logger.warn(message) MagicOutput("text/plain" -> message) } case Failure(t) => val message = s"Magic $name failed to execute with error: \n${t.getMessage}" logger.warn(message, t) MagicOutput("text/plain" -> message) } }
Example 161
Source File: RawAddrMessageSerializer.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.serializers.messages.control import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.serializers.RawBitcoinSerializer import org.bitcoins.core.util.BitcoinSUtil import org.bitcoins.spvnode.messages.AddrMessage import org.bitcoins.spvnode.messages.control.AddrMessage import org.bitcoins.spvnode.util.NetworkIpAddress import scala.annotation.tailrec private def parseNetworkIpAddresses(ipCount : CompactSizeUInt, bytes : Seq[Byte]) : (Seq[NetworkIpAddress], Seq[Byte]) = { @tailrec def loop(remainingAddresses : BigInt, remainingBytes : Seq[Byte], accum : List[NetworkIpAddress]) : (Seq[NetworkIpAddress], Seq[Byte]) = { if (remainingAddresses <= 0) (accum.reverse, remainingBytes) else { val networkIpAddress = RawNetworkIpAddressSerializer.read(remainingBytes) val newRemainingBytes = remainingBytes.slice(networkIpAddress.size, remainingBytes.size) loop(remainingAddresses - 1, newRemainingBytes, networkIpAddress :: accum) } } loop(ipCount.num.toInt,bytes, List()) } } object RawAddrMessageSerializer extends RawAddrMessageSerializer
Example 162
Source File: RawGetBlocksMessageSerializer.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.serializers.messages.data import org.bitcoins.core.crypto.DoubleSha256Digest import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.serializers.RawBitcoinSerializer import org.bitcoins.core.util.BitcoinSUtil import org.bitcoins.spvnode.messages.GetBlocksMessage import org.bitcoins.spvnode.messages.data.GetBlocksMessage import org.bitcoins.spvnode.versions.ProtocolVersion import scala.annotation.tailrec private def parseBlockHeaders(bytes : Seq[Byte],compactSizeUInt: CompactSizeUInt) : (Seq[DoubleSha256Digest],Seq[Byte]) = { @tailrec def loop(remainingHeaders : Long, accum : List[DoubleSha256Digest], remainingBytes : Seq[Byte]) : (List[DoubleSha256Digest], Seq[Byte]) = { if (remainingHeaders <= 0) (accum.reverse,remainingBytes) else loop(remainingHeaders - 1, DoubleSha256Digest(remainingBytes.slice(0,32)) :: accum, remainingBytes.slice(32,remainingBytes.size)) } loop(compactSizeUInt.num.toInt, List(), bytes) } } object RawGetBlocksMessageSerializer extends RawGetBlocksMessageSerializer
Example 163
Source File: RawGetHeadersMessageSerializer.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.serializers.messages.data import org.bitcoins.core.crypto.DoubleSha256Digest import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.serializers.RawBitcoinSerializer import org.bitcoins.spvnode.messages.GetHeadersMessage import org.bitcoins.spvnode.messages.data.GetHeadersMessage import org.bitcoins.spvnode.versions.ProtocolVersion import scala.annotation.tailrec private def parseHashes(bytes : Seq[Byte], numHashes : CompactSizeUInt): (Seq[DoubleSha256Digest], Seq[Byte]) = { @tailrec def loop(remainingBytes : Seq[Byte], remainingHashes : Long, accum : Seq[DoubleSha256Digest]): (Seq[DoubleSha256Digest], Seq[Byte]) = { if (remainingHashes <= 0) (accum.reverse, remainingBytes) else { val hash = DoubleSha256Digest(remainingBytes.take(32)) loop(remainingBytes.slice(32,remainingBytes.length), remainingHashes-1, hash +: accum) } } loop(bytes, numHashes.num.toInt, Seq()) } } object RawGetHeadersMessageSerializer extends RawGetHeadersMessageSerializer
Example 164
Source File: RawHeadersMessageSerializer.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.serializers.messages.data import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.protocol.blockchain.BlockHeader import org.bitcoins.core.serializers.RawBitcoinSerializer import org.bitcoins.spvnode.messages.HeadersMessage import org.bitcoins.spvnode.messages.data.HeadersMessage import scala.annotation.tailrec trait RawHeadersMessageSerializer extends RawBitcoinSerializer[HeadersMessage] { def read(bytes: List[Byte]): HeadersMessage = { val compactSizeUInt = CompactSizeUInt.parseCompactSizeUInt(bytes) val headerStartIndex = compactSizeUInt.size.toInt val headerBytes = bytes.slice(headerStartIndex, bytes.length) val headers = parseBlockHeaders(headerBytes,compactSizeUInt) HeadersMessage(compactSizeUInt,headers) } def write(headersMessage: HeadersMessage): String = { headersMessage.count.hex + headersMessage.headers.map(_.hex + "00").mkString } private def parseBlockHeaders(bytes: Seq[Byte], compactSizeUInt: CompactSizeUInt): Seq[BlockHeader] = { @tailrec def loop(remainingBytes: Seq[Byte], remainingHeaders: Long, accum: List[BlockHeader]): Seq[BlockHeader] = { if (remainingHeaders <= 0) accum //81 is because HeadersMessage appends 0x00 at the end of every block header for some reason //read https://bitcoin.org/en/developer-reference#headers else { require(remainingBytes.size >= 80, "We do not have enough bytes for another block header, this probably means a tcp frame was not aligned") loop(remainingBytes.slice(81,remainingBytes.length), remainingHeaders - 1, BlockHeader(remainingBytes.take(80)) :: accum) } } loop(bytes,compactSizeUInt.num.toInt, List()).reverse } } object RawHeadersMessageSerializer extends RawHeadersMessageSerializer
Example 165
Source File: RawInventoryMessageSerializer.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.serializers.messages.data import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.serializers.RawBitcoinSerializer import org.bitcoins.core.util.BitcoinSUtil import org.bitcoins.spvnode.messages.InventoryMessage import org.bitcoins.spvnode.messages.data.{Inventory, InventoryMessage} import scala.annotation.tailrec private def parseInventories(bytes : Seq[Byte], requiredInventories : CompactSizeUInt) : (Seq[Inventory], Seq[Byte]) = { @tailrec def loop(remainingInventories : Long, remainingBytes : Seq[Byte], accum : List[Inventory]) : (Seq[Inventory], Seq[Byte]) = { if (remainingInventories <= 0) (accum.reverse,remainingBytes) else { val inventory = RawInventorySerializer.read(remainingBytes.slice(0,36)) loop(remainingInventories - 1, remainingBytes.slice(36,remainingBytes.size), inventory :: accum ) } } loop(requiredInventories.num.toInt, bytes, List()) } } object RawInventoryMessageSerializer extends RawInventoryMessageSerializer
Example 166
Source File: RpcUtil.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.rpc.util import java.net.ServerSocket import akka.actor.ActorSystem import org.bitcoins.rpc.client.common.BitcoindRpcClient import scala.annotation.tailrec import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration.DurationInt import scala.util.{Failure, Random, Success, Try} abstract class RpcUtil extends AsyncUtil { def awaitServerShutdown( server: BitcoindRpcClient, duration: FiniteDuration = 300.milliseconds, maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = { retryUntilSatisfiedF(() => server.isStoppedF, duration, maxTries) } @tailrec final def randomPort: Int = { val MAX = 65535 // max tcp port number val MIN = 1025 // lowest port not requiring sudo val port = Math.abs(Random.nextInt(MAX - MIN) + (MIN + 1)) val attempt = Try { val socket = new ServerSocket(port) socket.close() socket.getLocalPort } attempt match { case Success(value) => value case Failure(_) => randomPort } } } object RpcUtil extends RpcUtil
Example 167
Source File: Bech32Spec.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.protocol import org.bitcoins.core.util.Bech32 import org.bitcoins.testkit.core.gen.ln.LnInvoiceGen import org.bitcoins.testkit.core.gen.{ AddressGenerator, ChainParamsGenerator, ScriptGenerators } import org.scalacheck.{Prop, Properties} import scala.annotation.tailrec import scala.util.{Random, Success} class Bech32Spec extends Properties("Bech32Spec") { property("split all LN invoices into HRP and data") = { Prop.forAll(LnInvoiceGen.lnInvoice) { invoice => val splitT = Bech32.splitToHrpAndData(invoice.toString) splitT.isSuccess } } property("split all Bech32 addresses into HRP and data") = { Prop.forAll(AddressGenerator.bech32Address) { address => val splitT = Bech32.splitToHrpAndData(address.value) splitT.isSuccess } } property("serialization symmetry") = { Prop.forAll(ScriptGenerators.witnessScriptPubKey, ChainParamsGenerator.networkParams) { case ((witSPK, _), network) => val addr = Bech32Address(witSPK, network) val spk = Bech32Address.fromStringToWitSPK(addr.value) spk == Success(witSPK) } } property("checksum must not work if we modify a char") = { Prop.forAll(AddressGenerator.bech32Address) { addr: Bech32Address => val old = addr.value val rand = Math.abs(Random.nextInt) val idx = rand % old.length val (f, l) = old.splitAt(idx) val replacementChar = pickReplacementChar(l.head) val replaced = s"$f$replacementChar${l.tail}" //should fail because we replaced a char in the addr, so checksum invalid Bech32Address.fromStringT(replaced).isFailure } } property("must fail if we have a mixed case") = { Prop.forAllNoShrink(AddressGenerator.bech32Address) { addr: Bech32Address => val old = addr.value val replaced = switchCaseRandChar(old) //should fail because we we switched the case of a random char val actual = Bech32Address.fromStringT(replaced) actual.isFailure } } @tailrec private def pickReplacementChar(oldChar: Char): Char = { val rand = Math.abs(Random.nextInt) val newChar = Bech32.charset(rand % Bech32.charset.size) //make sure we don't pick the same char we are replacing in the bech32 address if (oldChar == newChar) pickReplacementChar(oldChar) else newChar } @tailrec private def switchCaseRandChar(addr: String): String = { val rand = Math.abs(Random.nextInt) val idx = rand % addr.length val (f, l) = addr.splitAt(idx) if (l.head.isDigit) { switchCaseRandChar(addr) } else { val middle = if (l.head.isUpper) { l.head.toLower } else { l.head.toUpper } s"$f$middle${l.tail}" } } }
Example 168
Source File: SchnorrNonce.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.crypto import scodec.bits.ByteVector import scala.annotation.tailrec import scala.util.Try case class SchnorrNonce(bytes: ByteVector) extends NetworkElement { require(bytes.length == 32, s"Schnorr nonce must be 32 bytes, get $bytes") private val evenKey: ECPublicKey = ECPublicKey(s"02$hex") private val oddKey: ECPublicKey = ECPublicKey(s"03$hex") private val yCoordEven: Boolean = { evenKey.toPoint.getRawYCoord.sqrt() != null } def fromBipSchnorr( privKey: ECPrivateKey, message: ByteVector, auxRand: ByteVector): SchnorrNonce = { val k = kFromBipSchnorr(privKey, message, auxRand) k.publicKey.schnorrNonce } def apply(xCoor: FieldElement): SchnorrNonce = { SchnorrNonce(xCoor.bytes) } }
Example 169
Source File: SchnorrPublicKey.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.crypto import org.bitcoin.NativeSecp256k1 import scodec.bits.ByteVector import scala.annotation.tailrec import scala.util.Try case class SchnorrPublicKey(bytes: ByteVector) extends NetworkElement { require(bytes.length == 32, s"Schnorr public keys must be 32 bytes, got $bytes") require(Try(publicKey).isSuccess, s"Schnorr public key must be a valid x coordinate, got $bytes") // TODO: match on CryptoContext once secp version is added def verify(data: ByteVector, signature: SchnorrDigitalSignature): Boolean = { verifyWithBouncyCastle(data, signature) } def computeSigPointWithBouncyCastle( data: ByteVector, nonce: SchnorrNonce, compressed: Boolean = true): ECPublicKey = { BouncyCastleUtil.schnorrComputeSigPoint(data, nonce, this, compressed) } def publicKey: ECPublicKey = { val pubKeyBytes = ByteVector.fromByte(2) ++ bytes val validPubKey = CryptoContext.default match { case CryptoContext.LibSecp256k1 => NativeSecp256k1.isValidPubKey(pubKeyBytes.toArray) case CryptoContext.BouncyCastle => BouncyCastleUtil.validatePublicKey(pubKeyBytes) } require( validPubKey, s"Cannot construct schnorr public key from invalid x coordinate: $bytes") ECPublicKey(pubKeyBytes) } def xCoord: FieldElement = FieldElement(bytes) } object SchnorrPublicKey extends Factory[SchnorrPublicKey] { @tailrec def fromBytes(bytes: ByteVector): SchnorrPublicKey = { require(bytes.length <= 33, s"XOnlyPublicKey must be less than 33 bytes, got $bytes") if (bytes.length == 32) new SchnorrPublicKey(bytes) else if (bytes.length < 32) { // means we need to pad the private key with 0 bytes so we have 32 bytes SchnorrPublicKey.fromBytes(bytes.padLeft(32)) } else if (bytes.length == 33) { // this is for the case when java serialies a BigInteger to 33 bytes to hold the signed num representation SchnorrPublicKey.fromBytes(bytes.tail) } else { throw new IllegalArgumentException( "XOnlyPublicKey cannot be greater than 33 bytes in size, got: " + CryptoBytesUtil.encodeHex(bytes) + " which is of size: " + bytes.size) } } def apply(xCoor: FieldElement): SchnorrPublicKey = { SchnorrPublicKey(xCoor.bytes) } }
Example 170
Source File: LnUtil.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.protocol.ln.util import org.bitcoins.core.number.UInt5 import org.bitcoins.core.util.{Bech32, BitcoinSLogger} import scala.annotation.tailrec @tailrec final def decodeNumber(list: List[UInt5], accum: BigInt = 0): BigInt = { list match { case h :: Nil => decodeNumber(Nil, h.toInt + accum) case h :: t => val b32 = BigInt(32) val n = b32.pow(t.size) val newAccum = h.toBigInt * n + accum decodeNumber(t, newAccum) case Nil => accum } } } object LnUtil extends LnUtil
Example 171
Source File: BytesUtil.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.util import org.bitcoins.core.number.UInt64 import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.crypto.{CryptoBytesUtil, Factory, NetworkElement} import scodec.bits.ByteVector import scala.annotation.tailrec trait BytesUtil extends CryptoBytesUtil { def writeCmpctSizeUInt[T <: NetworkElement](ts: Seq[T]): ByteVector = { val serialized = ts.map(_.bytes).foldLeft(ByteVector.empty)(_ ++ _) val cmpct = CompactSizeUInt(UInt64(ts.size)) cmpct.bytes ++ serialized } def parseCmpctSizeUIntSeq[T <: NetworkElement]( bytes: ByteVector, factory: Factory[T]): (Vector[T], ByteVector) = { val count = CompactSizeUInt.parse(bytes) val payload = bytes.drop(count.byteSize.toInt) val builder = Vector.newBuilder[T] @tailrec def loop(remaining: ByteVector, counter: Int = 0): ByteVector = { if (counter == count.num.toInt) { remaining } else { val parsed = factory.fromBytes(remaining) val newRemaining = remaining.drop(parsed.byteSize) builder.+=(parsed) loop(newRemaining, counter + 1) } } val remaining = loop(payload) val result = builder.result() require( result.size == count.num.toInt, s"Could not parse the amount of required elements, got: ${result.size} required: ${count}") (result, remaining) } } object BytesUtil extends BytesUtil
Example 172
Source File: ConditionalPath.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.wallet.utxo import scala.annotation.tailrec sealed trait ConditionalPath { def headOption: Option[Boolean] } sealed trait Conditional extends ConditionalPath { def condition: Boolean override def headOption: Option[Boolean] = Some(condition) } object ConditionalPath { case object NoCondition extends ConditionalPath { override val headOption: Option[Boolean] = None } case class ConditionTrue(nextCondition: ConditionalPath) extends Conditional { override val condition: Boolean = true } case class ConditionFalse(nextCondition: ConditionalPath) extends Conditional { override val condition: Boolean = false } val nonNestedTrue: ConditionalPath = ConditionTrue(NoCondition) val nonNestedFalse: ConditionalPath = ConditionFalse(NoCondition) def toVector(conditionalPath: ConditionalPath): Vector[Boolean] = { @tailrec def loop( current: ConditionalPath, accum: Vector[Boolean]): Vector[Boolean] = { current match { case cond: ConditionTrue => loop(cond.nextCondition, accum :+ true) case cond: ConditionFalse => loop(cond.nextCondition, accum :+ false) case NoCondition => accum } } loop(conditionalPath, Vector.empty) } def fromBranch(branch: Vector[Boolean]): ConditionalPath = { if (branch.isEmpty) { NoCondition } else { if (branch.head) { ConditionTrue(fromBranch(branch.tail)) } else { ConditionFalse(fromBranch(branch.tail)) } } } }
Example 173
Source File: RawScriptWitnessParser.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.serializers.script import org.bitcoins.core.number.UInt64 import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.protocol.script.ScriptWitness import org.bitcoins.core.serializers.RawBitcoinSerializer import scodec.bits.ByteVector import scala.annotation.tailrec sealed abstract class RawScriptWitnessParser extends RawBitcoinSerializer[ScriptWitness] { def read(bytes: ByteVector): ScriptWitness = { //first byte is the number of stack items val stackSize = CompactSizeUInt.parseCompactSizeUInt(bytes) val (_, stackBytes) = bytes.splitAt(stackSize.byteSize.toInt) @tailrec def loop( remainingBytes: ByteVector, accum: Seq[ByteVector], remainingStackItems: UInt64): Seq[ByteVector] = { if (remainingStackItems <= UInt64.zero) accum else { val elementSize = CompactSizeUInt.parseCompactSizeUInt(remainingBytes) val (_, stackElementBytes) = remainingBytes.splitAt(elementSize.byteSize.toInt) val stackElement = stackElementBytes.take(elementSize.num.toInt) val (_, newRemainingBytes) = stackElementBytes.splitAt(stackElement.size) loop(newRemainingBytes, stackElement +: accum, remainingStackItems - UInt64.one) } } //note there is no 'reversing' the accum, in bitcoin-s we assume the top of the stack is the 'head' element in the sequence val stack = loop(stackBytes, Nil, stackSize.num) val witness = ScriptWitness(stack) witness } def write(scriptWitness: ScriptWitness): ByteVector = { @tailrec def loop( remainingStack: Seq[ByteVector], accum: Vector[ByteVector]): Vector[ByteVector] = { if (remainingStack.isEmpty) accum.reverse else { val compactSizeUInt: CompactSizeUInt = CompactSizeUInt.calc(remainingStack.head) val serialization: ByteVector = compactSizeUInt.bytes ++ remainingStack.head loop(remainingStack.tail, serialization +: accum) } } val stackItems: Vector[ByteVector] = loop(scriptWitness.stack.reverse, Vector.empty) val size = CompactSizeUInt(UInt64(stackItems.size)) (size.bytes +: stackItems).fold(ByteVector.empty)(_ ++ _) } } object RawScriptWitnessParser extends RawScriptWitnessParser
Example 174
Source File: RawTransactionWitnessParser.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.serializers.transaction import org.bitcoins.core.protocol.script.ScriptWitness import org.bitcoins.core.protocol.transaction.TransactionWitness import org.bitcoins.core.serializers.script.RawScriptWitnessParser import scodec.bits.ByteVector import scala.annotation.tailrec def read(bytes: ByteVector, numInputs: Int): TransactionWitness = { @tailrec def loop( remainingBytes: ByteVector, remainingInputs: Int, accum: Vector[ScriptWitness]): Vector[ScriptWitness] = { if (remainingInputs != 0) { val w = RawScriptWitnessParser.read(remainingBytes) val (_, newRemainingBytes) = remainingBytes.splitAt(w.bytes.size) loop(newRemainingBytes, remainingInputs - 1, w +: accum) } else accum.reverse } val witnesses = loop(bytes, numInputs, Vector.empty) require(witnesses.size == numInputs) TransactionWitness(witnesses) } def write(witness: TransactionWitness): ByteVector = { witness.foldLeft(ByteVector.empty)(_ ++ _.bytes) } } object RawTransactionWitnessParser extends RawTransactionWitnessParser
Example 175
Source File: IgnoreMacro.scala From diffx with Apache License 2.0 | 5 votes |
package com.softwaremill.diffx import scala.annotation.tailrec import scala.reflect.macros.blackbox object IgnoreMacro { private val ShapeInfo = "Path must have shape: _.field1.field2.each.field3.(...)" def ignoreMacro[T: c.WeakTypeTag, U: c.WeakTypeTag]( c: blackbox.Context )(path: c.Expr[T => U]): c.Tree = applyIgnored[T, U](c)(ignoredFromPathMacro(c)(path)) private def applyIgnored[T: c.WeakTypeTag, U: c.WeakTypeTag](c: blackbox.Context)( path: c.Expr[List[String]] ): c.Tree = { import c.universe._ q"""{ ${c.prefix}.ignoreUnsafe($path:_*) }""" } @tailrec def collectPathElements(tree: c.Tree, acc: List[PathElement]): List[PathElement] = { def typeSupported(diffxIgnoreType: c.Tree) = Seq("DiffxEach", "DiffxEither", "DiffxEachMap") .exists(diffxIgnoreType.toString.endsWith) tree match { case q"$parent.$child " => collectPathElements(parent, TermPathElement(child) :: acc) case q"$tpname[..$_]($t)($f) " if typeSupported(tpname) => val newAcc = acc match { // replace the term controlled by quicklens case TermPathElement(term, xargs @ _*) :: rest => FunctorPathElement(f, term, xargs: _*) :: rest case pathEl :: _ => c.abort(c.enclosingPosition, s"Invalid use of path element $pathEl. $ShapeInfo, got: ${path.tree}") case Nil => c.abort(c.enclosingPosition, s"Invalid use of path element(Nil). $ShapeInfo, got: ${path.tree}") } collectPathElements(t, newAcc) case t: Ident => acc case _ => c.abort(c.enclosingPosition, s"Unsupported path element. $ShapeInfo, got: $tree") } } val pathEls = path.tree match { case q"($arg) => $pathBody " => collectPathElements(pathBody, Nil) case _ => c.abort(c.enclosingPosition, s"$ShapeInfo, got: ${path.tree}") } c.Expr[List[String]](q"${pathEls.collect { case TermPathElement(c) => c.decodedName.toString }}") } private[diffx] def ignoredFromPath[T, U](path: T => U): List[String] = macro ignoredFromPathMacro[T, U] }
Example 176
Source File: MatchDecoder.scala From kantan.regex with Apache License 2.0 | 5 votes |
package kantan.regex import kantan.codecs.Decoder import kantan.codecs.DecoderCompanion import kantan.codecs.collection.Factory import scala.annotation.tailrec import scala.collection.mutable implicit def optMatch[A](implicit da: GroupDecoder[Option[A]]): MatchDecoder[Option[A]] = MatchDecoder.fromGroup[Option[A]](0)(GroupDecoder.from { os => da.decode(os.filter(_.nonEmpty)) }) // TODO: there *must* be a more elegant way to write this. // This is more of a hack and not terribly satisfactory, since regular expressions are much more limitted than I // initially assumed: matching "12345" against "(\d)*" will not result in 5 groups, but 2: "12345" and "5". implicit def fromCbf[F[_], A](implicit da: GroupDecoder[Option[A]], cbf: Factory[A, F[A]]): MatchDecoder[F[A]] = MatchDecoder.from { m => @tailrec def loop(i: Int, curr: DecodeResult[mutable.Builder[A, F[A]]]): DecodeResult[F[A]] = if(i > m.length || !curr.isRight) curr.map(_.result()) else loop(i + 1, for { fa <- curr a <- m.decode[Option[A]](i) } yield { a.foreach(fa += _) fa }) loop(1, DecodeResult.success(cbf.newBuilder)) } }
Example 177
Source File: CustomCodeEntryPoint.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.workflowexecutor.customcode import java.util.concurrent.TimeoutException import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{Await, Promise} import org.apache.spark.api.java.JavaSparkContext import org.apache.spark.sql.DataFrame import org.apache.spark.{SparkConf, SparkContext} import ai.deepsense.commons.utils.Logging import ai.deepsense.deeplang._ import ai.deepsense.sparkutils.SparkSQLSession class CustomCodeEntryPoint( val sparkContext: SparkContext, val sparkSQLSession: SparkSQLSession, val dataFrameStorage: DataFrameStorage, val operationExecutionDispatcher: OperationExecutionDispatcher) extends Logging { import ai.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint._ def getSparkContext: JavaSparkContext = sparkContext def getSparkSQLSession: SparkSQLSession = sparkSQLSession def getNewSparkSQLSession: SparkSQLSession = sparkSQLSession.newSession() def getSparkConf: SparkConf = sparkContext.getConf private val codeExecutor: AtomicReference[Promise[CustomCodeExecutor]] = new AtomicReference(Promise()) private val pythonPort: AtomicReference[Promise[Int]] = new AtomicReference(Promise()) def getCodeExecutor(timeout: Duration): CustomCodeExecutor = getFromPromise(codeExecutor.get, timeout) def getPythonPort(timeout: Duration): Int = getFromPromise(pythonPort.get, timeout) def registerCodeExecutor(newCodeExecutor: CustomCodeExecutor): Unit = replacePromise(codeExecutor, newCodeExecutor) def registerCallbackServerPort(newPort: Int): Unit = replacePromise(pythonPort, newPort) def retrieveInputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getInputDataFrame(workflowId, nodeId, portNumber).get def retrieveOutputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getOutputDataFrame(workflowId, nodeId, portNumber).get def registerOutputDataFrame( workflowId: String, nodeId: String, portNumber: Int, dataFrame: DataFrame): Unit = dataFrameStorage.setOutputDataFrame(workflowId, nodeId, portNumber, dataFrame) def executionCompleted(workflowId: String, nodeId: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Right(())) def executionFailed(workflowId: String, nodeId: String, error: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Left(error)) } object CustomCodeEntryPoint { private case class PromiseReplacedException() extends Exception @tailrec private def getFromPromise[T](promise: => Promise[T], timeout: Duration): T = { try { Await.result(promise.future, timeout) } catch { case e: TimeoutException => throw e case e: PromiseReplacedException => getFromPromise(promise, timeout) } } private def replacePromise[T](promise: AtomicReference[Promise[T]], newValue: T): Unit = { val oldPromise = promise.getAndSet(Promise.successful(newValue)) try { oldPromise.failure(new PromiseReplacedException) } catch { // The oldPromise will have been completed always, except for the first time. // The illegal state is expected, but we have to complete the oldPromise, // since someone might be waiting on it. case e: IllegalStateException => () } } case class CustomCodeEntryPointConfig( pyExecutorSetupTimeout: Duration = 5.seconds) }
Example 178
Source File: Disposer.scala From tensorflow_scala with Apache License 2.0 | 5 votes |
package org.platanios.tensorflow.api.utilities import java.lang.Thread.currentThread import java.lang.ref.{PhantomReference, Reference, ReferenceQueue} import java.security.{AccessController, PrivilegedAction} import java.util import java.util.concurrent.ConcurrentHashMap import scala.annotation.tailrec def add(target: Any, disposer: () => Unit ): Reference[Any] = { val reference = new PhantomReference(target, queue) records.put(reference, disposer) // TODO: make sure reference isn't GC'd before this point (e.g., with org.openjdk.jmh.infra.Blackhole::consume). reference } AccessController.doPrivileged(new PrivilegedAction[Unit] { override def run(): Unit = { // The thread must be a member of a thread group which will not get GCed before the VM exit. For this reason, we // make its parent the top-level thread group. @tailrec def rootThreadGroup(group: ThreadGroup = currentThread.getThreadGroup): ThreadGroup = { group.getParent match { case null => group case parent => rootThreadGroup(parent) } } new Thread(rootThreadGroup(), "TensorFlow Scala API Disposer") { override def run = while (true) { // Blocks until there is a reference in the queue. val referenceToDispose = queue.remove records.remove(referenceToDispose).apply() referenceToDispose.clear() } setContextClassLoader(null) setDaemon(true) setPriority(Thread.MAX_PRIORITY) start() } } }) }
Example 179
Source File: DebuggerInterpreterHelpers.scala From scala-debugger with Apache License 2.0 | 5 votes |
package org.scaladebugger.language.interpreters import scala.annotation.tailrec import org.scaladebugger.language.models trait DebuggerInterpreterHelpers { this: DebuggerInterpreter => def fillInArgs( args: Seq[(models.Identifier, models.Expression)], argNames: Seq[models.Identifier], scope: models.Scope ): Map[models.Identifier, models.Expression] = { // Map args based on name, falling back to order if name not provided val mappedArgs = args.zipWithIndex.map { case (t, index) => val (identifier, expression) = t (if (identifier.name.nonEmpty) identifier else argNames(index), expression) }.toMap // Fill in any missing arguments with undefined val missingArgs = argNames.diff(mappedArgs.keySet.toSeq) .map(_ -> models.Undefined).toMap val filledMappedArgs = mappedArgs ++ missingArgs filledMappedArgs.mapValues(eval(_: models.Expression, scope)) } type I = models.Identifier type E = models.Expression def toEvalArgs(scope: models.Scope, l: E, r: E): Seq[(I, E)] = Seq( models.Identifier("l") -> eval(l, scope), models.Identifier("r") -> eval(r, scope) ) def toBaseValue(expression: models.Expression): models.BaseValue = toBaseValue(expression, rootScope) @tailrec final def toBaseValue( expression: models.Expression, scope: models.Scope ): models.BaseValue = { expression match { case b: models.BaseValue => b case e => toBaseValue(eval(expression, scope), scope) } } def invokeOperator(name: String, scope: models.Scope, l: E, r: E): E = eval(models.FunctionCall( models.Identifier(name), toEvalArgs(scope, l, r) ), scope) def getVariable( name: String, scope: models.Scope ): models.Expression = scope.findVariable(models.Identifier(name)) match { case Some(v) => v case None => throw new RuntimeException(s"Variable $name does not exist!") } def getFunction( expression: models.Expression, scope: models.Scope ): models.Function = expression match { case f: models.Function => f case i: models.Identifier => scope.findVariable(i) match { case Some(f: models.Function) => f case Some(x) => throw new RuntimeException(s"Function expected, but $x found!") case None => throw new RuntimeException(s"Function ${i.name} does not exist!") } case e => throw new RuntimeException(s"Function expected, but $e found!") } }
Example 180
Source File: Path.scala From shield with MIT License | 5 votes |
package shield.routing import scala.annotation.tailrec import scala.util.matching.Regex object Path { def apply(path: String): Path = { new Path(PathTemplateParser.pathToSegments(path)) } } case class Path(segments: List[Segment]) extends Ordered[Path] { override def compare(that: Path): Int = { @tailrec def analyze(segments: (List[Segment], List[Segment])) : Int = { segments match { case (Nil, Nil) => 0 case (Nil, _) => 1 case (_, Nil) => -1 case (this_head :: this_tail, that_head :: that_tail) => (this_head, that_head) match { case (l, r) if l.priority != r.priority => l.priority - r.priority case (l, r) => analyze(this_tail, that_tail) } } } val comparison = analyze(segments, that.segments) if (comparison == 0) { segments.toString().compare(that.segments.toString()) } else { comparison } } override lazy val toString : String = { (Iterable("/") ++ segments.map(_.toString)).mkString("") } // todo: safely handle invalid regexs due to bad swagger documentation lazy val regex : Regex = { (Iterable("^/") ++ segments.map(_.regexPiece) ++ Iterable("/?$")).mkString("").r } }
Example 181
Source File: ProxyBalancer.scala From shield with MIT License | 5 votes |
package shield.proxying import akka.actor.{ActorContext, ActorRef, ActorRefFactory, PoisonPill} import akka.pattern.ask import akka.routing.RoundRobinGroup import akka.util.Timeout import shield.actors.Middleware import shield.actors.config.{ProxyState, WeightedProxyState} import shield.config.ServiceLocation import shield.routing.{EndpointTemplate, Param} import spray.http.{HttpRequest, HttpResponse} import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.Random case class ProxyRequest(template: EndpointTemplate, request: HttpRequest) case class ProxiedResponse(upstreamService: ServiceLocation, serviceName: String, template: EndpointTemplate, cacheParams: Set[Param], response: HttpResponse) trait ProxyBalancer { def proxy(template: EndpointTemplate, request: HttpRequest) : Future[ProxiedResponse] } object FailBalancer extends ProxyBalancer { def proxy(template: EndpointTemplate, request: HttpRequest): Future[ProxiedResponse] = Future.failed(new NotImplementedError()) } class AkkaBalancer(val balancer: ActorRef) extends ProxyBalancer { // todo: use global timeout config implicit val timeout = Timeout(60.seconds) def proxy(template: EndpointTemplate, request: HttpRequest) = (balancer ? ProxyRequest(template, request)).mapTo[ProxiedResponse] } trait ProxyBalancerBuilder[T <: ProxyBalancer] { val allMiddleware : List[Middleware] def build(actors: Set[ActorRef]) : ProxyBalancer def teardown() : Unit } object EmptyBalancerBuilder extends ProxyBalancerBuilder[ProxyBalancer] { val allMiddleware : List[Middleware] = Nil def build(actors: Set[ActorRef]) : ProxyBalancer = { FailBalancer } def teardown() : Unit = {} } // todo: weighted service instances (and dynamic weighting) // todo: retry safe gets (config option to enable) via something like http://doc.akka.io/docs/akka/snapshot/scala/routing.html#TailChoppingPool_and_TailChoppingGroup class RoundRobinBalancerBuilder(val allMiddleware: List[Middleware], factory: ActorRefFactory, hostProxies: Map[ActorRef, WeightedProxyState])(implicit execContext: ExecutionContext) extends ProxyBalancerBuilder[AkkaBalancer] { var balancers : List[ActorRef] = Nil // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations // nb: gcd is associative, so we're safe to `reduce` the results @tailrec private def gcd(a: Int, b: Int) : Int = if (b == 0) { a } else { gcd(b, a % b) } def build(actors: Set[ActorRef]) : AkkaBalancer = { // todo: refactor this out to somewhere common when we have other balancer types val actorWeight = actors.map(hostProxies(_).weight) val totalWeight = actorWeight.sum val group = if (totalWeight == 0) { actors.toList } else { val actorGCD = actorWeight.filter(_ != 0).reduceLeftOption(gcd).getOrElse(1) Random.shuffle(actors.toList.flatMap(a => List.fill(hostProxies(a).weight / actorGCD)(a))) } val balancer = factory.actorOf(RoundRobinGroup(group.map(_.path.toString)).props()) balancers = balancer :: balancers new AkkaBalancer(balancer) } def teardown() = { for (balancer <- balancers) { // just stop the router, not the host proxy behind them balancer ! PoisonPill } balancers = Nil } }
Example 182
Source File: PlayJavaClientCodeGenerator.scala From play-grpc with Apache License 2.0 | 5 votes |
package play.grpc.gen.javadsl import akka.grpc.gen.Logger import akka.grpc.gen.javadsl.JavaCodeGenerator import akka.grpc.gen.javadsl.Service import play.grpc.gen.scaladsl.PlayScalaClientCodeGenerator import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse import templates.PlayJava.txt.AkkaGrpcClientModule import templates.PlayJava.txt.ClientProvider import scala.annotation.tailrec import scala.collection.immutable import play.grpc.gen.scaladsl.PlayScalaClientCodeGenerator object PlayJavaClientCodeGenerator extends PlayJavaClientCodeGenerator class PlayJavaClientCodeGenerator extends JavaCodeGenerator { override def name: String = "play-grpc-client-java" override def perServiceContent = super.perServiceContent + generateClientProvider private val generateClientProvider: (Logger, Service) => immutable.Seq[CodeGeneratorResponse.File] = (logger, service) => { val b = CodeGeneratorResponse.File.newBuilder() b.setContent(ClientProvider(service).body) b.setName(s"${service.packageName.replace('.', '/')}/${service.name}ClientProvider.java") logger.info(s"Generating Play gRPC client provider for ${service.packageName}.${service.name}") immutable.Seq(b.build) } override def staticContent(logger: Logger, allServices: Seq[Service]): Set[CodeGeneratorResponse.File] = { if (allServices.nonEmpty) { val packageName = packageForSharedModuleFile(allServices) val b = CodeGeneratorResponse.File.newBuilder() b.setContent(AkkaGrpcClientModule(packageName, allServices).body) b.setName(s"${packageName.replace('.', '/')}/${PlayScalaClientCodeGenerator.ClientModuleName}.java") val set = Set(b.build) logger.info( s"Generated [${packageName}.${PlayScalaClientCodeGenerator.ClientModuleName}] add it to play.modules.enabled and a section " + "with Akka gRPC client config under akka.grpc.client.\"servicepackage.ServiceName\" to be able to inject " + "client instances.", ) set } else Set.empty } private[play] def packageForSharedModuleFile(allServices: Seq[Service]): String = // single service or all services in single package - use that if (allServices.forall(_.packageName == allServices.head.packageName)) allServices.head.packageName else { // try to find longest common prefix allServices.tail.foldLeft(allServices.head.packageName)((packageName, service) => if (packageName == service.packageName) packageName else PlayScalaClientCodeGenerator.commonPackage(packageName, service.packageName), ) } }
Example 183
Source File: PlayScalaClientCodeGenerator.scala From play-grpc with Apache License 2.0 | 5 votes |
package play.grpc.gen.scaladsl import scala.collection.immutable import akka.grpc.gen.Logger import akka.grpc.gen.scaladsl.ScalaCodeGenerator import akka.grpc.gen.scaladsl.Service import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse import templates.PlayScala.txt.AkkaGrpcClientModule import templates.PlayScala.txt.ClientProvider import scala.annotation.tailrec object PlayScalaClientCodeGenerator extends PlayScalaClientCodeGenerator class PlayScalaClientCodeGenerator extends ScalaCodeGenerator { val ClientModuleName = "AkkaGrpcClientModule" override def name: String = "play-grpc-client-scala" override def perServiceContent = super.perServiceContent + generateClientProvider private val generateClientProvider: (Logger, Service) => immutable.Seq[CodeGeneratorResponse.File] = (logger, service) => { val b = CodeGeneratorResponse.File.newBuilder() b.setContent(ClientProvider(service).body) b.setName(s"${service.packageName.replace('.', '/')}/${service.name}ClientProvider.scala") logger.info(s"Generating Play gRPC play client provider for ${service.packageName}.${service.name}") immutable.Seq(b.build) } override def staticContent(logger: Logger, allServices: Seq[Service]): Set[CodeGeneratorResponse.File] = { if (allServices.nonEmpty) { val packageName = packageForSharedModuleFile(allServices) val b = CodeGeneratorResponse.File.newBuilder() b.setContent(AkkaGrpcClientModule(packageName, allServices).body) b.setName(s"${packageName.replace('.', '/')}/${ClientModuleName}.scala") val set = Set(b.build) logger.info( s"Generated [${packageName}.${ClientModuleName}] add it to play.modules.enabled and a section " + "with Akka gRPC client config under akka.grpc.client.\"servicepackage.ServiceName\" to be able to inject " + "client instances.", ) set } else Set.empty } def packageForSharedModuleFile(allServices: Seq[Service]): String = // single service or all services in single package - use that if (allServices.forall(_.packageName == allServices.head.packageName)) allServices.head.packageName else { // try to find longest common prefix allServices.tail.foldLeft(allServices.head.packageName)((packageName, service) => if (packageName == service.packageName) packageName else commonPackage(packageName, service.packageName), ) } def commonPackage(a: String, b: String): String = { val aPackages = a.split('.') val bPackages = b.split('.') @tailrec def countIdenticalPackage(pos: Int): Int = { if ((aPackages.length < pos + 1) || (bPackages.length < pos + 1)) pos else if (aPackages(pos) == bPackages(pos)) countIdenticalPackage(pos + 1) else pos } val prefixLength = countIdenticalPackage(0) if (prefixLength == 0) "" // no common, use root package else aPackages.take(prefixLength).mkString(".") } }
Example 184
Source File: IOUtils.scala From OUTDATED_ledger-wallet-android with MIT License | 5 votes |
package co.ledger.wallet.core.utils.io import java.io._ import co.ledger.wallet.core.utils.logs.Logger import scala.annotation.tailrec object IOUtils { val BufferSize = 8192 @tailrec def copy(source: InputStream, destination: OutputStream, buffer: Array[Byte] = new Array(BufferSize), progress: (Long) => Unit = _ => ()) : Unit = { require(source != null) require(destination != null) var read: Int = 0 read = source.read(buffer) if (read == -1) return destination.write(buffer, 0, read) progress(read) copy(source, destination, buffer, progress) } }
Example 185
Source File: Atomic.scala From spark1.52 with Apache License 2.0 | 5 votes |
package ch7 import org.learningconcurrency._ import ch7._ object AtomicHistoryBad extends App { import java.util.concurrent.atomic._ import scala.annotation.tailrec import scala.concurrent._ import ExecutionContext.Implicits.global val urls = new AtomicReference[List[String]](Nil) val clen = new AtomicInteger(0) def addUrl(url: String): Unit = { @tailrec def append(): Unit = { val oldUrls = urls.get if (!urls.compareAndSet(oldUrls, url :: oldUrls)) append() } append() clen.addAndGet(url.length + 1) } def getUrlArray(): Array[Char] = { val array = new Array[Char](clen.get) val urlList = urls.get for ((character, i) <- urlList.map(_ + "\n").flatten.zipWithIndex) { array(i) = character } array } Future { try { log(s"sending: ${getUrlArray().mkString}") } catch { case e: Exception => log(s"problems getting the array $e") } } Future { addUrl("http://scala-lang.org") addUrl("https://github.com/scala/scala") addUrl("http://www.scala-lang.org/api") log("done browsing") } }
Example 186
Source File: RateLimitedOutputStream.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import scala.annotation.tailrec import java.io.OutputStream import java.util.concurrent.TimeUnit._ import org.apache.spark.Logging private[streaming] class RateLimitedOutputStream(out: OutputStream, desiredBytesPerSec: Int) extends OutputStream with Logging { require(desiredBytesPerSec > 0) private val SYNC_INTERVAL = NANOSECONDS.convert(10, SECONDS) private val CHUNK_SIZE = 8192 private var lastSyncTime = System.nanoTime private var bytesWrittenSinceSync = 0L override def write(b: Int) { waitToWrite(1) out.write(b) } override def write(bytes: Array[Byte]) { write(bytes, 0, bytes.length) } @tailrec override final def write(bytes: Array[Byte], offset: Int, length: Int) { val writeSize = math.min(length - offset, CHUNK_SIZE) if (writeSize > 0) { waitToWrite(writeSize) out.write(bytes, offset, writeSize) write(bytes, offset + writeSize, length) } } override def flush() { out.flush() } override def close() { out.close() } @tailrec private def waitToWrite(numBytes: Int) { val now = System.nanoTime val elapsedNanosecs = math.max(now - lastSyncTime, 1) val rate = bytesWrittenSinceSync.toDouble * 1000000000 / elapsedNanosecs if (rate < desiredBytesPerSec) { // It's okay to write; just update some variables and return bytesWrittenSinceSync += numBytes if (now > lastSyncTime + SYNC_INTERVAL) { // Sync interval has passed; let's resync lastSyncTime = now bytesWrittenSinceSync = numBytes } } else { // Calculate how much time we should sleep to bring ourselves to the desired rate. val targetTimeInMillis = bytesWrittenSinceSync * 1000 / desiredBytesPerSec val elapsedTimeInMillis = elapsedNanosecs / 1000000 val sleepTimeInMillis = targetTimeInMillis - elapsedTimeInMillis if (sleepTimeInMillis > 0) { logTrace("Natural rate is " + rate + " per second but desired rate is " + desiredBytesPerSec + ", sleeping for " + sleepTimeInMillis + " ms to compensate.") Thread.sleep(sleepTimeInMillis) } waitToWrite(numBytes) } } }
Example 187
Source File: ClockSkewFromParentTransformer.scala From haystack-traces with Apache License 2.0 | 5 votes |
package com.expedia.www.haystack.trace.reader.readers.transformers import com.expedia.open.tracing.Span import com.expedia.www.haystack.trace.commons.utils.SpanUtils import com.expedia.www.haystack.trace.reader.readers.utils.{MutableSpanForest, SpanTree} import scala.annotation.tailrec import scala.collection.mutable.ListBuffer import scala.collection.{Seq, mutable} class ClockSkewFromParentTransformer extends SpanTreeTransformer { case class SpanTreeWithParent(spanTree: SpanTree, parent: Option[Span]) override def transform(forest: MutableSpanForest): MutableSpanForest = { val underlyingSpans = new mutable.ListBuffer[Span] forest.getAllTrees.foreach(tree => { adjustSkew(underlyingSpans, List(SpanTreeWithParent(tree, None))) }) forest.updateUnderlyingSpans(underlyingSpans) } @tailrec private def adjustSkew(fixedSpans: ListBuffer[Span], spanTrees: Seq[SpanTreeWithParent]): Unit = { if (spanTrees.isEmpty) return // collect the child trees that need to be corrected for clock skew val childTrees = mutable.ListBuffer[SpanTreeWithParent]() spanTrees.foreach(e => { val rootSpan = e.spanTree.span var adjustedSpan = rootSpan e.parent match { case Some(parentSpan) => adjustedSpan = adjustSpan(rootSpan, parentSpan) fixedSpans += adjustedSpan case _ => fixedSpans += rootSpan } childTrees ++= e.spanTree.children.map(tree => SpanTreeWithParent(tree, Some(adjustedSpan))) }) adjustSkew(fixedSpans, childTrees) } private def adjustSpan(child: Span, parent: Span): Span = { var shift = 0L if (child.getStartTime < parent.getStartTime) { shift = parent.getStartTime - child.getStartTime } val childEndTime = SpanUtils.getEndTime(child) val parentEndTime = SpanUtils.getEndTime(parent) if (parentEndTime < childEndTime + shift) { shift = parentEndTime - childEndTime } if (shift == 0L) { child } else { Span.newBuilder(child).setStartTime(child.getStartTime + shift).build() } } }
Example 188
Source File: Composer.scala From aecor with MIT License | 5 votes |
package aecor.data import scala.annotation.tailrec import scala.util.Try object Composer { final class WithSeparator(separator: Char) { private val escapeChar = '\\' def apply(firstComponent: String, secondComponent: String, otherComponents: String*): String = apply(firstComponent :: secondComponent :: otherComponents.toList) def apply(components: List[String]): String = { val builder = new java.lang.StringBuilder() var idx = 0 components.foreach { component => if (idx > 0) builder.append(separator) component.foreach { char => if (char == separator || char == escapeChar) { builder.append(escapeChar) } builder.append(char) } idx = idx + 1 } builder.toString } def unapply(value: String): Option[List[String]] = Some { var acc = List.empty[String] val component = new java.lang.StringBuilder(36) var escaped = false value.foreach { x => if (escaped) { component.append(x) escaped = false } else if (x == escapeChar) { escaped = true } else if (x == separator) { acc = component.toString :: acc component.setLength(0) } else { component.append(x) } } acc = component.toString :: acc acc.reverse } } object WithSeparator { def apply(separator: Char): WithSeparator = new WithSeparator(separator) } final class WithLengthHint(lengthSeparator: Char) { private val lengthSeparatorString = String.valueOf(lengthSeparator) def apply(firstComponent: String, secondComponent: String, otherComponents: String*): String = apply(firstComponent :: secondComponent :: otherComponents.toList) def apply(components: List[String]): String = { val builder = new java.lang.StringBuilder components.foreach { x => builder.append(x.length) builder.append(lengthSeparator) builder.append(x) } builder.toString } @tailrec private def decodeLoop(elements: Vector[String], string: String): Vector[String] = if (string.isEmpty) elements else { val firstSep = string.indexOf(lengthSeparatorString) val (sizeString, tail) = (string.take(firstSep), string.drop(firstSep + 1)) val size = sizeString.toInt val (value, remainder) = (tail.take(size), tail.drop(size)) decodeLoop(elements :+ value, remainder) } def unapply(string: String): Option[List[String]] = Try(decodeLoop(Vector.empty, string).toList).toOption } object WithLengthHint { def apply(lengthSeparator: Char): WithLengthHint = new WithLengthHint(lengthSeparator) } }
Example 189
Source File: CustomerApp.scala From reactive-application-development-scala with Apache License 2.0 | 5 votes |
package com.rarebooks.library import akka.actor.{ActorSelection, ActorSystem, Address, RootActorPath} import akka.event.Logging import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.{Duration, FiniteDuration, MILLISECONDS => Millis} import scala.io.StdIn object CustomerApp { protected def createCustomer(count: Int, odds: Int, tolerance: Int): Unit = { val selection: ActorSelection = system.actorSelection( RootActorPath(rareBooksAddress) / "user" / "rare-books") selection.resolveOne(resolveTimeout).onComplete { case scala.util.Success(rareBooks) => for (_ <- 1 to count) system.actorOf(Customer.props(rareBooks, odds, tolerance)) case scala.util.Failure(ex) => log.error(ex, ex.getMessage) } } }
Example 190
Source File: RareBooksApp.scala From reactive-application-development-scala with Apache License 2.0 | 5 votes |
package com.rarebooks.library import akka.actor.{ ActorRef, ActorSystem } import akka.event.Logging import scala.annotation.tailrec import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.io.StdIn object RareBooksApp { protected def createRareBooks(): ActorRef = { system.actorOf(RareBooks.props, "rare-books") } def run(): Unit = { log.warning(f"{} running%nWaiting for customer requests.", getClass.getSimpleName) commandLoop() Await.ready(system.whenTerminated, Duration.Inf) } @tailrec private def commandLoop(): Unit = Command(StdIn.readLine()) match { case Command.Customer(count, odds, tolerance) => log.warning(s"Enter customer commands from the customer app prompt.") commandLoop() case Command.Quit => system.terminate() case Command.Unknown(command) => log.warning(s"Unknown command $command") commandLoop() } }
Example 191
Source File: PsUtil.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master.execution.workers.starter import scala.annotation.tailrec trait PsUtil { def parseArguments(s: String): Seq[String] = { def safeTail(s: String): String = if (s.isEmpty) "" else s.tail @tailrec def parse(in: String, curr: Vector[String]): Seq[String] = in.headOption match { case None => curr case Some(' ') => parse(in.dropWhile(_ == ' '), curr) case Some(sym) if sym == '"' || sym == '\'' => val (elem, rest) = in.tail.span(_ != sym) parse(safeTail(rest), curr :+ elem) case Some(_) => val (elem, rest) = in.span(_ != ' ') parse(safeTail(rest), curr :+ elem) } parse(s, Vector.empty) } } object PsUtil extends PsUtil
Example 192
Source File: Service.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.gen.javadsl import com.google.protobuf.Descriptors.{ FileDescriptor, ServiceDescriptor } import scalapb.compiler.{ DescriptorImplicits, GeneratorParams } import scala.annotation.tailrec import scala.collection.JavaConverters._ import scala.collection.immutable final case class Service( descriptor: String, packageName: String, name: String, grpcName: String, methods: immutable.Seq[Method], serverPowerApi: Boolean, usePlayActions: Boolean, comment: Option[String] = None) { def serializers: Seq[Serializer] = (methods.map(_.deserializer) ++ methods.map(_.serializer)).distinct def packageDir = packageName.replace('.', '/') } object Service { def apply( fileDesc: FileDescriptor, serviceDescriptor: ServiceDescriptor, serverPowerApi: Boolean, usePlayActions: Boolean): Service = { val comment = { // Use ScalaPB's implicit classes to avoid replicating the logic for comment extraction // Note that this be problematic if/when ScalaPB uses scala-specific stuff to do that implicit val ops = new DescriptorImplicits(GeneratorParams(), fileDesc.getDependencies.asScala.toList :+ fileDesc.getFile) import ops._ serviceDescriptor.comment } val packageName = if (fileDesc.getOptions.hasJavaPackage) fileDesc.getOptions.getJavaPackage else fileDesc.getPackage Service( outerClass(fileDesc) + ".getDescriptor()", packageName, serviceDescriptor.getName, (if (fileDesc.getPackage.isEmpty) "" else fileDesc.getPackage + ".") + serviceDescriptor.getName, serviceDescriptor.getMethods.asScala.toList.map(method => Method(method)), serverPowerApi, usePlayActions, comment) } private[javadsl] def basename(name: String): String = name.replaceAll("^.*/", "").replaceAll("\\.[^\\.]*$", "") private[javadsl] def outerClass(t: FileDescriptor) = if (t.toProto.getOptions.hasJavaOuterClassname) t.toProto.getOptions.getJavaOuterClassname else { val className = Service.toCamelCase(protoName(t)) if (hasConflictingClassName(t, className)) className + "OuterClass" else className } private def hasConflictingClassName(d: FileDescriptor, className: String): Boolean = d.findServiceByName(className) != null || d.findMessageTypeByName(className) != null || d.findEnumTypeByName(className) != null private[javadsl] def protoName(t: FileDescriptor) = t.getName.replaceAll("\\.proto", "").split("/").last private[javadsl] def toCamelCase(name: String): String = if (name.isEmpty) "" else toCamelCaseRec(name, 0, new StringBuilder(name.length), true) @tailrec private def toCamelCaseRec(in: String, idx: Int, out: StringBuilder, capNext: Boolean): String = { if (idx >= in.length) out.toString else { val head = in.charAt(idx) if (head.isLetter) toCamelCaseRec(in, idx + 1, out.append(if (capNext) head.toUpper else head), false) else toCamelCaseRec(in, idx + 1, if (head.isDigit) out.append(head) else out, true) } } }
Example 193
Source File: TwoSum.scala From coding-interview-questions-scala with Apache License 2.0 | 5 votes |
package org.questions.arrays import scala.annotation.tailrec import scala.collection.immutable.HashSet class TwoSum { def findPairSum(seq: Seq[Int], sum: Int): Option[(Int, Int)] = { @tailrec def inner(seq: Seq[Int], hash: HashSet[Int]): Option[(Int, Int)] = seq match { case Nil => None case head :: tail => val lookingFor = sum - head if (hash.contains(lookingFor)) Some(head -> lookingFor) else inner(tail, hash + head) } inner(seq, HashSet.empty[Int]) } }
Example 194
Source File: ArrayMaxSumNonConsecutive.scala From coding-interview-questions-scala with Apache License 2.0 | 5 votes |
package org.questions.arrays import scala.annotation.tailrec trait ArrayMaxSumNonConsecutive { def maxSum(numbers: Seq[Int]): Int } class Recursive extends ArrayMaxSumNonConsecutive { def maxSum(numbers: Seq[Int]): Int = numbers match { case Nil => 0 case Seq(n) => n case Seq(a, b) => Math.max(a, b) case head :: tail => val currentAndSkipOne = head + maxSum(tail.tail) val skipCurrent = maxSum(tail) Math.max(currentAndSkipOne, skipCurrent) } } class DynamicProgramming extends ArrayMaxSumNonConsecutive { override def maxSum(numbers: Seq[Int]): Int = { if (numbers.isEmpty) return 0 var inclusive = numbers.head var exclusive = 0 for (i <- 1 until numbers.length) { val newInclusive = exclusive + numbers(i) val newExclusive = inclusive inclusive = newInclusive exclusive = newExclusive } math.max(exclusive, inclusive) } } class DynamicProgrammingImmutable extends ArrayMaxSumNonConsecutive { override def maxSum(numbers: Seq[Int]): Int = { @tailrec def inner(numbers: Seq[Int], inclusive: Int, exclusive: Int): Int = numbers match { case Nil => math.max(inclusive, exclusive) case head :: tail => inner(numbers.tail, numbers.head + exclusive, inclusive) } if (numbers.nonEmpty) inner(numbers.tail, numbers.head, 0) else 0 } }
Example 195
Source File: LongestIncreasing.scala From coding-interview-questions-scala with Apache License 2.0 | 5 votes |
package org.questions.arrays import scala.annotation.tailrec trait LongestIncreasing { def findLongestIncreasing(seq: Seq[Int]): Seq[Int] } class LongestIncreasingRecursive extends LongestIncreasing { override def findLongestIncreasing(seq: Seq[Int]): Seq[Int] = { require(seq.nonEmpty) findLongestIncreasingInternal(seq) } private def findLongestIncreasingInternal(seq: Seq[Int]): Seq[Int] = seq match { case Nil => Nil case _ => val firstIncreasing = increasingSeq(seq) val otherIncreasing = findLongestIncreasingInternal(seq.drop(firstIncreasing.length)) Seq(firstIncreasing, otherIncreasing).maxBy(_.length) } @tailrec private def increasingSeq(seq: Seq[Int], res: Seq[Int] = Nil): Seq[Int] = seq match { case head :: (tail@Seq(next, _*)) => if (head < next) increasingSeq(tail, res :+ head) else res :+ head case Seq(single) => res :+ single case _ => res } } class LongestIncreasingIterative extends LongestIncreasing { def findLongestIncreasing(seq: Seq[Int]): Seq[Int] = { require(seq.nonEmpty) var localLongest = Seq[Int](seq.head) var globalLongest = Seq[Int]() def setGlobalFromLonger(): Unit = { globalLongest = Seq(localLongest, globalLongest).maxBy(_.length) } for (i <- seq.indices.drop(1)) { val prev = seq(i - 1) val current = seq(i) if (prev < current) { localLongest = localLongest :+ current } else { setGlobalFromLonger() localLongest = Seq(current) } } setGlobalFromLonger() globalLongest } }
Example 196
Source File: TailRecursion.scala From coding-interview-questions-scala with Apache License 2.0 | 5 votes |
package org.questions.fibonacci import scala.annotation.tailrec class TailRecursion extends Fibonacci { override def nth(n: Int): Long = { @tailrec def loop(n: Int, current: Int, temp: Int): Int = n match { case 0 => current case _ => loop(n - 1, temp, current + temp) } require(n >= 0) loop(n, 0, 1) } }
Example 197
Source File: BaseFeature.scala From cornichon with Apache License 2.0 | 5 votes |
package com.github.agourlay.cornichon.dsl import java.util.concurrent.ConcurrentLinkedDeque import com.github.agourlay.cornichon.core.{ Config, Done, FeatureDef, Step } import com.github.agourlay.cornichon.matchers.Matcher import com.github.agourlay.cornichon.resolver.Mapper import monix.execution.Scheduler import pureconfig.error.{ ConvertFailure, KeyNotFound } import scala.annotation.tailrec import scala.collection.mutable.ListBuffer import scala.concurrent.Future trait BaseFeature { protected[cornichon] val beforeFeature: ListBuffer[() => Unit] = ListBuffer.empty protected[cornichon] val afterFeature: ListBuffer[() => Unit] = ListBuffer.empty protected[cornichon] val beforeEachScenario: ListBuffer[Step] = ListBuffer.empty protected[cornichon] val afterEachScenario: ListBuffer[Step] = ListBuffer.empty private[cornichon] lazy val config = BaseFeature.config lazy val executeScenariosInParallel: Boolean = config.executeScenariosInParallel lazy val seed: Option[Long] = None // Convenient implicits for the custom DSL implicit lazy val ec = Scheduler.Implicits.global def feature: FeatureDef def registerExtractors: Map[String, Mapper] = Map.empty def registerMatchers: List[Matcher] = Nil def beforeFeature(before: => Unit): Unit = beforeFeature += (() => before) def afterFeature(after: => Unit): Unit = (() => after) +=: afterFeature def beforeEachScenario(step: Step): Unit = beforeEachScenario += step def afterEachScenario(step: Step): Unit = step +=: afterEachScenario } // Protect and free resources object BaseFeature { import pureconfig.generic.auto._ import pureconfig.ConfigSource import pureconfig.error.{ ConfigReaderException, ConfigReaderFailures } lazy val config = ConfigSource.default.at("cornichon").load[Config] match { case Right(v) => v case Left(ConfigReaderFailures(ConvertFailure(KeyNotFound("cornichon", _), _, _), _*)) => Config() case Left(failures) => throw new ConfigReaderException[Config](failures) } private val hooks = new ConcurrentLinkedDeque[() => Future[_]]() def addShutdownHook(h: () => Future[_]): Unit = hooks.push(h) def shutDownGlobalResources(): Future[Done] = { import scala.concurrent.ExecutionContext.Implicits.global @tailrec def clearHooks(previous: Future[Any] = Future.successful[Any](())): Future[Any] = Option(hooks.poll()) match { case None => previous case Some(f) => clearHooks { previous.flatMap { _ => f().recover { case _ => Done } } } } clearHooks().map(_ => Done) } }
Example 198
Source File: FileWithLastrun.scala From sbt-node with MIT License | 5 votes |
// Project: sbt-node // Module: // Description: package de.surfice.sbtnpm.utils import sbinary.{Input, Output} import sbt._ import scala.annotation.tailrec case class FileWithLastrun(file: File, lastrun: Long) { def needsUpdate: Boolean = !file.exists() || file.lastModified > lastrun def needsUpdate(reference: sbt.File): Boolean = needsUpdate || reference.lastModified() > lastrun def needsUpdateComparedToConfig(baseDir: File): Boolean = FileWithLastrun.configNewerThanTimestamp( FileWithLastrun.findProjectRoot(baseDir), lastrun) || needsUpdate } object FileWithLastrun { def apply(file: sbt.File): FileWithLastrun = apply(file,new java.util.Date().getTime) private var _configClassesDir: File = _ private def configNewerThanTimestamp(projectRoot: File, lastrun: Long): Boolean = { if(_configClassesDir == null) this.synchronized { _configClassesDir = projectRoot / "project" / "target" / "config-classes" } _configClassesDir.lastModified > lastrun } // TODO: handle detection of project root directories without build.sbt @tailrec private def findProjectRoot(baseDir: File): File = if(baseDir.isDirectory && (baseDir / "build.sbt").canRead) { baseDir } else if(baseDir.getParentFile == baseDir) throw new RuntimeException("Could not locate project root") else findProjectRoot(baseDir.getParentFile) implicit object format extends sbinary.Format[FileWithLastrun] { import sbt.Cache._ override def reads(in: Input): FileWithLastrun = FileWithLastrun( new sbt.File(StringFormat.reads(in)), LongFormat.reads(in) ) override def writes(out: Output, value: FileWithLastrun): Unit = { StringFormat.writes(out,value.file.getCanonicalPath) LongFormat.writes(out,value.lastrun) } } }
Example 199
Source File: ProcessorFibonacci.scala From akka-cluster-playground with MIT License | 5 votes |
package com.elleflorio.cluster.playground.node.processor import akka.actor.{Actor, ActorRef, Props} import scala.annotation.tailrec object ProcessorFibonacci { sealed trait ProcessorFibonacciMessage case class Compute(n: Int, replyTo: ActorRef) extends ProcessorFibonacciMessage def props(nodeId: String) = Props(new ProcessorFibonacci(nodeId)) def fibonacci(x: Int): BigInt = { @tailrec def fibHelper(x: Int, prev: BigInt = 0, next: BigInt = 1): BigInt = x match { case 0 => prev case 1 => next case _ => fibHelper(x - 1, next, next + prev) } fibHelper(x) } } class ProcessorFibonacci(nodeId: String) extends Actor { import ProcessorFibonacci._ override def receive: Receive = { case Compute(value, replyTo) => { replyTo ! ProcessorResponse(nodeId, fibonacci(value)) } } }
Example 200
Source File: IList.scala From CSYE7200 with MIT License | 5 votes |
package edu.neu.coe.csye7200 import scala.annotation.tailrec trait IList { def x0: Boolean = this match {case INil => true; case _ => false } def x1: Int = this match { case INil => 0 case Cons(_, tl) => 1 + tl.x1 } def x2: Int = this match { case INil => 0 case Cons(hd, tl) => hd + tl.x2 } def x2a: Int = this match { case INil => throw new Exception("logic error"); case Cons(hd, _) => hd } def x3: IList = this match { case INil => INil; case Cons(_, tl) => tl } def x3a: Option[Int] = this match { case INil => None; case Cons(hd, _) => Some(hd) } def x4(x: Int): Option[Int] = { @tailrec def inner(as: IList, x: Int): Option[Int] = as match { case INil => None case Cons(hd, tl) => if (x == 0) Some(hd) else inner(tl, x - 1) } if (x < 0) None else inner(this, x) } } case object INil extends IList case class Cons(head: Int, tail: IList) extends IList