package com.michalrus.nofatty.data

import java.util.UUID

import scala.slick.driver.SQLiteDriver.simple._
import org.joda.time.DateTime

object DBDummy {

  DB.db withSession { implicit session ⇒
    val butterID, breadID, sandwichID = UUID.randomUUID
    val r1 = DB.basicProducts += BasicProduct(butterID, DateTime.now, "Cow butter", 748, 0, 83, 0, 0)
    val r2 = DB.basicProducts += BasicProduct(breadID, DateTime.now, "White bread", 257, 8.5, 1.4, 54.3, 2.7)
    val r3 = DB.compoundProducts += ((sandwichID, DateTime.now, "Sandwich", 1.0))
    val r4 = DB.ingredients += ((sandwichID, breadID, 25))
    val r5 = DB.ingredients += ((sandwichID, butterID, 5))
    val _ = (r1, r2, r3, r4, r5)
  }

}

Create a dummy DB filler

Create a dummy DB filler


package org.jetbrains.plugins.scala.lang.typeInference

import org.jetbrains.plugins.scala.LatestScalaVersions.{Scala_2_12, Scala_2_13}
import org.jetbrains.plugins.scala.ScalaVersion
import org.jetbrains.plugins.scala.base.ScalaLightCodeInsightFixtureTestAdapter

class EmptyParamEtaExpansion extends ScalaLightCodeInsightFixtureTestAdapter {
  override implicit def version: ScalaVersion = Scala_2_13

  def testSCL18172(): Unit = checkTextHasNoErrors(
    """
      |import java.util.concurrent.{ExecutorService, Future}
      |type Par[A] = ExecutorService => Future[A]
      |def join[A](a: Par[Par[A]]): Par[A] = es => {
      |  val value: Par[A] = a(es).get
      |  ???
      |}
      |""".stripMargin
  )
}

class EmpptyParamEtaExpansion_2_12 extends ScalaLightCodeInsightFixtureTestAdapter {
  override implicit def version: ScalaVersion = Scala_2_12

  def testSCL18172(): Unit = checkHasErrorAroundCaret(
    s"""
      |import java.util.concurrent.{ExecutorService, Future}
      |type Par[A] = ExecutorService => Future[A]
      |def join[A](a: Par[Par[A]]): Par[A] = es => {
      |  val value: Par[A] = ${CARET}a(es).get
      |  ???
      |}""".stripMargin
  )
}

Fix eta-expansion rules for empty-param methods

#SCL-18172 fixed


object Solution {

  def ways(limit: Int): Array[Int] = {
    val p = 1e9.toInt + 7
    val result = Array.fill(limit + 1)(0)
    result(0) = 1
    for {
      i <- 1 to limit
      j <- i to limit
    } result(j) = (result(j) + result(j - i)) % p
    // result will be all the summations include the single 1 integer.
    result
  }

  def main(args: Array[String]) {
    val t = readInt
    val tcs = (1 to t).map(x => readInt)
    val result = ways(tcs.max)
    for (t <- tcs) {
      println(result(t) - 1)
    }
  }
}

Add solution to problem 76.

Add solution to problem 76.


/*
 * Copyright (c) 2013-2014 Telefónica Investigación y Desarrollo S.A.U.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package es.tid.cosmos.infinity.server.util

import scala.concurrent.Future

import com.twitter.util.Await
import org.scalatest.FlatSpec
import org.scalatest.matchers.MustMatchers

class TwitterConversionsTest extends FlatSpec with MustMatchers {

  import TwitterConversions._

  "Twitter conversions" must "convert a success Scala future into a Twitter future" in {
    Await.result(Future.successful(1234).toTwitter) must be (1234)
  }

  it must "convert a failure Scala future into a Twitter future" in {
    evaluating {
      Await.result(Future.failed(new Exception("fail")).toTwitter)
    } must produce[Exception]
  }
}

Add a unit test for Twitter conversions

Add a unit test for Twitter conversions


import stainless.lang._
import stainless.annotation._

object HeapGetProjectsReadsExample {
  case class Cell(var value: BigInt) extends AnyHeapRef

  def onlyOne(c1: Cell, c2: Cell): BigInt = {
    reads(Set(c1))
    Heap.get.eval { c2.value }
  }

  def testOne(c1: Cell, c2: Cell): Unit = {
    require(c1 != c2)
    reads(Set(c1, c2))
    modifies(Set(c2))
    c2.value = 1
    val h1 = Heap.get
    val v1 = onlyOne(c1, c2)
    assert(v1 == 1)
    c2.value = 2
    val h2 = Heap.get
    assert(Heap.unchanged(Set(c1), h1, h2))
    val v2 = onlyOne(c1, c2)
    assert(v2 == 2)
    assert(v1 == v2)
    assert(false, "Contradiction")
  }
}

Add test case witnessing previous HeapGet unsoundness


package utils

import play.api.mvc._

/**
 * A predicated action is one where a condition must be satisfied in order to proceed with the request. If the
 * condition is not satisfied then a supplied status result is yielded.
 */
class PredicatedActionBuilder {
  def apply[A](p: Boolean, failed: Result)(action: Action[A]): Action[A] = new Action[A] {
    def apply(request: Request[A]): Result = {
      if (p) action(request) else failed
    }

    lazy val parser = action.parser
  }
}

object PredicatedAction extends PredicatedActionBuilder

package utils

import play.api.mvc._

/**
 * A predicated action is one where a condition must be satisfied in order to proceed with the request. If the
 * condition is not satisfied then a supplied status result is yielded.
 */
class PredicatedActionBuilder {
  def apply[A](p: => Boolean, failed: Result)(action: Action[A]): Action[A] = new Action[A] {
    def apply(request: Request[A]): Result = {
      if (p) action(request) else failed
    }

    lazy val parser = action.parser
  }
}

object PredicatedAction extends PredicatedActionBuilder

Revert "Doesn't require a function declaration for the predicate"

This reverts commit a4dabeee7c7d08fe20b5f895b7adc2312ffb1367.


import stainless.lang._

object FieldInheritance2 {

  sealed abstract class Foo[B] {
    val thisIsIt: BigInt = 1
    val y: BigInt
    val z: BigInt = y
  }

  case class Bar[X](override val thisIsIt: BigInt) extends Foo[X] {
    val y = thisIsIt
  }

  def foo[A](f: Foo[A]) = {  f.y == f.z }.holds // wrong because f.z is always null
}

Add bad field inheritance example

Add bad field inheritance example


package com.ataraxer.zooowner

import org.apache.zookeeper.{Watcher => ZKWatcher, WatchedEvent}
import org.apache.zookeeper.Watcher.{Event => ZKEvent}
import org.apache.zookeeper.Watcher.Event.{KeeperState, EventType}

package event {
  private[zooowner] sealed abstract class Event

  case class NodeCreated(path: String, data: String) extends Event
  case class NodeChanged(path: String, data: String) extends Event
  case class NodeDeleted(path: String, data: String) extends Event
  case class NodeChildrenChanged(path: String, children: Seq[String])
      extends Event
}

// vim: set ts=2 sw=2 et:

Add events as case classes

Add events as case classes


package gitbucket.monitoring.models

import gitbucket.monitoring.utils._

object LogBack {

  val enableLogging = Java.all.contains("logback.configurationFile")
  val confPath = Java.all.getOrElse("logback.configurationFile", "Can not find logback-settings.xml")

  def xmlContent: Either[String, String] = {
    if (enableLogging) {
      try {
        val s = scala.io.Source.fromFile(confPath)
        try {
          (Right(
            s.mkString
          ))
        } catch {
          case e: Exception => Left("ERROR")
        } finally {
          s.close
        }
      } catch {
        case e: Exception => Left("ERROR")
      }
    } else {
      Left("Dosen't configure Logback")
    }
  }
}

class Logs () {
  val logBack = LogBack
}

Create get LogBack Information class

Create get LogBack Information class


package ch.usi.inf.l3.sana.modulej.namers

import ch.usi.inf.l3.sana
import sana.tiny
import sana.calcj
import sana.primj
import sana.brokenj
import sana.ooj
import sana.modulej

import tiny.dsl._
import primj.namers.SymbolAssignerComponent
import modulej.ast._
import ooj.ast.PackageDefApi
import modulej.symbols.CompilationUnitSymbol
import tiny.ast.Implicits._

@component
trait CompilationUnitSymbolAssignerComponent extends SymbolAssignerComponent {
  (cunit: CompilationUnitApi) => {
    val owner   = cunit.owner
    val sym     = CompilationUnitSymbol(Nil, None, cunit.sourceName,
                                  cunit.sourcePath, owner)
    owner.foreach(owner => {
      owner.declare(sym)
    })
    cunit.module.owner = sym
    cunit.imports.foreach(_.owner = sym)

    val pkg            = assign(cunit.module).asInstanceOf[PackageDefApi]
    val imports        = cunit.imports.map(assign(_).asInstanceOf[ImportApi])
    val importSymbols  = imports.flatMap(_.symbol)
    sym.module         = pkg.symbol
    sym.imports        = importSymbols
    TreeCopiers.copyCompilationUnit(cunit)(imports = imports, module = pkg)
  }
}

Update symbolassigner component for compilation unit


/*
 * Copyright (c) <2015-2016>, see CONTRIBUTERS
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <organization> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package ch.usi.inf.l3.sana.dcct.errors

import ch.usi.inf.l3.sana
import sana.tiny.errors.ErrorCode
import sana.ooj

trait ErrorCodes extends ooj.errors.ErrorCodes {
  case object CLOUDY_RETURN_TYPE extends ErrorCode {
    val message: String =
      "Method cannot return a cloud type"
  }

  case object CLOUDY_VARIABLE_TYPE extends ErrorCode {
    val message: String =
      "Variable cannot be a cloud type"
  }
}

object ErrorCodes extends ErrorCodes

Add error codes to dcct

Add error codes to dcct


object Solution {

  def numbOfOddSquareRootPeriod(x: Int): Int = {
    var count = 0
    for (n <- 2 to x) {
      val a0 = math.sqrt(n).toInt
      if (a0 * a0 == n) {}// perfect square, skip
      else {
        var period = 0
        var (d, m, a) = (1, 0, a0)
        while(a != 2 * a0) {
          m = d * a - m
          d = (n - m * m) / d
          a = (a0 + m) / d
          period += 1
        }
        period += 1
        if (period % 2 == 0) count += 1
      }
    }
    count
  }

  def main(args: Array[String]) {
    val n = readLine.toInt
    println(numbOfOddSquareRootPeriod(n))
  }
}

Add solution to problem 64. Should check mathblog and related Wikipedia article.


// See LICENSE for license details.

package util

import scala.collection.immutable.ListMap
import chisel3._

class HVec[T<:Data](wires: Seq[T]) extends Record with collection.IndexedSeq[T] {
  def apply(x: Int) = wires(x)
  val elements = ListMap(wires.zipWithIndex.map { case (n,i) => (i.toString, n) }:_*)
  def length = wires.length

  override def cloneType: this.type = new HVec(wires.map(_.cloneType)).asInstanceOf[this.type]
}

object HVec {
  def apply[T<:Data](wires: Seq[T]) = new HVec(wires)
  def tabulate[T<:Data](size: Int)(gen: Int => T) = HVec(Seq.tabulate(size) { i => gen(i)})
}

Add a util 'Heterogeneous Vec' class used to make parameterized Vecs of structs


import stainless.lang._
import stainless.collection._
import stainless.annotation._

object Iterables {

  def test_setToList(set: Set[BigInt]) = {
    require(set.contains(1) && set.contains(2) && !set.contains(3))

    val res = set.toList

    assert(res.contains(1))
    assert(res.contains(2))
    assert(!res.contains(3))
  }

  def test_setMap(set: Set[BigInt]) = {
    require(set.contains(1) && set.contains(2) && !set.contains(3))

    val res = set.map(_ + 1)

    assert(res.contains(2))
    assert(res.contains(3))
    assert(!res.contains(4))
  }

  def test_mapKeys(map: Map[Int, String]) = {
    require(map.contains(1) && map.contains(2) && !map.contains(3))

    val res = map.keys

    assert(res.contains(1))
    assert(res.contains(2))
    assert(!res.contains(3))
  }

  def test_mapValues(map: Map[Int, String]) = {
    require(map.get(1) == Some("foo") && map.get(2) == Some("bar"))

    val res = map.values

    assert(res.contains("foo"))
    assert(res.contains("bar"))
  }

  def test_mapToList(map: Map[Int, String]) = {
    require(map.get(1) == Some("foo") && map.get(2) == Some("bar"))

    val res = map.toList

    assert(res.contains((1, "foo")))
    assert(res.contains((2, "bar")))
  }
}

Add benchmark for new methods on Map and Set


package com.salutr
import scala.io.Source
import java.io.PrintWriter
import java.io.File
import com.salutr.declinator.CzechVocativeDeclinator

object VocativeDeclinatorApp {
  def main(args: Array[String]) {
    if (args.length < 2) {
      println("arguments: INPUT_FILE OUTPUT_FILE")
      sys.exit()
    }
    val inputFileName = args(0)
    val input = Source.fromFile(new File(inputFileName), "UTF-8")
    val outputFileName = args(1)
    val writer = new PrintWriter(new File(outputFileName), "UTF-8")

    val declinator = new CzechVocativeDeclinator()
    for {
      line <- input.getLines()
      val columns = line.split(",")
      if columns.length > 0
      val nominative = toTitleCase(columns(0))
    } {
      try {
        val result = declinator.declineWord(nominative, null)
        val vocative = result.get("vocative")
        if (vocative.isDefined) {
          writer.println(nominative + "," + toTitleCase(vocative.get))
        } else {
          System.err.println(nominative + ": " + result.get("message"))
        }
      } catch {
        case ex: Exception => System.err.println(nominative + ": " + ex)
      }
    }
    writer.flush
  }

  def toTitleCase(text: String) = {
    text.take(1).toUpperCase + text.drop(1).toLowerCase
  }
}

Make a simple app to decline name nominatives to vocative in a batch.


package thunder.examples

import org.apache.spark.{SparkContext, SparkConf}
import thunder.util.Load

object ExampleLoad {

  def main(args: Array[String]) {

    val master = args(0)

    val file = args(1)

    val conf = new SparkConf().setMaster(master).setAppName("ExampleLoad")

    if (!master.contains("local")) {
      conf.setSparkHome(System.getenv("SPARK_HOME"))
        .setJars(List("target/scala-2.10/thunder_2.10-0.1.0.jar"))
        .set("spark.executor.memory", "100G")
    }

    val sc = new SparkContext(conf)

    val data = Load.fromBinaryWithKeys(sc, file, nKeys = 1)

    data.take(10).foreach{x =>
      println(x._1.mkString(" "))
      println(x._2.mkString(" "))
    }

    println(data.count())

  }
}

package thunder.examples

import org.apache.spark.{SparkContext, SparkConf}
import thunder.util.Load

object ExampleLoad {

  def main(args: Array[String]) {

    val master = args(0)

    val file = args(1)

    val conf = new SparkConf().setMaster(master).setAppName("ExampleLoad")

    if (!master.contains("local")) {
      conf.setSparkHome(System.getenv("SPARK_HOME"))
        .setJars(List("target/scala-2.10/thunder_2.10-0.1.0.jar"))
        .set("spark.executor.memory", "100G")
    }

    val sc = new SparkContext(conf)

    val data = Load.fromBinaryWithKeys(sc, file, nKeys = 3, format="short")

    data.take(10).foreach{x =>
      println(x._1.mkString(" "))
      println(x._2.mkString(" "))
    }

    println(data.count())

  }
}

Use binary for example loading

Use binary for example loading


object Solution {

  def squareRoot(i: Int, nDigits: Int): BigInt = {
    val limit = BigInt(10).pow(nDigits + 1)
    var a = 5 * BigInt(i)
    var b = BigInt(5)
    while (b < limit) {
      if (a >= b) {
        a -= b
        b += 10
      }
      else {
        a *= 100
        b = (b / 10) * 100 + 5
      }
    }
    b / 100
  }

  def isPerfectRoot(x: Int): Boolean = {
    val sqrt = math.sqrt(x).toInt
    sqrt * sqrt == x
  }

  def main(args: Array[String]) {
    val n = readInt
    val p = readInt
    val sum = (1 to n).filter(!isPerfectRoot(_))
                      .map(x => squareRoot(x, p).toString.map(_.asDigit).sum).sum
    println(sum)
  }
}

Add solution to problem 80.

Add solution to problem 80.


object MutableField {
  case class Container(var v: BigInt) {
    def load(x: BigInt) = {
      v = x
    }
  }

  abstract class Box {
    val c = Container(0)
    def load(x: BigInt) = c.load(x)
  }

  case class Nat() extends Box {
    require(c.v >= 0)         // should not be allowed (?)

    def extract(): BigInt = {
      c.v
    } ensuring(_ >= 0)
  }

  def theorem() = {
    val n = Nat()
    n.load(-1)
    assert(n.extract() == 0)  // should be -1
  }
}

Add benchmark for rejecting mutable class fields


package com.yuzhouwan.hacker.enums

/**
  * Copyright @ 2018 yuzhouwan.com
  * All right reserved.
  * Function：Enums
  *
  * @author Benedict Jin
  * @since 2018/4/13
  */
object Enums extends Enumeration {
  type enums = Value
  val E, N, U, M, OTHER = Value

  // normal
  // equals 方法检查的是：值是否相等
  def convert2Enums(enums: String): Option[enums] = {
    Enums.values.find(x => x.toString.equals(enums))
  }

  // bad
  def convert2Enums2(enums: String): Option[enums] = {
    Enums.values.find(_.toString.equals(enums))
  }

  // better
  def convert2Enums3(enums: String): Option[enums] = {
    Enums.values.find(_.toString eq enums)
  }

  // same as `eq`
  // eq 和 == 检查的是：引用是否相同
  def convert2Enums4(enums: String): Option[enums] = {
    Enums.values.find(_.toString == enums)
  }

  def main(args: Array[String]): Unit = {
    assert(convert2Enums("E").getOrElse(Enums.OTHER).equals(Enums.E))
    assert(convert2Enums2("N").getOrElse(Enums.OTHER).equals(Enums.N))
    assert(convert2Enums3("U").getOrElse(Enums.OTHER).equals(Enums.U))
    assert(convert2Enums4("M").getOrElse(Enums.OTHER).equals(Enums.M))
  }
}

Add test case for scala Enumeration

Add test case for scala Enumeration


package featurebee.registry.s3

import java.util.concurrent.atomic.AtomicReference

import akka.actor.Scheduler
import featurebee.api.Feature.FeatureName
import featurebee.api.{Feature, FeatureRegistry}
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.language.postfixOps

/**
  * Feature registry that periodically reloads the feature registry via the given reCreator function. If the recreator function returns None, the
  * feature registry stays the same. With that you can implement keeping the old registry in case of failures or if you are able to detect that nothing
  * in the feature set has changed.
  */
class ReloadingFeatureRegistry(initial: FeatureRegistry, reCreator: () => Option[FeatureRegistry], scheduler: Scheduler,
                               reloadAfter: FiniteDuration = 3 minute, executor: ExecutionContext) extends FeatureRegistry {

  private val ref = new AtomicReference[FeatureRegistry](initial)
  scheduler.schedule(reloadAfter, reloadAfter) {
    reCreator().foreach(ref.set)
  }(executor)

  override def feature(name: String): Option[Feature] = ref.get().feature(name)

  override def allFeaturesx: Set[(FeatureName, Feature)] = ref.get().allFeatures
}

package featurebee.registry.s3

import java.util.concurrent.atomic.AtomicReference

import akka.actor.Scheduler
import featurebee.api.Feature.FeatureName
import featurebee.api.{Feature, FeatureRegistry}
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.language.postfixOps

/**
  * Feature registry that periodically reloads the feature registry via the given reCreator function. If the recreator function returns None, the
  * feature registry stays the same. With that you can implement keeping the old registry in case of failures or if you are able to detect that nothing
  * in the feature set has changed.
  */
class ReloadingFeatureRegistry(initial: FeatureRegistry, reCreator: () => Option[FeatureRegistry], scheduler: Scheduler,
                               reloadAfter: FiniteDuration = 3 minute, executor: ExecutionContext) extends FeatureRegistry {

  private val ref = new AtomicReference[FeatureRegistry](initial)
  scheduler.schedule(reloadAfter, reloadAfter) {
    reCreator().foreach(ref.set)
  }(executor)

  override def feature(name: String): Option[Feature] = ref.get().feature(name)

  override def allFeatures: Set[(FeatureName, Feature)] = ref.get().allFeatures
}

Revert "testing build break dashboard display"

This reverts commit 8755efb95b5e73c26817efd7e76c05cd8ece9ed8.


package turksem.util

import cats.implicits._

// has nothing to do with the project really

import java.nio.charset._
import java.nio.file._
import java.io.File

case class SensePairInstance(
  propBankFramesetId: String,
  verbNetClass: String)

object PropBankLinker {

  // TODO use streams
  def argInstancesIterator = {
    import scala.collection.JavaConverters._
    for {
      section <- new File("resources/semlink-1.2.2c/wsjTokens").listFiles.iterator
      file <- section.listFiles.iterator
      line <- Files.readAllLines(Paths.get(file.getPath), Charset.forName("UTF-8")).asScala.iterator
      arr = line.split(" ")
    } yield SensePairInstance(
      propBankFramesetId = arr(7),
      verbNetClass = arr(5)
    )
  }

  lazy val sensePairMappings = {
    val fileString = scala.io.Source.fromFile("resources/semlink-1.2.2c/vn-pb/vnpbMappings.txt").getLines.mkString("\n")
    val xmlFile = scala.xml.XML.loadString(fileString)

    (xmlFile \ "predicate" \ "argmap").toList.map(node =>
      SensePairInstance(
        propBankFramesetId = (node \ "@pb-roleset").text,
        verbNetClass = (node \ "@vn-class").text
      ) -> (node \ "role").toList.map(roleNode =>
        ("ARG" + (roleNode \ "@pb-arg").text) -> (roleNode \ "@vn-theta").text
      ).toMap
    ).toMap
  }

  lazy val instancesByPropBankSense = argInstancesIterator.toList
    .groupBy(_.propBankFramesetId)
    .map { case (pbId, instances) =>
      pbId -> counts(instances.map(_.verbNetClass))
  }

  lazy val roleMappingsByPropBankSense = instancesByPropBankSense
    .map { case (pbFramesetId, instanceCounts) =>
      pbFramesetId -> instanceCounts.toVector.sortBy(-_._2).map(_._1)
        .foldLeft(Map.empty[String, String]) { case (acc, vnClass) =>
          sensePairMappings.get(SensePairInstance(pbFramesetId, vnClass)).fold(acc)(roleMapping =>
            roleMapping.foldLeft(acc) { case (acc2, (pbRole, vnRole)) =>
              if(acc2.contains(pbRole)) acc2 else acc2.updated(pbRole, vnRole)
            }
          )
      }
  }

  def writeRoleMappingsToFile(filename: String) = {
    val str = roleMappingsByPropBankSense.map { case (pbFramesetId, roleMapping) =>
      roleMapping.map { case (pbRole, vnRole) =>
        s"$pbFramesetId\t$pbRole\t$vnRole"
      }.mkString("\n")
    }.mkString("\n")
    Files.write(Paths.get(filename), str.getBytes(StandardCharsets.UTF_8))
  }
}

Add PropBank linker to VerbNet roles (using SemLink)


package uk.ac.wellcome.models

import org.scalatest.{FunSpec, Matchers}
import org.scalatest.prop.TableDrivenPropertyChecks._
import uk.ac.wellcome.test.utils.JsonTestUtil
import uk.ac.wellcome.utils.JsonUtil._

class ConceptTest extends FunSpec with Matchers with JsonTestUtil {

  val testCases = Table(
    ("concept", "expectedJson"),

    (
      Concept(label = "Woodwork"),
      s"""{
        "ontologyType": "Concept",
        "label": "Woodwork",
        "type": null,
        "qualifiers": []
      }"""
    ),

    (
      QualifiedConcept(
        label = "Dangerous diseases",
        concept = Concept(
          label = "disease",
          qualifiers = List(
            Concept(
              qualifierType = "general-subdivision",
              label = "dispersion & direction"
            )
          )
        )
      ),
      s"""{
        "type": "QualifiedConcept",
        "label": "Dangerous diseases",
        "concept": {
          "label": "disease",
          "type": "Concept",
          "qualifiers": [
            {
              "label": "dispersion & direction",
              "type": "Concept",
              "qualifierType": "general-subdivision",
            }
          ]
        }
      }"""
    )
  )

  it("serialises Concepts to JSON") {
    forAll(testCases) { (concept: AbstractConcept, expectedJson: String) =>
      val actualJson = toJson(concept).get
      assertJsonStringsAreEqual(actualJson, expectedJson)
    }
  }

  it("deserialises JSON as Concepts") {
    forAll(testCases) { (concept: AbstractConcept, expectedJson: String) =>
      val parsedConcept = fromJson[AbstractConcept](expectedJson).get
      parsedConcept shouldBe concept
    }
  }
}

Write some initial tests for Concept JSON [de]serialisation


package hmda.api.http.filing

import akka.actor.typed.ActorSystem
import akka.cluster.sharding.typed.scaladsl.ClusterSharding
import akka.event.{LoggingAdapter, NoLogging}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.util.Timeout
import hmda.persistence.AkkaCassandraPersistenceSpec
import org.scalatest.MustMatchers
import akka.actor.typed.scaladsl.adapter._
import akka.cluster.typed.{Cluster, Join}
import akka.http.scaladsl.model.StatusCodes
import akka.testkit._
import hmda.persistence.filing.FilingPersistence
import hmda.api.http.codec.filing.FilingCodec._
import hmda.persistence.institution.InstitutionPersistence
import io.circe.generic.auto._

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

class FilingHttpApiSpec
    extends AkkaCassandraPersistenceSpec
    with MustMatchers
    with FilingHttpApi
    with ScalatestRouteTest {

  val duration = 10.seconds

  implicit val routeTimeout = RouteTestTimeout(duration.dilated)

  override implicit val typedSystem: ActorSystem[_] = system.toTyped
  override val log: LoggingAdapter = NoLogging
  val ec: ExecutionContext = system.dispatcher
  override implicit val timeout: Timeout = Timeout(duration)
  override val sharding: ClusterSharding = ClusterSharding(typedSystem)

  override def beforeAll(): Unit = {
    super.beforeAll()
    Cluster(typedSystem).manager ! Join(Cluster(typedSystem).selfMember.address)
    InstitutionPersistence.startShardRegion(sharding)
    FilingPersistence.startShardRegion(sharding)
  }

  override def afterAll(): Unit = super.afterAll()

  "Filings" must {
    "return error when institution does not exist" in {
      Get("/institutions/xxxx/filings/2018") ~> filingRoutes ~> check {
        status mustBe StatusCodes.BadRequest
      }
    }
  }
}

Add test for empty institution

Add test for empty institution


import sbt._
import Keys._
object MyBuild extends Build {
  // ...
  val getJars = TaskKey[Unit]("get-jars")
  val getJarsTask = getJars <<= (target, fullClasspath in Runtime) map { (target, cp) =>
    println("Target path is: " + target)
    println("Full classpath is: " + cp.map(_.data).mkString(":"))
  }
  lazy val project = Project(
    "project",
    file("."),
    settings = Defaults.defaultSettings ++ Seq(getJarsTask))
}

Add task 'get-jars' prints classpath

Add task 'get-jars' prints classpath


/*
 * Copyright 2017 Spotify AB.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.spotify.hype

import com.spotify.hype.model.{RunEnvironment, VolumeRequest}
import com.spotify.hype.util.Fn
import org.scalatest.{FlatSpec, Matchers}

import scala.reflect.io.File

class LocalSubmitterTest extends FlatSpec with Matchers {

  private def testEnv = RunEnvironment.environment("spotify-hype-testing")

  // Once hype has scala module this should be obsolete
  implicit def funToFn[T](fn: () => T): Fn[T] = {
    new Fn[T] { override def run(): T = fn() }
  }

  "LocalSubmitter" should "work" in {
    val submitter = Submitter.createLocal()
    submitter.runOnCluster(() => "foobar", testEnv) should be ("foobar")
  }

  it should "support volumes write -> read" in {
    val volume = VolumeRequest.volumeRequest("foo", "10g")
    val submitter = Submitter.createLocal()

    val writeEnv = testEnv.withMount(volume.mountReadWrite("/foo"))
    val writeFn = () => {
      // side effect in volume
      File("/foo/bar.txt").appendAll("foobar in a file")
      "foobar"
    }

    val readFn = () => File("/readFoo/bar.txt").bufferedReader().readLine()
    val readEnv = testEnv.withMount(volume.mountReadOnly("/readFoo"))

    submitter.runOnCluster(writeFn, writeEnv) should be ("foobar")
    submitter.runOnCluster(readFn, readEnv) should be ("foobar in a file")
  }

}

Add tests for local runner

Add tests for local runner


package core.controllers.graphql.contact

import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.model.StatusCodes.OK
import akka.http.scaladsl.testkit.RouteTestTimeout
import akka.testkit.TestDuration
import akka.util.ByteString
import core.controllers.graphql.TestHelper
import core.controllers.graphql.jsonProtocols.GraphQLMessage
import core.controllers.graphql.jsonProtocols.GraphQLMessageJsonProtocol._
import modules.common.FieldError
import modules.contact.models.ContactPayload
import spray.json._

import scala.concurrent.duration._

class QuerySpec extends TestHelper {

  "GraphQLController" must {

    implicit val contactPayloadJsonReader: ContactPayloadJsonReader.type = ContactPayloadJsonReader
    implicit val timeout: RouteTestTimeout = RouteTestTimeout(10.seconds.dilated)

    "send a mail" in {
      val mutation = "mutation contact($input: ContactInput!) { contact(input: $input) { errors { message } } }"
      val variables =
        """{"input":
          |{"name": "test_user",
          |"email": "test_user@gmail.com",
          |"content": "Test message"}}""".stripMargin.parseJson.asJsObject
      val graphQLMessage = ByteString(GraphQLMessage(mutation, None, Some(variables)).toJson.compactPrint)
      val entity = HttpEntity(`application/json`, graphQLMessage)
      Post(endpoint, entity) ~> routes ~> check {
        val contactPayload = responseAs[String].parseJson.convertTo[ContactPayload]

        status shouldBe OK
        contentType.mediaType shouldBe `application/json`

        contactPayload.errors should not be defined
      }
    }
  }
}

object ContactPayloadJsonReader extends JsonReader[ContactPayload] with DefaultJsonProtocol {
  override def read(json: JsValue): ContactPayload = {
    println(json)
    json.asJsObject.getFields("data", "errors") match {
      case Seq(_) => ContactPayload(None)
      case Seq(_, JsArray(errors)) =>
        val errorsList = errors.map {
          error =>
            FieldError("", error.asJsObject.fields.head._2.convertTo[String])
        }.toList
        ContactPayload(Some(errorsList))
      case _ => throw DeserializationException("")
    }
  }
}

Add querySpec for contact module

Add querySpec for contact module


object OpaquePatmat {
  opaque type Pos = Int
  object Pos {
    def mkPos(i: Int): Pos = {
      require(i > 0)
      i
    }
    def coerce[F[_]](fa: F[Int]): F[Pos] = fa
  }

  sealed trait Expr[T]
  final case class PosExpr(p: Pos) extends Expr[Pos]
  final case class IntExpr(i: Int) extends Expr[Int]
  final case class StrExpr(s: String) extends Expr[String]

  def eval(e: Expr[Pos]): Pos = e match {
    case PosExpr(p) => p
    // both of the patterns need to be well-typed here,
    // since Pos is potentially equal to any other type
    case IntExpr(_) => Pos.mkPos(1)
    case StrExpr(_) => ???
  }

  eval(Pos.coerce[Expr](IntExpr(-1)))
}

Test interaction between opaque and match


/*
 * Copyright 2015 – 2016 Martin Seeler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package rx.oanda.orders

import cats.data.Xor
import io.circe.DecodingFailure
import io.circe.parser._
import org.scalatest._
import rx.oanda.utils.Buy

class ClosedOrderDecoderSpec extends FlatSpec with Matchers {

  behavior of "The ClosedOrder Decoder"

  it must "parse a closed order from valid json" in {
    val json =
      """
        |{
        |  "id": 10234643940,
        |  "instrument": "EUR_USD",
        |  "price": 1.29343,
        |  "side": "buy",
        |  "time": "1453326442000000",
        |  "type": "BuyEntry",
        |  "units": 1
        |}
      """.stripMargin

    decode[ClosedOrder](json) should matchPattern {
      case Xor.Right(ClosedOrder(10234643940L, "EUR_USD", 1.29343, Buy, 1453326442000000L, 1)) ⇒
    }
  }

  it must "fail on missing property" in {
    val json =
      """
        |{
        |  "id": 10234643940,
        |  "instrument": "EUR_USD",
        |  "side": "buy",
        |  "time": "1453326442000000",
        |  "type": "BuyEntry",
        |  "units": 1
        |}
      """.stripMargin

    decode[ClosedOrder](json) should matchPattern {
      case Xor.Left(e: DecodingFailure) ⇒ //...
    }
  }

}

Add spec for ClosedOrder Decoder

Add spec for ClosedOrder Decoder


object Solution {

  def nums(n: Int, m: Int): Long = n match {
    case 1 => m
    case 2 => m * (m - 1)
    case x =>
      val pre = m * (m - 1)
      if (pre == 0) 0
      else
        (pre * BigInt(m - 2).modPow(n - 2, 1e9.toInt + 7)).mod(1e9.toInt + 7).toInt
  }

  def main(args: Array[String]) {
    for (_ <- 1 to readInt) {
      val Array(n, m) = readLine.split(" ").map(_.toInt)
      println(nums(n, m))
    }
  }
}

Add partial solution to problem Antipalindromic Strings.


/*
 * Copyright (c) <2014>, Amanj Sherwany <http://www.amanj.me>
 * All rights reserved.
 * */
package ch.usi.inf.l3.lombrello.plugin

// import transformers._
// import scala.tools.nsc.transform.TypingTransformers
// import scala.tools.nsc.ast.TreeDSL
// import scala.tools.nsc.transform.Transform
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.Phase
// import scala.language.implicitConversions
// import scala.reflect.runtime.universe._
// import ch.usi.inf.l3.lombrello.util.SymbolsUtil

abstract class CheckerPluginComponent(val plugin: LombrelloPlugin)
  extends PluginComponent {

  import plugin._

  val global: plugin.global.type = plugin.global

  import global._

  def newPhase(_prev: Phase): StdPhase

  abstract class CheckerComponent(override val prev: Phase)
    extends StdPhase(prev) {

    val global: plugin.global.type = plugin.global
  }

}

Add the base class of Lombrello's checker phases


package edu.stanford.cme323.spark.smti

import org.apache.spark.rdd.RDD

private[smti] case class BasicProp (
  val listPos: Int = 0
)

private[smti] case class BasicAccp (
  val curRank: Rank = LastRank
)

class SMTIGSBasic (propPrefList: RDD[(Index, PrefList)], accpPrefList: RDD[(Index, PrefList)])
  extends SMTIGS[BasicProp, BasicAccp](propPrefList, accpPrefList,
    new BasicProp(), new BasicAccp()) {

  private case class Proposal(
    val from: Index
  )

  private case class Response(
    val from: Index
  )

  def run(maxRounds: Int = Int.MaxValue) {

    def propActive = (person: Proposer) => {
      person.status.listPos < person.prefList.size && person.fiance == InvIndex
    }

    def propMakeProposal = (selfIdx: Index, person: Proposer) => {
      val listPos = person.status.listPos % person.prefList.size
      val favoriteIndex = person.prefList.getFavorite(listPos)._1
      (favoriteIndex, Proposal(selfIdx))
    }

    def accpMakeResponse = (selfIdx: Index, person: Acceptor) => {
      (person.fiance, Response(selfIdx))
    }

    def propHandleResponse = (person: Proposer, optn: Option[Response]) => {
      var listPos = person.status.listPos
      if (optn.isEmpty) {
        // no response received
        // break up with previous fiance, or proposing fails
        listPos += 1
        new Person(person.prefList, InvIndex, BasicProp(listPos))
      } else {
        val resp = optn.get
        // keep relationship or proposing succeeds
        assert(person.fiance == resp.from || person.fiance == InvIndex)
        new Person(person.prefList, resp.from, BasicProp(listPos))
      }
    }

    def accpHandleProposal = (person: Acceptor, optn: Option[Iterable[Proposal]]) => {
      if (optn.isEmpty) {
        // no proposal received
        person
      } else {
        val iter = optn.get
        // select the best proposal
        val bestProp = iter.minBy( prop => person.prefList.getRankOf(prop.from) )
        val newRank = person.prefList.getRankOf(bestProp.from)
        if (newRank < person.status.curRank) {
          // accept
          new Acceptor(person.prefList, bestProp.from, BasicAccp(newRank))
        } else {
          person
        }
      }
    }

    doMatching(maxRounds,
      propActive,
      propMakeProposal,
      accpMakeResponse,
      propHandleResponse,
      accpHandleProposal)
  }

}

Add GS basic algorithm implementation.

Add GS basic algorithm implementation.


/**
 *
 */
package ru.wordmetrix.webcrawler
import WebCrawler._
import ru.wordmetrix.webcrawler.ActorDebug._
import scala.actors.Actor

trait SampleHierarchy2PriorityBase extends Actor

class SampleHierarchy2PriorityStub  extends SampleHierarchy2PriorityBase {
    def act {}
}

Implement a stub for SampleHierrchy2Priority


package model

import org.specs2.mutable.Specification
import org.squeryl.PrimitiveTypeMode.{transaction, inTransaction, from, select, where}
import play.api.test.FakeApplication
import play.api.test.Helpers.{running,inMemoryDatabase}
import org.specs2.specification.BeforeExample
import org.specs2.specification.AfterExample
import org.specs2.specification.BeforeContextExample

class SponsorSpec extends Specification {

  "Sponsor model" should {
    "be insertable" in {
      running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
        transaction {

          val sponsor: Sponsor = new Sponsor("name", "title", "description", "website", 1, None, None)
          Sponsor.insert(sponsor)

          sponsor.isPersisted must beTrue
          sponsor.id must beGreaterThan(0L)
        }
      }
    }
    "be retrivable by id" in {
      running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
        transaction {

          val sponsor: Sponsor = new Sponsor("name", "title", "description", "website", 1, None, None)
          Sponsor.insert(sponsor)

          sponsor.isPersisted must beTrue

          val sponsorDb: Option[Sponsor] = Sponsor.lookup(sponsor.id)

          sponsorDb.isEmpty must beFalse
          sponsorDb.get.id must equalTo(sponsor.id)
        }
      }
    }
    "be retrivable by id with hackaton relationship" in {
      running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
        transaction {

          val sponsor: Sponsor = new Sponsor("name", "title", "description", "website", 1, Some(1L), None)
          Sponsor.insert(sponsor)

          sponsor.isPersisted must beTrue

          val sponsorDb: Option[Sponsor] = Sponsor.lookup(sponsor.id)

          sponsorDb.isEmpty must beFalse
          sponsorDb.get.hackathon.get.organiser must not beNull
        }
      }
    }
    "be retrivable in bulk" in {
      running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
        transaction {

          val originalSize = Sponsor.all.size

          Sponsor.insert(new Sponsor("name", "title", "description", "website", 1, None, None))
          Sponsor.insert(new Sponsor("name", "title", "description", "website", 1, None, None))

          val sponsorList: Iterable[Sponsor] = Sponsor.all
          sponsorList must have size(originalSize + 2)
        }
      }
    }
  }
}

Add tests for Sponsor model

Add tests for Sponsor model


package com.softwaremill.codebrag.dao

import org.bson.types.ObjectId

object ObjectIdTestUtils {
  implicit def intSuffixToObjId(number: Int) = new ObjectId(intSuffixToStringId(number))
  implicit def intSuffixToStringId(number: Int) = "507f191e810c19729de860e" + number

}

Add test utils to easily generate object ids


package scala.in.programming.abstract_internal_control

/**
  * @author loustler
  * @since 02/17/2017 01:56
  */
object SimpleCodeForClientCodes {
  def containsNeg(nums: List[Int]): Boolean = {
    var exists = false

    for ( num <- nums )
      if ( num < 0 )
        exists = true

    exists
  }

  def containsNeg2(nums: List[Int]) = nums.exists(_ < 0)

  def containsOdd(nums: List[Int]): Boolean = {
    var exists = false

    for ( num <- nums )
      if ( num % 2 == 1 )
        exists = true

    exists
  }

  def containsOdd2(nums: List[Int]) = nums.exists(_ % 2 == 1)
}

Add example that make simple to client's code using HOF


final class Outer {

  sealed trait Inner

  final case class Inner1(foo: Int) extends Inner

  val inner: Outer#Inner = Inner1(0)

  def bar = inner match {

    case Inner1(i) => i

  }

}

object Test {

  def main(args: Array[String]): Unit = {
    val s = (new Outer).bar
    assert(s == 0)
  }

}

Test that SI-4400 stays fixed.

Test that SI-4400 stays fixed.


object Solution {
  import scala.annotation.tailrec

  @tailrec
  def gcd(a: Int, b: Int): Int = if (a == 0) b else gcd(b % a, a)

  def numberOfTriangles(n: Int): Long = {
    var p = 3L * n * n
    for {
      x <- 1 to n
      y <- 1 to n
    } {
      val fact = gcd(x, y)
      p += math.min(y * fact / x, (n - x) * fact / y) * 2L
    }
    p
  }

  def main(args: Array[String]) {
    val n = readInt
    println(numberOfTriangles(n))
  }
}

Add solution to problem 91. This is based on mathblog.


trait A {
 var str = ""
 str = "a"
 val s = str += 'A'
 str += '1'
}

trait B extends A {
 str += 'b'
 override val s = str += 'B'
 str += '2'
}

class D extends A {
 str += 'd'
 override val s = str += 'D'
 str += '3'
}

object Test extends D with B {
 // should only have 2 fields
 str += 'E'
 def main(args: Array[String]) = assert(str == "aA1dD3bB2E4")
 str += '4'
}

Add a test for trait initialization.

Add a test for trait initialization.


object Solution {
  import scala.collection.mutable.Set

  def generateSolution(n: Int, total: Int): Seq[String] = {
    val result = Set.empty[Vector[Int]]
    val maxSum = 4 * n - 1
    val pairSum = Array.fill(maxSum + 1)(Set.empty[(Int, Int)])
    for {
      i <- 1 to 2 * n
      j <- i + 1 to 2 * n
    } pairSum(i + j).add((i, j))

    def search(arr: Vector[Int], idx: Int, visted: Set[Int]) {
      if (arr(0) == 0) { // initial state, try all the possible
        for {
          i <- 1 to n + 1 if total - i >= 0 && total - i <= maxSum
          (l, r) <- pairSum(total - i) if l != i && r != i
        } {
          val newVisted = visted + i + l + r
          val newArray = arr.updated(0, i)
          search(newArray.updated(n, l).updated(n + 1, r), 1, newVisted)
          search(newArray.updated(n, r).updated(n + 1, l), 1, newVisted)
        }
      } else {
        if (idx == n - 1) { // the final choice. Check if it can be a magic ring.
          val last = total - arr(2 * n - 1) - arr(n)
          if (!visted(last) && last >= arr(0) && last <= 2 * n)
            result.add(arr.updated(idx, last))
          else return // not a valid solution
        } else {
          val known = arr(n + idx)
          if (total - known < 0 || total - known > maxSum) return
          for {
            (l, r) <- pairSum(total - known)
            if !visted(l) && !visted(r) && r > arr(0)
          } {
            val newVisted = visted + l + r
            if (l > arr(0)) {
              val newArray = arr.updated(n + idx + 1, r).updated(idx, l)
              search(newArray, idx + 1, newVisted)
            }
            val newArray = arr.updated(n + idx + 1, l).updated(idx, r)
            search(newArray, idx + 1, newVisted)
          }
        }
      }
    }

    def vectorToString(arr: Vector[Int]): String = {
      (for (i <- 0 until n) yield "" + arr(i) + arr(n + i) + arr(n + (i + 1) % n)).mkString
    }

    search(Vector.fill(2 * n)(0), 0, Set.empty[Int])
    result.toList.map(vectorToString).sorted
  }

  def main(args: Array[String]) {
    val Array(n, total) = readLine.split(" ").map(_.toInt)
    println(generateSolution(n, total).mkString("\n"))
  }
}

Add solution to problem Magic N-gon Ring. It can be solved by simple recursive search. However it's kindle of slow. We can optimize this code by doing more checks, will consider doing that later.


/**
 * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
 */
package scala.async
package neg

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test

@RunWith(classOf[JUnit4])
class AnfTransformNegSpec {

  @Test
  def `inlining block produces duplicate definition`() {
    expectError("x is already defined as value x", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") {
      """
        | import scala.concurrent.ExecutionContext.Implicits.global
        | import scala.concurrent.Future
        | import scala.async.Async._
        |
        | async {
        |   val f = Future { 12 }
        |   val x = await(f)
        |
        |   {
        |     val x = 42
        |     println(x)
        |   }
        |
        |   x
        | }
      """.stripMargin
    }
  }

  @Test
  def `inlining block in tail position produces duplicate definition`() {
    expectError("x is already defined as value x", "-cp target/scala-2.10/classes -deprecation -Xfatal-warnings") {
      """
        | import scala.concurrent.ExecutionContext.Implicits.global
        | import scala.concurrent.Future
        | import scala.async.Async._
        |
        | async {
        |   val f = Future { 12 }
        |   val x = await(f)
        |
        |   {
        |     val x = 42
        |     x
        |   }
        | }
      """.stripMargin
    }
  }
}

Add negative tests for inlining blocks in ANF transform


package vtpassim

import org.apache.spark.sql.SparkSession

import scala.collection.mutable.StringBuilder
import scala.util.Try
import scala.xml.pull._

object TEIPages {
  def main(args: Array[String]) {
    val spark = SparkSession.builder().appName("TEIPages Import").getOrCreate()
    import spark.implicits._

    spark.sparkContext.hadoopConfiguration
      .set("mapreduce.input.fileinputformat.input.dir.recursive", "true")

    spark.sparkContext.binaryFiles(args(0), spark.sparkContext.defaultParallelism)
      .filter(_._1.endsWith(".xml"))
      .flatMap( in => {
        val buf = new StringBuilder
        var buffering = false
        var seq = -1
        var id = ""

        val pass = new XMLEventReader(scala.io.Source.fromURL(in._1))
        pass.flatMap { event =>
          event match {
            case EvElemStart(_, "idno", attr, _) => {
              if ( id == "" ) {
                buffering = true
                buf.clear
              }
              None
            }
            case EvElemEnd(_, "idno") => {
              if ( id == "" ) {
                val rawid = buf.toString.trim
                id = (if ( rawid != "" ) rawid else in._1)
                buffering = false
                buf.clear
              }
              None
            }
            case EvElemStart(_, "pb", attr, _) => {
              val rec = if ( buffering ) {
                Some((f"$id%s_$seq%04d", id, seq, buf.toString.trim))
              } else {
                None
              }
              buffering = true
              seq += 1
              buf.clear
              rec
            }
            case EvElemEnd(_, "text") => {
              if ( buffering ) {
                seq += 1
                val text = buf.toString.trim
                buffering = false
                buf.clear
                Some((f"$id%s_$seq%04d", id, seq, text))
              } else {
                None
              }
            }
            case EvText(t) => {
              if ( buffering ) buf ++= t
              None
            }
            case EvEntityRef(n) => {
              if ( buffering ) buf ++= "&" + n + ";"
              None
            }
            case _ => None
          }
        }
      })
      .toDF("id", "book", "seq", "text")
      .write.save(args(1))
    spark.stop()
  }
}

Add simple TEI page record extractor.

Add simple TEI page record extractor.


object Solution {

  def main(args: Array[String]) {
    val s = readLine
    val charSet = (for (c <- s.toLowerCase if c != ' ') yield c).toSet
    if (charSet.size == 26) {
      println("pangram")
    } else {
      println("not pangram")
    }
  }
}

Add solution to Pangram problem.

Add solution to Pangram problem.


package io.flow.play.actors

import akka.actor.ActorRef
import io.flow.play.util.DefaultConfig
import play.api.Logger
import play.api.libs.concurrent.Akka
import play.api.Play.current
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

/**
  * Common utilities to help with logging, scheduling of actors at FLow.
  */
trait Util {

  implicit val mainActorExecutionContext: ExecutionContext = Akka.system.dispatchers.lookup("main-actor-context")

  /**
   * Helper to schedule a message to be sent on a recurring interval
   * based on a configuration parameter.
   *
   * @param configName The name of the configuration parameter containing the number
   *        of seconds between runs. You can also optionally add a configuration
   *        parameter of the same name with "_inital" appended to set the initial
   *        interval if you wish it to be different.
   */
  def scheduleRecurring[T](
    actor: ActorRef,
    configName: String,
    message: T
  ) {
    val seconds = DefaultConfig.requiredString(configName).toInt
    val initial = DefaultConfig.optionalString(s"${configName}_initial").map(_.toInt).getOrElse(seconds)
    Logger.info(s"scheduling a periodic message[$message]. Initial[$initial seconds], recurring[$seconds seconds]")
    Akka.system.scheduler.schedule(
      FiniteDuration(initial, SECONDS),
      FiniteDuration(seconds, SECONDS),
      actor, message
    )
  }

  def withErrorHandler[T](
    description: Any
  ) (
    f: => T
  ) {
    try {
      f
    } catch {
      case t: Throwable => {
        Logger.error(msg(s"$description: ${t}") , t)
      }
    }
  }

  def withVerboseErrorHandler[T](
    description: Any
  ) (
    f: => T
  ) {
    Logger.info(msg(description.toString))
    withErrorHandler(description)(f)
  }

  def logUnhandledMessage[T](
    description: Any
  ) {
    Logger.error(msg(s"got an unhandled message: $description"))
  }

  private[this] def msg(value: String) = {
    s"${getClass.getName}: $value"
  }

}

Add actor util (initially from organization repo)


package quotidian.web.controller

import quotidian.DatastoreSpecification
import quotidian.model.Quote

object DatastoreQuoteControllerSpecs extends DatastoreSpecification {
	val controller = new DatastoreQuoteController
	"A controller" should {
		val quote = Quote("text","source","context")
		"save a quote" >> {
			val key = controller.save(quote)
			key must notBeNull
		}
	}
}

Add start of specs trying to exercise the IndexWriter


package org.bjean.sample.wordcount.processor

import java.io.{ByteArrayOutputStream, PrintStream}
import java.nio.file.{Files, Paths}

import org.scalatest.{BeforeAndAfter, FlatSpec, Matchers}

class WordCountAppTest extends FlatSpec with Matchers  {

  "A Word Count App" should " return usage when input is not defined" in {
    val baos = new ByteArrayOutputStream
    val ps = new PrintStream(baos)
    Console.withOut(ps)(WordCountApp.main(Array("--output", "myout")))

    baos.toString shouldBe theUsage
  }

  it should " return usage when output is not defined" in {
    val baos = new ByteArrayOutputStream
    val ps = new PrintStream(baos)
    Console.withOut(ps)(WordCountApp.main(Array("--input", "myin")))

    baos.toString shouldBe theUsage
  }

  private def theUsage: String = {
    """Enhanced Word Count 1.0
      |Usage: spark-submit --deploy-mode cluster
      |             --master yarn-cluster
      |             --class org.bjean.sample.wordcount.processor.WordCountApp
      |             pathTo/processing-assembly-0.1-SNAPSHOT.jar
      |             --input hdfs://somepath/
      |             --output hdfs://outpath/,
      |             --wordLength 4 [options]
      |
      |  -i <URI> | --input <URI>
      |        Data Input. Comma separated spark path ie:./somePath,htfs://somepath/,s3://somepath
      |  -o <URI> | --output <URI>
      |        Spark output path ie: s3://someDestinationPath
      |  -w <number> | --wordLength <number>
      |        Number of char withing a work to create a pattern. Default 4
      |To get Better Result run in a cluster spark
      |
      |  --help
      |        Prints this usage text
      |""".stripMargin
  }

  private def inputFilePath(name: String) = getClass.getResource(s"/input/${name}.txt").getPath
  private def outputFilePath(name: String) = getClass.getResource(s"/output/${name}.txt").getPath

}

Add Test on Main class

Add Test on Main class


package ml.combust.mleap.runtime.transformer.feature

import ml.combust.mleap.core.feature.ElementwiseProductModel
import ml.combust.mleap.runtime.{LeapFrame, LocalDataset, Row}
import ml.combust.mleap.runtime.types.{StructField, StructType, TensorType}
import org.apache.spark.ml.linalg.Vectors
import org.scalatest.FunSpec

/**
  * Created by hollinwilkins on 9/28/16.
  */
class ElementWiseProductSpec extends FunSpec {
  val schema = StructType(Seq(StructField("test_vec", TensorType.doubleVector()))).get
  val dataset = LocalDataset(Array(Row(Vectors.dense(Array(0.0, 20.0, 20.0)))))
  val frame = LeapFrame(schema, dataset)

  val ewp = ElementwiseProduct(inputCol = "test_vec",
    outputCol = "test_norm",
    model = ElementwiseProductModel(Vectors.dense(Array(0.5, 1.0, 0.5))))

  describe("#transform") {
    it("multiplies each input vector by a provided weight vector") {
      val frame2 = ewp.transform(frame).get
      val data = frame2.dataset.toArray(0).getVector(1)

      assert(data.toArray sameElements Array(0.0, 20.0, 10.0))
    }

    describe("with invalid input column") {
      val ewp2 = ewp.copy(inputCol = "bad_input")

      it("returns a Failure") { assert(ewp2.transform(frame).isFailure) }
    }
  }
}

Add in specs for element wise product transformer.


package org.precompiler.scala101.ch06

import java.util.Date

/**
  * Created by RL on 9/17/17.
  */
class SSH private (){
  private def time(start: Date): Unit = {
    println(s"Time elapsed: ${new Date().getTime - start.getTime}")
  }
  private def exec(command: String): Unit = {
    println(s"executing: ${command}")
  }
}

object SSH {
  def sh(command: String): Unit = {
    val ssh = new SSH()
    val start = new Date()
    try {
      ssh.exec(command)
    } finally {
      ssh.time(start)
    }
  }
}

object SSHDemo extends App {
  SSH.sh("ls")
  SSH.sh("docker ps")
}

Add execute around method pattern demo.

Add execute around method pattern demo.


package busymachines.rest

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import cats.effect.IO
import org.scalatest.FlatSpec

import scala.concurrent.ExecutionContext

/**
  *
  * @author Lorand Szakacs, lsz@lorandszakacs.com, lorand.szakacs@busymachines.com
  * @since 22 Dec 2017
  *
  */
class HttpServerTest extends FlatSpec {

  private lazy val restAPI: RestAPI = new JsonRestAPI with Directives {
    override protected def routeDefinition: Route = {
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "
Hello commons!
"))
        }
      }
    }
  }

  behavior of "HttpServer"

  it should "... correctly bind port, and cleanup in default case" in {
    implicit val as: ActorSystem       = ActorSystem("http-server-test")
    implicit val am: ActorMaterializer = ActorMaterializer()
    implicit val ec: ExecutionContext  = as.dispatcher

    val httpServer = HttpServer(
      name  = "HttpServerTest",
      route = restAPI.route,
      config = MinimalWebServerConfig(
        host = "0.0.0.0",
        port = 15898
      )
    ).startThenWaitUntilShutdownDoCustomCleanup(
      waitForShutdownIO = ctx => ctx.logNormalIO("shutting down immediately as part of test"),
      cleanupIO         = ctx => ctx.terminateActorSystemIO
    )

    val terminationFuture = IO.fromFuture(IO(as.whenTerminated))

    httpServer.unsafeRunSync()

    terminationFuture.unsafeRunSync()
    succeed
  }

  //unfortunately, this can never be run as a test because it requires a shutdown of the JVM
  ignore should "... wait until JVM shutdown to do cleanup — code example" in {
    implicit val as: ActorSystem       = ActorSystem("http-server-test")
    implicit val am: ActorMaterializer = ActorMaterializer()
    implicit val ec: ExecutionContext  = as.dispatcher

    val httpServer = HttpServer(
      name  = "HttpServerTest",
      route = restAPI.route,
      config = MinimalWebServerConfig(
        host = "0.0.0.0",
        port = 15898
      )
    ).startThenCleanUpActorSystem

    val terminationFuture = IO.fromFuture(IO(as.whenTerminated))

    httpServer.unsafeRunSync()

    terminationFuture.unsafeRunSync()
    succeed
  }
}

Add test for HttpServer bind + cleanup w/ custom shutdown hook


package org.http4s
package util

import scalaz.concurrent.Task
import scalaz.stream.Process

class ProcessAppSpec extends Http4sSpec {

  "ProcessApp" should {
    "Terminate Server on a Initial Process" in {
      val myError = new Throwable("Bad Initial Process")
      class TestApp extends ProcessApp {
        override def main(args: List[String]): Process[Task, Unit] = {
          Process.fail(myError)
        }
      }
      new TestApp().main(Array.empty[String]) should_== (())
    }

    "Terminate Server on a Bad Second Value" in {
      val myError = new Throwable("Bad Secondary Process")
      class TestApp extends ProcessApp {
        override def main(args: List[String]): Process[Task, Unit] = {
          Process.emit(()).append(Process.fail(myError))
        }
      }
      new TestApp().main(Array.empty[String]) should_== (())
    }

    "Terminate Server on a Valid Process" in {
      class TestApp extends ProcessApp {
        override def main(args: List[String]): Process[Task, Unit] = {
          Process.empty[Task, String].append(Process.emit("Valid Process")).map(_ => ())
        }
      }
      new TestApp().main(Array.empty[String]) should_== (())
    }

    "Terminate Server on Bad Task" in {
      val myError = new Throwable("Bad Task")
      class TestApp extends ProcessApp {
        override def main(args: List[String]): Process[Task, Unit] = {
          Process.eval(Task.fail(myError))
        }
      }
      new TestApp().main(Array.empty[String]) should_== (())
    }

    "Terminate On A Valid Task" in {
      class TestApp extends ProcessApp {
        override def main(args: List[String]): Process[Task, Unit] = {
          Process.emit(Task("Valid Task"))
        }
      }
      new TestApp().main(Array.empty[String]) should_== (())
    }
  }

}

Include Testing Suite for ProcessApp

Include Testing Suite for ProcessApp


/**
 * Copyright 2016 Matthew Farmer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
**/
import frmr.scyig.matching.scoring

import frmr.scyig.Generators._
import frmr.scyig.matching.models._
import frmr.scyig.matching.scoring._
import java.util.UUID
import org.scalatest._
import org.scalatest.prop._
import net.liftweb.common._
import scala.util.Random

class ScoreActionGeneratorSpec extends WordSpec with GeneratorDrivenPropertyChecks with Matchers {
  "ScoreActionGenerator" should {
    "generate correct scoring actions" in {
      forAll(multipleScheduledRoundMatchesGen) { scheduledRounds =>
        val scoresByTrial: Map[UUID, ScoreByTeam] = scheduledRounds.collect({
          case trial: Trial =>
            val pScore = Random.nextInt(100)
            val dScore = Random.nextInt(100)
            (trial.id, ScoreByTeam(pScore, dScore))
        }).toMap

        val actionGenerator = new ScoreActionGenerator(() => Full(scheduledRounds))
        val scoringActions = actionGenerator.generateScoringActions(scoresByTrial).openOrThrowException("error in scoring action generator")

        scoringActions.foreach { scoringAction =>
          scoringAction match {
            case RecordHistoricalBye(teamId) =>
              val locatedBye = scheduledRounds.collectFirst {
                case bye: Bye if bye.team.id == teamId =>
                  bye
              }

              locatedBye.isDefined should equal(true)

            case RecordHistoricalTrial(histTrial) =>
              val locatedTrial = scheduledRounds.collectFirst {
                case trial @ Trial(p, d, pj, sj, _, _) if p.id == histTrial.prosecutionIdentifier &&
                                                          d.id == histTrial.defenseIdentifier &&
                                                          pj.id == histTrial.presidingJudgeIdentifier &&
                                                          sj.map(_.id) == histTrial.scoringJudgeIdentifier =>
                  trial
              }

              locatedTrial.isDefined should equal(true)

              val openedTrial = locatedTrial.get
              scoresByTrial.get(openedTrial.id).isDefined should equal(true)

              val openedScores = scoresByTrial.get(openedTrial.id).get
              openedScores.prosecution should equal(histTrial.prosecutionScore)
              openedScores.defense should equal(histTrial.defenseScore)

            case DenoteScoringError(error) =>
              fail(s"Scoring error was surfaced: $error")
          }
        }
      }
    }
  }
}

Add a spec covering the score action generator


package core.guice.modules

import akka.actor.{Actor, ActorSystem}
import com.google.inject.Provides
import com.google.inject.name.Names
import common.actors.DispatcherActor
import core.guice.injection.GuiceActorRefProvider
import javax.inject.Named
import net.codingwell.scalaguice.ScalaModule

class ActorModule extends ScalaModule with GuiceActorRefProvider {

  override def configure(): Unit = {
    bind[Actor].annotatedWith(Names.named(DispatcherActor.name)).to[DispatcherActor]
  }

  @Provides
  @Named(DispatcherActor.name)
  def dispatcherActor(implicit actorSystem: ActorSystem) = {
    provideActorRef(DispatcherActor.name)
  }
}

Add bind of 'Actor' and 'ActorRef' to 'DispatcherActor'


/*
 * Copyright 2015 – 2016 Martin Seeler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package rx.oanda.rates

import cats.data.Xor
import io.circe.DecodingFailure
import io.circe.parse._
import org.scalatest._

class OandaTickDecoderSpec extends FlatSpec with Matchers {

  behavior of "The Oanda Tick Decoder"

  it must "parse a tick from valid json" in {
    val json =
      """
        |{
        | "instrument": "EUR_USD",
        | "time": "1453847424597195",
        | "bid": 1.08646,
        | "ask": 1.08668
        |}
      """.stripMargin
    decode[OandaTick](json) should matchPattern {
      case Xor.Right(OandaTick("EUR_USD", 1453847424597195L, 1.08646, 1.08668)) ⇒
    }
  }

  it must "fail on missing property" in {
    val json =
      """
        |{
        | "instrument": "EUR_USD",
        | "time": "1453847424597195",
        | "ask": 1.08668
        |}
      """.stripMargin
    decode[OandaTick](json) should matchPattern {
      case Xor.Left(e: DecodingFailure) ⇒ //...
    }
  }

  it must "parse a list of prices from valid json" in {
    val json =
      """
        |{
        | "prices": [
        |  {
        |   "instrument": "EUR_USD",
        |   "time": "1453847424597195",
        |   "bid": 1.08646,
        |   "ask": 1.08668
        |  }
        | ]
        |}
      """.stripMargin
    decode[Vector[OandaTick]](json) should matchPattern {
      case Xor.Right(Vector(
        OandaTick("EUR_USD", 1453847424597195L, 1.08646, 1.08668)
      )) ⇒
    }
  }

}

Add test for tick decoding

Add test for tick decoding


trait Scope
class MScope extends Scope

case class CI(pre: Int, decls: Scope) {
  def derivedCI(pre: Int) = new CI(pre, decls)
  def derivedCI(pre: Int = this.pre, decls: Scope = this.decls) = new CI(pre, decls)
}

object Test {
  def ci = new CI(1, new MScope)
  val decls1 = new MScope
  ci.derivedCI(2, decls = decls1)
  ci.derivedCI(pre = 2)
  ci.derivedCI(decls = decls1)
}

Add test to illustrate overloading problem.


package org.apache.spark.streaming.dstream

import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.event.Event
import org.apache.spark.streaming.{Dependency, Duration, TimeWindowDependency}

import scala.reflect.ClassTag

private[streaming]
class TimeWindowedDStream[T: ClassTag](
    parent: DStream[T],
    _window: Duration,
    _slide: Duration
  ) extends DStream[T](parent.ssc) {

  // Persist parent level by default, as those RDDs are going to be obviously reused.
  parent.persist(StorageLevel.MEMORY_ONLY_SER)

  def windowLength: Duration = _window

  def slideLength: Duration = _slide

  override def dependencies: List[Dependency[_]] = {
    List(new TimeWindowDependency[T](parent, parent.zeroTime, windowLength, slideLength))
  }

  override def slideDuration: Duration = slideLength

  override def parentRememberDuration: Duration = rememberDuration + windowLength

  override def persist(level: StorageLevel): DStream[T] = {
    // Do not let this windowed DStream be persisted as windowed (union-ed) RDDs share underlying
    // RDDs and persisting the windowed RDDs would store numerous copies of the underlying data.
    // Instead control the persistence of the parent DStream.
    parent.persist(level)
    this
  }

  override def compute(event: Event): Option[RDD[T]] = {
    // Compute the window that ends at or just before 'event'.
    val dependency = dependencies.head.asInstanceOf[TimeWindowDependency[T]]
    val rddsInWindow = dependency.rdds(event)
    // A window is computed if 'event' is the only event at the end or after it.
    if (rddsInWindow.nonEmpty && dependency.eventsAfterPrevWindow(event).size <= 1) {
      Some(ssc.sc.union(rddsInWindow))
    } else {
      None
    }
  }
}

Implement a time window stream as opposed to tail window stream


package sampler.abc

import org.scalatest.FreeSpec
import play.api.libs.json.JsValue

class PopulationTest extends FreeSpec {
  case class TestParams(s: String, d: Double, i: Int)

  "Population should" - {
    "render json" in { fail("TODO") }

    "parse from json" in {
      val jsonStr = """
        {
          "generation" : 3,
          "tolerance" : 0.975,
          "acceptance-ratio" : 0.25,
          "particles" : {
      		  "a-string":  ["hay", "bee", "sea"],
            "b-double" : [ 0.3,  0.4,   0.5 ],
            "c-int" :    [ 6,    7,     8 ],
            "weight" :   [ 1,    3,     2 ]
          }
        }"""

      def parameterParser(map: Map[String, JsValue]): TestParams =
        TestParams(
          map("a-string").as[String],
          map("b-double").as[Double],
          map("c-int").as[Int])

      val expected = Population[TestParams](
        Map[TestParams, Double](
            TestParams("hay", 0.3, 6) -> 1,
            TestParams("bee", 0.4, 7) -> 3,
            TestParams("sea", 0.5, 8) -> 2),
	      3,
		    0.975,
	    	0.25
      )
      assertResult(expected){
        Population.fromJson(jsonStr, parameterParser _)
      }
    }

    "build proposal distribution using weights table" in { fail("TODO") }
  }
}

Test for parsing json Population files

Test for parsing json Population files


package doodle
package examples

import cats.implicits._
import doodle.algebra.{Picture, Text}
import doodle.language.Basic
import doodle.syntax._

/**
 * Really basic example of a picture that abstracts over algebras, and hence is
 * independent of back-end.
 */
object Letters {
  def letters[Alg[x[_]] <: Basic[x] with Text[x], F[_]]: Picture[Alg, F, Unit] =
    text[Alg,F]("Hi from Doodle!").on(
      square[Alg,F](40)
    )
}

Add example of cross-backend text rendering


addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.1")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.11")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.1.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.8.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.3")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.1")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.11")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.1.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.9.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.3")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

Update sbt-scalajs, scalajs-compiler, ... to 1.9.0 in master


/*
 * Copyright (c) 2014. Mount Sinai School of Medicine
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.berkeley.cs.amplab.adam.predicates

import edu.berkeley.cs.amplab.adam.util.{ParquetLogger, SparkFunSuite}
import org.scalatest.BeforeAndAfter
import java.util.logging.Level
import java.io.File
import edu.berkeley.cs.amplab.adam.avro.{ADAMContig, ADAMVariant, ADAMGenotype}
import org.apache.spark.rdd.RDD
import edu.berkeley.cs.amplab.adam.rdd.AdamContext._
import com.google.common.io.Files
import org.apache.commons.io.FileUtils

class GenotypePredicatesSuite extends SparkFunSuite with BeforeAndAfter {
  var genotypesParquetFile: File   = null

  sparkBefore("genotypepredicatessuite_before") {
    ParquetLogger.hadoopLoggerLevel(Level.SEVERE)

    val v0 = ADAMVariant.newBuilder
      .setContig(ADAMContig.newBuilder.setContigName("11").build)
      .setPosition(17409571)
      .setReferenceAllele("T")
      .setVariantAllele("C")
      .build

    val genotypes = sc.parallelize(List(
      ADAMGenotype.newBuilder().setVariant(v0).setVarIsFiltered(true).build(),
      ADAMGenotype.newBuilder().setVariant(v0).build()
    ))

    genotypesParquetFile = new File(Files.createTempDir(), "genotypes")
    if (genotypesParquetFile.exists())
      FileUtils.deleteDirectory(genotypesParquetFile.getParentFile)

    genotypes.adamSave(genotypesParquetFile.getAbsolutePath)
  }

  after {
    FileUtils.deleteDirectory(genotypesParquetFile.getParentFile)
  }

  sparkTest("Return only PASSing records") {
    val gts1: RDD[ADAMGenotype] = sc.adamLoad(
      genotypesParquetFile.getAbsolutePath,
      predicate = Some(classOf[GenotypeVarFilterPASSPredicate])
    )
    assert(gts1.count === 1)
  }
}

Test suite for new Genotype predicate

Test suite for new Genotype predicate


package com.campudus.tableaux

import com.campudus.tableaux.database.DatabaseConnection
import com.campudus.tableaux.database.model.SystemModel
import io.vertx.ext.unit.TestContext
import io.vertx.ext.unit.junit.VertxUnitRunner
import io.vertx.scala.SQLConnection
import org.junit.Test
import org.junit.runner.RunWith
import org.vertx.scala.core.json.JsonArray

@RunWith(classOf[VertxUnitRunner])
class DatabaseVersioningTest extends TableauxTestBase {

  @Test
  def checkVersion3(implicit c: TestContext): Unit = okTest {
    val sqlConnection = SQLConnection(verticle, databaseConfig)
    val dbConnection = DatabaseConnection(verticle, sqlConnection)
    val system = SystemModel(dbConnection)

    for {
      _ <- system.uninstall()
      _ <- system.install(Some(1))
      result1 <- dbConnection.query("SELECT * FROM system_table")
      _ <- system.uninstall()
      _ <- system.install(Some(3))
      result2 <- dbConnection.query("SELECT * FROM system_table")
    } yield {
      logger.info(s"result=${result1.encode()}")
      logger.info(s"result=${result2.encode()}")
      assertTrue(!result1.getJsonArray("fields").contains("is_hidden"))
      assertTrue(result2.getJsonArray("fields").contains("is_hidden"))
    }
  }

  @Test
  def checkUpdateOfOrdering(implicit c: TestContext): Unit = okTest {
    val sqlConnection = SQLConnection(verticle, databaseConfig)
    val dbConnection = DatabaseConnection(verticle, sqlConnection)
    val system = SystemModel(dbConnection)

    for {
      _ <- system.uninstall()
      _ <- system.install(Some(1))

      // Needs to create tables like it did in database version 1
      inserted <- dbConnection.query("INSERT INTO system_table (user_table_name) VALUES ('table1'),('table2') RETURNING table_id")
      _ = logger.info(s"inserted=${inserted.encode()}")
      tableId1 = inserted.getJsonArray("results").getJsonArray(0).getLong(0)
      _ <- dbConnection.query(s"CREATE TABLE user_table_$tableId1 (id BIGSERIAL, PRIMARY KEY (id))")
      _ <- dbConnection.query(s"CREATE SEQUENCE system_columns_column_id_table_$tableId1")
      tableId2 = inserted.getJsonArray("results").getJsonArray(1).getLong(0)
      _ <- dbConnection.query(s"CREATE TABLE user_table_$tableId2 (id BIGSERIAL, PRIMARY KEY (id))")
      _ <- dbConnection.query(s"CREATE SEQUENCE system_columns_column_id_table_$tableId2")

      _ <- system.update()
      tablesOrdering <- dbConnection.query("SELECT table_id, ordering FROM system_table")
    } yield {

      assertTrue(tablesOrdering.getJsonArray("results").size() > 0)

      import scala.collection.JavaConverters._
      for {
        (tableId, ordering) <- tablesOrdering.getJsonArray("results").asScala
          .map(_.asInstanceOf[JsonArray])
          .map(elem => (elem.getLong(0), elem.getLong(1)))
      } yield {
        assertEquals(tableId, ordering)
      }
    }
  }
}

Add test for installing a smaller system version

Signed-off-by: Joern Bernhardt <7b85a41a628204b76aba4326273a3ccc74bd009a@campudus.com>


package com.johnsnowlabs.nlp.embeddings

import com.johnsnowlabs.nlp.annotators.sbd.pragmatic.SentenceDetector
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import org.apache.spark.ml.Pipeline
import org.scalatest._

class BertSentenceEmbeddingsTestSpec extends FlatSpec {

  "BertSentenceEmbeddings" should "correctly work with empty tokens" ignore {

    val testData = ResourceHelper.spark.createDataFrame(Seq(
      (1, "This is my first sentence. This is my second."),
      (2, "This is my third sentence. This is my forth.")
    )).toDF("id", "text")

    val document = new DocumentAssembler()
      .setInputCol("text")
      .setOutputCol("document")

    val sentence = new SentenceDetector()
      .setInputCols("document")
      .setOutputCol("sentence")

    val embeddings = BertSentenceEmbeddings.pretrained("sent_small_bert_L2_128")
      .setInputCols("document")
      .setOutputCol("bert")
      .setCaseSensitive(false)
      .setMaxSentenceLength(64)

    val pipeline = new Pipeline().setStages(Array(document, sentence, embeddings))

    val pipelineDF = pipeline.fit(testData).transform(testData)

    pipelineDF.select("bert.embeddings").show()

  }

}

Add unit test for BertSentenceEmbeddings


package ch.usi.inf.l3.sana.robustj.names

import ch.usi.inf.l3.sana
import sana.ooj
import sana.tiny
import tiny.names.Name

trait StdNames extends ooj.names.StdNames {
  val THROWABLE_CLASS_NAME              = Name("Throwable")
  val EXCEPTION_CLASS_NAME              = Name("Exception")
  val RUNTIME_EXCEPTION_CLASS_NAME      = Name("RuntimeException")
  val ERROR_CLASS_NAME                  = Name("Error")
}

object StdNames extends StdNames

Add standard names related to robustj

Add standard names related to robustj


package scala.tools.partest

import scala.tools.nsc.util.JavaClassPath
import scala.collection.JavaConverters._
import scala.tools.asm
import asm.ClassReader
import asm.tree.{ClassNode, MethodNode, InsnList}
import java.io.InputStream

/**
 * Providies utilities for inspecting bytecode using ASM library.
 *
 * HOW TO USE
 * 1. Create subdirectory in test/files/jvm for your test. Let's name it $TESTDIR.
 * 2. Create $TESTDIR/BytecodeSrc_1.scala that contains Scala source file which you
 *    want to inspect the bytecode for. The '_1' suffix signals to partest that it
 *    should compile this file first.
 * 3. Create $TESTDIR/Test.scala:
 *    import scala.tools.partest.BytecodeTest
 *    object Test extends BytecodeTest {
 *      def show {
 *        // your code that inspect ASM trees and prints values
 *      }
 *    }
 * 4. Create corresponding check file.
 *
 * EXAMPLE
 * See test/files/jvm/bytecode-test-example for an example of bytecode test.
 *
 */
abstract class BytecodeTest {

  /** produce the output to be compared against a checkfile */
  protected def show(): Unit

  def main(args: Array[String]): Unit = show

  protected def getMethod(classNode: ClassNode, name: String): MethodNode =
    classNode.methods.asScala.find(_.name == name) getOrElse
      sys.error(s"Didn't find method '$name' in class '${classNode.name}'")

  protected def loadClassNode(name: String): ClassNode = {
    val classBytes: InputStream = (for {
      classRep <- classpath.findClass(name)
      binary <- classRep.binary
    } yield binary.input) getOrElse sys.error(s"failed to load class '$name'; classpath = $classpath")

    val cr = new ClassReader(classBytes)
    val cn = new ClassNode()
    cr.accept(cn, 0)
    cn
  }

  protected lazy val classpath: JavaClassPath = {
    import scala.tools.nsc.util.ClassPath.DefaultJavaContext
    import scala.tools.util.PathResolver.Defaults
    // logic inspired by scala.tools.util.PathResolver implementation
    val containers = DefaultJavaContext.classesInExpandedPath(Defaults.javaUserClassPath)
    new JavaClassPath(containers, DefaultJavaContext)
  }
}

Add Bytecode test (ASM-based) to partest.

This commit introduces a new kind of test `Bytecode` that allows
one to inspect bytecode generated for given piece of Scala code.

The bytecode inspection is achieved by inspection of ASM trees.
See the included example for details.

NOTE: This commit does not introduce a new category of pratest tests.
Bytecode tests should be run in `jvm` category of partest tests.

Specific list of changes:
  * Add BytecodeTest that contains common utilities to partest
  * Add asm to classpath when compiling partest. That's not a
    new dependency as it's being already done for javac task
    we were running while compiling partest.
  * Add an example test that shows how to count null checks in
    given method.


package pureconfig.module.cats

import scala.annotation.tailrec

import cats.functor.{ Contravariant, Invariant }
import cats.{ Eq, MonadError, MonadReader }
import com.typesafe.config.ConfigValue
import pureconfig._
import pureconfig.error.{ ConfigReaderFailure, ConfigReaderFailures }

package object instances {

  implicit val configReaderCatsInstance: MonadError[ConfigReader, ConfigReaderFailures] with MonadReader[ConfigReader, ConfigValue] = {
    new MonadError[ConfigReader, ConfigReaderFailures] with MonadReader[ConfigReader, ConfigValue] {

      def pure[A](x: A): ConfigReader[A] =
        ConfigReader.fromFunction { _ => Right(x) }

      def flatMap[A, B](fa: ConfigReader[A])(f: A => ConfigReader[B]): ConfigReader[B] =
        fa.flatMap(f)

      def tailRecM[A, B](a: A)(f: A => ConfigReader[Either[A, B]]): ConfigReader[B] = ConfigReader.fromFunction { cv =>
        @tailrec
        def loop(currA: A): Either[ConfigReaderFailures, B] = f(currA).from(cv) match {
          case Left(failures) => Left(failures)
          case Right(Right(b)) => Right(b)
          case Right(Left(nextA)) => loop(nextA)
        }
        loop(a)
      }

      def raiseError[A](e: ConfigReaderFailures): ConfigReader[A] =
        ConfigReader.fromFunction { _ => Left(e) }

      def handleErrorWith[A](fa: ConfigReader[A])(f: ConfigReaderFailures => ConfigReader[A]): ConfigReader[A] =
        ConfigReader.fromFunction { cv =>
          fa.from(cv) match {
            case Left(failures) => f(failures).from(cv)
            case r @ Right(_) => r
          }
        }

      def ask: ConfigReader[ConfigValue] =
        ConfigReader.fromFunction { cv => Right(cv) }

      def local[A](f: ConfigValue => ConfigValue)(fa: ConfigReader[A]): ConfigReader[A] =
        fa.contramapConfig(f)
    }
  }

  implicit val configWriterCatsInstance: Contravariant[ConfigWriter] = new Contravariant[ConfigWriter] {
    def contramap[A, B](fa: ConfigWriter[A])(f: B => A): ConfigWriter[B] =
      fa.contramap(f)
  }

  implicit val configConvertCatsInstance: Invariant[ConfigConvert] = new Invariant[ConfigConvert] {
    def imap[A, B](fa: ConfigConvert[A])(f: A => B)(g: B => A): ConfigConvert[B] =
      fa.xmap(f, g)
  }

  implicit val configValueEq: Eq[ConfigValue] = Eq.fromUniversalEquals
  implicit val configReaderFailureEq: Eq[ConfigReaderFailure] = Eq.fromUniversalEquals
  implicit val configReaderFailuresEq: Eq[ConfigReaderFailures] = Eq.fromUniversalEquals
}

Add cats instances for ConfigReader, ConfigWriter and ConfigConvert


package org.precompiler.scala101.ch07

/**
  *
  * @author Richard Li
  */
object MethodLateBinding extends App{
  val writer1 = new StringWriter with UpperCaseWriter with TagWriter

  val writer2 = new StringWriter with TagWriter with UpperCaseWriter

  writer1.write("hello world")

  writer2.write("hello world")
}

abstract class Writer {
  def write(message: String): Unit
}

class StringWriter extends Writer{
  val writer = new java.io.StringWriter
  override def write(message: String): Unit = {
    writer.write(message)
    println(writer.toString)
  }
}

trait UpperCaseWriter extends Writer {
  abstract override def write(message: String): Unit = {
    if (message != null) {
      super.write(message.toUpperCase)
    }
  }
}

trait TagWriter extends Writer {
  abstract override def write(message: String): Unit = {
    if (message != null) {
      super.write(s"<tag>${message}</tag>")
    }
  }
}

Add method late binding demo.

Add method late binding demo.


package lampetia.security.spray.module

import lampetia.security.module.SecurityModule

/**
 * @author rhelal
 */
object SecurityTestModule extends SecurityModule {

  Runtime.getRuntime.addShutdownHook(new Thread() {
    override def run(): Unit = {
      configuration.logger.info("This test configuration should be shutting down now ...")
      configuration.shutdown()
    }
  })

}

Add findMembers function to GroupService and its test, And add the GroupRoute and SecurityRoute tests


object Solution {

  def ways(m: Int, n: Int) = {
    val p = 1e9.toInt + 7 // this is a prime
    // the ways of matrix tracing is Combinations(m + n - 1, m - 1)
    val N = m + n - 2
    val K = m - 1
    val numerator = (N to N - K + 1 by -1).foldLeft(1L) { _ * _ % p }
    val denominator = (1 to K).foldLeft(1L) { _ * _ % p }
    // we can get inverse mod p, since p is a prime
    numerator * BigInt(denominator).modPow(p - 2, p) % p
  }
  def main(args: Array[String]) {
    for (_ <- 1 to readInt) {
      val Array(m, n) = readLine.split(" ").map(_.toInt)
      println(ways(m, n))
    }
  }
}

Add solution to problem Matrix Tracing problem. If we look the pattern carefully, the ways of tracking an m * n word is nCr(m + n - 2, m - 1)


import org.pegdown.PegDownProcessor
import org.scalatest.FunSuite

/**
  * Created by blacky on 17/05/18.
  */
class PegDownSuite extends FunSuite {

  test("pegdown") {
    val processer = new PegDownProcessor()
    val source =
      """
        |# h1
        |## h2
        |### h3
      """.stripMargin
    val expect =
      """
h1

        |
h2

        |
h3
""".stripMargin
    assert(processer.markdownToHtml(source) == expect)
  }
}

Add a test about PegDown

Add a test about PegDown


package com.programmaticallyspeaking.ncd.nashorn.java9

import com.programmaticallyspeaking.ncd.host.SimpleValue
import com.programmaticallyspeaking.ncd.nashorn.RealMarshallerTestFixture
import org.scalatest.Inside

class TemplateLiteralTest extends RealMarshallerTestFixture with RunningJava9 with Inside {
  "Marshalling of template literals" - {

    "works for a plain one" in {
      evaluateExpression("`foo`") { (_, actual) =>
        actual should be (SimpleValue("foo"))
      }
    }

    "works for one with an embedded string" in {
      evaluateExpression("(function (x) { return `foo${x}`; })(42)") { (_, actual) =>
        actual should be (SimpleValue("foo42"))
      }
    }

    "works for a multiline lstring" in {
      evaluateExpression("`foo\nbar`") { (_, actual) =>
        actual should be (SimpleValue("foo\nbar"))
      }
    }
  }
}

Add a few tests to verify that marsalling of ES6 template literals work


package filodb.core.reprojector

import com.typesafe.config.ConfigFactory
import org.velvia.filo.TupleRowReader

import filodb.core.KeyRange
import filodb.core.metadata.{Column, Dataset}
import filodb.core.columnstore.SegmentSpec

import org.scalatest.{FunSpec, Matchers, BeforeAndAfter}

class MemTableMemoryTest extends FunSpec with Matchers with BeforeAndAfter {
  import SegmentSpec._
  import MemTable._

  val keyRange = KeyRange("dataset", Dataset.DefaultPartitionKey, 0L, 10000L)
  val newSetting = "memtable.max-rows-per-table = 200000"
  val config = ConfigFactory.parseString(newSetting).withFallback(
                 ConfigFactory.load("application_test.conf"))
  val mTable: MemTable = new MapDBMemTable(config)
  import scala.concurrent.ExecutionContext.Implicits.global

  before {
    mTable.clearAllData()
  }

  val schemaWithPartCol = schema ++ Seq(
    Column("league", "dataset", 0, Column.ColumnType.StringColumn)
  )

  val numRows = 100000

  val lotsOfNames = (0 until (numRows/6)).toIterator.flatMap { partNum =>
    names.map { t => (t._1, t._2, t._3, Some(partNum.toString)) }.toIterator
  }

  private def printDetailedMemUsage() {
    val mxBean = java.lang.management.ManagementFactory.getMemoryMXBean
    println(mxBean.getNonHeapMemoryUsage)
  }

  // To really see amount of memory used, might have to uncomment the thread sleep and use VisualVM,
  // because tests are forked.
  it("should add tons of rows without overflowing memory and taking too long") {
    val start = System.currentTimeMillis
    val startFreeMem = sys.runtime.freeMemory
    println(s"Start: free memory = $startFreeMem")
    printDetailedMemUsage()

    val setupResp = mTable.setupIngestion(dataset, schema, 0)
    setupResp should equal (SetupDone)

    var numRows = 0
    lotsOfNames.map(TupleRowReader).grouped(2000).foreach { rows =>
      mTable.ingestRows("dataset", 0, rows.toSeq) should equal (Ingested)
      numRows += rows.length
      // println(s"Ingested $numRows rows")
      // Thread sleep 1000
    }

    val elapsed = System.currentTimeMillis - start
    val endFreeMem = sys.runtime.freeMemory
    println(s"End: free memory = $endFreeMem   elapsed = ${elapsed} ms")
    printDetailedMemUsage()
  }
}

Add test for loading lots of records into MemTable


package unfiltered.plan

import unfiltered.request._
import unfiltered.response._

/** To ecapsulate a filter in a class definition */
trait Plan {
  def filter: PartialFunction[HttpRequest[_ <: AnyRef], ResponseFunction]
}

/** To define a filter class with an independent function */
class Planify(val filter: PartialFunction[HttpRequest[_ <: AnyRef], ResponseFunction]) extends Plan

/** To create a filter instance with an independent function */
object Planify {
  def apply(filter: PartialFunction[HttpRequest[_ <: AnyRef], ResponseFunction]) = new Planify(filter)
}

Correct naming convention for plan.scala. IDEA gets me every time :)


package uk.ac.wellcome.platform.reindex.reindex_worker.models

/** This is the request received from a caller who wants to trigger a reindexer.
  *
  * @param id The name of the reindex to run, e.g. "miro--reporting" or "sierra--catalogue".
  * @param parameters Parameters for the reindex.
  */
case class ReindexRequest(
  id: String,
  parameters: ReindexParameters
)

Add a case class for passing ID/parameters to the reindexer


package ch.usi.inf.l3.sana.arrooj.typechecker

import ch.usi.inf.l3.sana
import sana.arrooj
import sana.arrayj
import sana.ooj
import sana.brokenj
import sana.primj
import sana.calcj
import sana.tiny

import sana.dsl._
import sana.core._
import ooj.typechecker.{N, FlowCorrectnessCheckerComponent}
import arrayj.ast.{ArrayCreationApi, ArrayAccessApi,
                   ArrayTypeUseApi, ArrayInitializerApi}

/*
ArrayCreation: DONE
ArrayAccess: DONE
ArrayInitializer: DONE
ArrayTypeUse: DONE
*/

@component(tree, env)
trait ArrayCreationFlowCorrectnessCheckerComponent extends
  FlowCorrectnessCheckerComponent {
  (creation: ArrayCreationApi) => {
    check((creation.array, env))
    creation.size.foreach(s => check((s, env)))
    N
  }
}

@component(tree, env)
trait ArrayAccessFlowCorrectnessCheckerComponent extends
  FlowCorrectnessCheckerComponent {
  (access: ArrayAccessApi) => {
    check((access.array, env))
    check((access.index, env))
    N
  }
}

@component(tree, env)
trait ArrayInitializerFlowCorrectnessCheckerComponent extends
  FlowCorrectnessCheckerComponent {
  (init: ArrayInitializerApi) => {
    init.elements.foreach(elem => check((elem, env)))
    N
  }
}

@component(tree, env)
trait ArrayTypeUseFlowCorrectnessCheckerComponent extends
  FlowCorrectnessCheckerComponent {
  (tuse: ArrayTypeUseApi) => {
    check((tuse.tpt, env))
    N
  }
}

Implement flow analysis for arrays

Implement flow analysis for arrays


package com.example.http4s.blaze

import org.http4s._
import org.http4s.dsl._
import org.http4s.client._
import org.http4s.client.blaze.{defaultClient => client}

object ClientPostExample extends App {
  val req = POST(uri("https://duckduckgo.com/"), UrlForm("q" -> "http4s"))
  val responseBody = client(req).as[String]
  println(responseBody.run)
}

Make implicit UrlForm encoder, improve syntax, show example.

This eases the common case of a POST request with a
x-www-form-urlencoded body.


package utils.json

import shapeless.ToInt
import spray.json._

import scala.reflect.ClassTag
import scala.util.Try

object Implicits {

  final implicit class RichJSValue(val json: JsValue) extends AnyVal {

    /**
     * Get's a field from a dot-separated path (eg. `root.node.leaf`)
     *
     * @param path The dot separated path
     * @tparam JV The type of JSON value to return
     * @return The JSON field value (wrapped in an option)
     */
    def getPath[JV <: JsValue: ClassTag](path: String): Option[JV] =
      getPath(path.split("\\.").toList)

    def getPath[JV <: JsValue: ClassTag](pathElements: List[String]): Option[JV] =
      (pathElements, json) match {
        case (Nil, jv: JV) => Some(jv)
        case (elem :: rem, JsObject(fields)) => fields.get(elem).flatMap(_.getPath(rem))
        case (ToInt(i) :: rem, JsArray(elems)) => elems.drop(i).headOption.flatMap(_.getPath(rem))
        case _ => None
      }

    /**
     * Unwraps a JSON value. If the given value is a JSON string, number or
     * boolean, a `String`, `BigDecimal` or `Boolean` is returned
     * respectively. If the given value is a JSON array or object, a `List[Any]`
     * or `Map[String, Any]` is returned respectively, where each of the values
     * is recursively unwrapped. If the given value is a JSON null, `null` is
     * returned.
     * @return the unwrapped JSON value.
     */
    def toValue: Any = json match {
      case JsString(str) => str
      case JsNumber(num) => num
      case JsObject(map) => map.mapValues(_.toValue).map(identity)
      case JsArray(elems) => elems.map(_.toValue)
      case JsBoolean(bool) => bool
      case JsNull => null
    }
  }

}

object ToInt {
  def unapply(str: String) = Try(str.toInt).toOption
}

Add a RichJSValue utility class to deal with json objects

Thanks Rui


package com.negrisoli.algorithms.implementation

object CaesarCipher {

    def main(args: Array[String]) {
        val sc = new java.util.Scanner (System.in);
        var n = sc.nextInt();
        var s = sc.next();
        var k = sc.nextInt();

        s.map(_.toInt).map(x => cripto(x, k)).map(x => print(x.toChar))
    }

    def cripto(c:Int, k:Int):Int  = {
        if (isUper(c)) (c-65 + k) % 26 + 65
        else if (isDown(c)) (c-97 + k)  % 26 + 97
        else c
    }

    def isUper(c:Int) = c >= 65 && c <= 90
    def isDown(c:Int) = c >= 97 && c <= 122

}

Add a Caesar cipher solution.

Add a Caesar cipher solution.


package ch.usi.inf.l3.sana.dynj.errors

import ch.usi.inf.l3.sana
import sana.tiny.errors.ErrorCode
import sana.robustj

trait ErrorCodes extends robustj.errors.ErrorCodes {
  case object DYNAMIC_TYPE_CHECK_NONE_REF_TYPE extends ErrorCode {
    val message: String =
      "Dynamic type checks can only be performed on reference types"
  }
}

object ErrorCodes extends ErrorCodes

Add error code for bad isinstanceof

Add error code for bad isinstanceof


package ch.usi.inf.l3.sana.guod.symbols

import ch.usi.inf.l3.sana
import sana.tiny.symbols.Symbol
import sana.tiny.types.Type
import sana.tiny.names.Name
import sana.calcj.types.IntType
import sana.calcj.symbols._
import sana.primj.symbols.{VariableSymbol, VoidSymbol}
import sana.ooj.symbols.{ClassSymbol, PackageSymbol, CompilationUnitSymbol}
import sana.ooj.modifiers._
import sana.robustj.names.StdNames

import sana.ooj.symbols.PackageSymbol

trait SymbolUtils extends sana.modulej.symbols.SymbolUtils {
  def toFullyQualifiedTypeName(symbol: Option[Symbol]): String = symbol match {
    case Some(vsym: VariableSymbol)         =>
      toFullyQualifiedTypeName(vsym.typeSymbol)
    case Some(csym: ClassSymbol)            =>
      toFullyQualifiedTypeName(csym.owner) + csym.name
    case Some(pkg: PackageSymbol)           =>
      if(pkg.name != StdNames.DEFAULT_PACKAGE_NAME)
        toFullyQualifiedTypeName(pkg.owner) + pkg.name + "."
      else ""
    case Some(cunit: CompilationUnitSymbol) =>
      toFullyQualifiedTypeName(cunit.owner)
    case Some(sym: BooleanSymbol)           =>
      sym.name.asString
    case Some(sym: ByteSymbol)              =>
      sym.name.asString
    case Some(sym: CharSymbol)              =>
      sym.name.asString
    case Some(sym: ShortSymbol)             =>
      sym.name.asString
    case Some(sym: IntSymbol)               =>
      sym.name.asString
    case Some(sym: LongSymbol)              =>
      sym.name.asString
    case Some(sym: FloatSymbol)             =>
      sym.name.asString
    case Some(sym: DoubleSymbol)            =>
      sym.name.asString
    case Some(sym: VoidSymbol)              =>
      sym.name.asString
    case _                                  =>
      ""
  }
}

object SymbolUtils extends SymbolUtils

Add toFullyQualifiedTypeName method to symbol utils


package object icfpc2013 {

  implicit class PimpedString(val str: String) extends AnyVal {
    def asBvProgram: Program = BvParser(str).get
  }

  implicit class PimpedProgram(val prog: Program) extends AnyVal {
    def run(input: Long): Long = BvCompiler(prog)(input)
  }
}

Add power pimps to aid in console testing.