Skip to content

Instantly share code, notes, and snippets.

package pl.japila.scalania.s99
object S99_P02 {
def penultimate[T](ts: Seq[T]): T = ts match {
case a :: b :: Nil => Some(a)
case a:: tail => None
case _ => penultimate(ts.tail)
}
}
package pl.japila.scalania.s99
object S99_P03 {
def nth[T](k: Int, ts: List[T]): Option[T] = {
if (k > 0) ts match {
case a :: tail => nth(k - 1, tail)
case Nil => None
}
else if (k == 0) Some(ts.head)
else None
@kastoestoramadus
kastoestoramadus / pwd_jdk7
Created July 16, 2015 08:12
pwd with jdk7
import java.nio.file.Paths
val pwd = Paths.get(".").toAbsolutePath().normalize().toString()
@kastoestoramadus
kastoestoramadus / fileReporter
Created July 16, 2015 08:18
file logger com.codahale.metrics fix akka
// cancel sheduler and cloase file as one cancelable
private def fileLogger(): Cancellable with Object {def cancel(): Boolean; def isCancelled: Boolean} = {
val w = new BufferedWriter(new FileWriter(
s"../metrics_${TcpUtil.firstPartFromIp}_${numberOfClients}_${LocalTime.now()}.dat"))
val scheduled = context.system.scheduler.schedule(Config.STAT_PROBE_DURATION, Config.STAT_PROBE_DURATION) {
w.write( LineLog.toFileString(LocalTime.now(),
beats.getCount, fails.getCount, closings.getCount, receives.getCount, reconnects.getCount)+"\n")
w.flush()
}
// generate cyclic sending mail wiht rotation on chosen data
private def generateMailsSending(sentmails: Meter): Cancellable = {
import sys.process._
val getNextHeaderSet: () => ClientHeaders = {
def b(): Stream[ClientHeaders] = headers.toStream #::: b
var cyclicIterator: Stream[ClientHeaders] = b()
() => {
val tail = cyclicIterator.tail
@kastoestoramadus
kastoestoramadus / codiDemo.scala
Created October 28, 2015 05:53
few version of solutions with one stuff to discuiss
import scala.annotation.tailrec
import scala.collection.JavaConversions._
object Solution {
def solutionShortest(A: Array[Int]): Int = {
// fake from https://github.com/khatwaniNikhil/codility-scala/blob/master/src/com/codility/lesson1/TapeEquilibrium.scala
val sumAccum = A.scanLeft(0L)(_ + _).tail
(1 to A.size - 1).map(p =>
scala.math.abs(
@kastoestoramadus
kastoestoramadus / notesSendMail
Last active June 28, 2017 07:58
batch sendmail scala and bash
import sys.process._
import java.time.LocalDateTime
object SendMailMain {
def main(args: Array[String]) {
val start = LocalDateTime.now()
(1 to 1000).par.foreach { i =>
print(s"$i ")
val out = s"echo anything $i" #| s"sendmail 123456789@[192.168.2.118]" !;
Replace all numbers with that number of '?':
java like:
import java.util.regex.Pattern
val NKasString = "?"
val p = Pattern.compile("[0-9]+")
val m = p.matcher("\"a2a12a2\"")
val sb = new StringBuffer()
while (m.find()) {
byobu new -d -s $$ && tmux send -t $$ 'sbt -mem 1600 run' ENTER
@kastoestoramadus
kastoestoramadus / ImplicitTest.scala
Last active June 28, 2017 07:46
Scala implicitly. Where to override it?
package snippet
/**
* Created by walidus on 27/06/17. How to override the implicit val with c?
*/
object ImplicitTest extends App{
implicit val overrider = MyClass.c // works
Util.print()
}
object Util {