Skip to content

Instantly share code, notes, and snippets.

View iulianu's full-sized avatar

Iulian Dogariu iulianu

View GitHub Profile
import scala.collection.JavaConverters._
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Task1Solution {
def phoneMetric(number: Int): Long = {
val numberStr = number.toString
100000000000L * numberStr.size +
import scala.annotation.tailrec
import scala.collection.JavaConverters._
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Task2Solution {
class TransformationIterator(s: String, removable: String) extends Iterator[String] {
var i = 0
val remLen = removable.length
require 'zlib'
APACHE_LINE_RX = /^([0-9\.]+)\s+\S+\s+\S+\s+\[[^\]]+\]\s+\"[^\"]+\"\s+(\d+)\s+(\d+).*$/
def parse_apache_line(text_line)
if text_line =~ APACHE_LINE_RX
{:ip => $1, :bytes => $3.to_i}
else
nil
end
end
import scala.slick.lifted.CanBeQueryCondition
// optionally filter on a column with a supplied predicate
case class MaybeFilter[X, Y](val query: scala.slick.lifted.Query[X, Y]) {
def filter[T,R:CanBeQueryCondition](data: Option[T])(f: T => X => R) = {
data.map(v => MaybeFilter(query.filter(f(v)))).getOrElse(this)
}
}
// example use case
import java.sql.Date
sealed trait ContextDescription {
def value: String
}
final case class EmailContext(value: String) extends ContextDescription
final case class MobileContext(value: String) extends ContextDescription
// ...
Seq(EmailContext(email), MobileContext(mobile)).
filter(_.value.nonEmpty).
map( context =>
LocalPersonContext(
scala> val kidsNames = List("John", "George", "Victor")
kidsNames: List[String] = List(John, George, Victor)
scala> val beatlesNames = List("John", "Ringo", "Paul", "George")
beatlesNames: List[String] = List(John, Ringo, Paul, George)
scala> kidsNames.intersect(beatlesNames)
res2: List[String] = List(John, George)
// Looks reasonable!