Skip to content

Instantly share code, notes, and snippets.

View TheDIM47's full-sized avatar

Dmitry Koudryavtsev TheDIM47

View GitHub Profile
@TheDIM47
TheDIM47 / scala-async-console-io.scala
Last active August 29, 2015 14:22
Simple Scala console app which handles user's input asynchronously
package user.demo
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
/**
* Scala console app which handles user's input asynchronously.
* Each user answer should be set to default value by timeout.
*/
@TheDIM47
TheDIM47 / UnshortTest.scala
Created August 22, 2015 16:56
Unshort URL with Scala and Dispatch
import java.net.URI
import org.scalatest._
import scala.concurrent.duration._
class UnshortTest extends FlatSpec with Matchers {
def unshort(u: String, w: Duration): Option[URI] = {
import dispatch._, Defaults._
import scala.concurrent.Await
@TheDIM47
TheDIM47 / FileFinderAkka.scala
Created September 2, 2015 18:55
Recursive file finder with Scala and Akka
package fastlookup
import java.io.File
import akka.actor._
object FileFinder {
case class Scan(val dir: File, val name: String)
@TheDIM47
TheDIM47 / KdTree.java
Created October 18, 2015 21:14
Coursera Algorithms I
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.SET;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private static class Node {
private Point2D p; // the point
private RectHV rect; // the axis-aligned rectangle corresponding to this node
@TheDIM47
TheDIM47 / MissingElement.java
Last active October 27, 2015 10:10
Find missing Integer in array with Java
public class MissingElement {
public int solution(int[] A) {
if (A == null) return 1;
else if (A.length == 0) return 1;
else {
java.util.Arrays.sort(A);
return evaluate(A, 0, A.length - 1);
}
}
@TheDIM47
TheDIM47 / ForkJoinArrayEvaluator.java
Created November 19, 2015 14:17
ForkJoin sample
package mireyn;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction;
public class ForkJoinArrayEvaluator {
private static final int CPU = Runtime.getRuntime().availableProcessors();
public static int THRESHOLD = CPU * 16;
@TheDIM47
TheDIM47 / case_class_localization.scala
Created September 19, 2016 18:37
Case class localization sample
sealed trait Localizator {
def apply(s: State): String
}
sealed trait State {
def name(implicit loc: Localizator): String = loc.apply(this)
}
sealed trait OkState extends State
sealed trait ErrorState extends State
@TheDIM47
TheDIM47 / XmlTvParser.scala
Last active March 18, 2018 16:02
Simple scala-xml parser for XmlTv format
import scala.xml._
import java.util.Date
import java.lang.ThreadLocal
import java.text.SimpleDateFormat
import scala.collection.immutable._
/**
* Simple XmlTv format parser
* http://wiki.xmltv.org/index.php/XMLTVFormat
*/
import java.net.InetAddress
import java.nio.ByteBuffer
object InetUtils {
private[this] val Radix = 16
// Fast Hex converters
private[this] val HexChars: Array[Char] = "0123456789ABCDEF".toCharArray
/**
* Join list of lists by order of elements
* val a = List(3, 5, 6)
* val b = List(1, 2, 7, 8)
* val expected = List(3, 1, 2, 5, 6, 7, 8)
*
* @param lists list of lists of elements
* @return list of elements
*/
def join[A](lists: List[List[A]]): List[A] = {