Skip to content

Instantly share code, notes, and snippets.

View radixSort.scala
def radixSort(arr: Seq[Int]): Seq[Int] = {
def fn(state: Seq[Int], d: Int): Seq[Int] = {
val ten_power_d = Iterator.fill(d)(10).product
val buckets = state.groupBy(_ / ten_power_d % 10).toMap
buckets.keys.toSeq.sorted.foldLeft(Seq.empty[Int]) { case (acc, i) =>
acc ++ buckets(i)
}
}
val digits = Iterator.iterate(arr) {
@alaz
alaz / readfile.js.es6
Last active Jan 7, 2017 — forked from depeele/gist:66656a73bf53773ae852f6613ae05876
HackerRank Javascript stdin using an ES6 generator and larger buffer
View readfile.js.es6
'use strict';
function* readLine(stream) {
const EOL = require('os').EOL;
const Fs = require('fs');
const buf = new Buffer(1024);
stream.resume();
stream.setEncoding('ascii');
@alaz
alaz / blocking.scala
Last active Apr 4, 2016 — forked from jessitron/gist:8777503
The magic of blocking { ... } in Scala's global ExecutionContext: it leads to the creation of more threads, so the CPUs don't get bored
View blocking.scala
val des = scala.concurrent.ExecutionContext.global
import scala.concurrent._
import duration._
def ct = Thread.currentThread.getName
val n = Runtime.getRuntime.availableProcessors
def hogThread(sec:Int) = future {
@alaz
alaz / 200x150.html
Created Sep 4, 2012
Osinka banner 200x150
View 200x150.html
<a href="http://www.osinka.ru/?utm_source=people&utm_medium=button&utm_content=osinka_banner_200x150_1&utm_campaign=110125"
title="Осинка.ру - все о моде, шитье и рукоделии"><img
src="http://www.osinka.ru/info/banners/osinka_banner_200x150_1.png"
width="200"
height="150"
border="0"></a>
@alaz
alaz / Comments.scala
Created Jan 20, 2012
Subset: "Blog post - comments" example
View Comments.scala
package com.osinka.subset.examples
/**
* Data model from this MongoDB doc page:
* http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
*/
// Use :paste mode
import com.mongodb._
import com.osinka.subset._
@alaz
alaz / 01_init.scala
Created Jan 20, 2012
REPLable Subset play
View 01_init.scala
import scala.io._
import collection.JavaConversions._
import com.mongodb._
import com.osinka.subset._
import SmartValues._
// we want to store it in MongoDB. ok..
val mongo = new Mongo("192.168.0.164")
val db = mongo getDB "test"
val coll = db getCollection "timeline"
@alaz
alaz / LiftResourceBundleControl.scala
Created Nov 23, 2011
ResourceBundle.Control implementation based on Liftweb resources XML format
View LiftResourceBundleControl.scala
package com.osinka.i18n
import java.util.{Locale,ResourceBundle}
import collection.JavaConversions._
import net.liftweb.common.Box._
import net.liftweb.util.{BundleBuilder,PCDataXmlParser}
/**
* ResourceBundle.Control implementation for Liftweb resources
*
View PostStart.scala
trait PostStart { actor: Actor =>
def postStart: Unit
override def preStart {
super.preStart
actor.become {
case "PostStart" => try { postStart } finally { actor.unbecome }
}
actor.self ! "PostStart"
}
View ConformMatcher.scala
import reflect.{Manifest, ClassManifest}
import org.scalatest.matchers.{Matcher,MatchResult}
trait CustomMatchers {
/**
* Based on http://daily-scala.blogspot.com/2010/01/overcoming-type-erasure-in-matching-1.html
*/
class ConformMatcher[A](implicit m: Manifest[A]) extends Matcher[AnyRef] {
override def apply(obj: AnyRef) = {
def deepConformance[B,C](desired: Manifest[B], actual: Manifest[C]): Boolean = {
View optionalEmbeddedField.scala
class OptionalEmbeddedField[V](override val mongoFieldName: String, val g: T => Option[V], val p: Option[(T,Option[V]) => Unit])
extends MongoScalar[V] with EmbeddedContent[V] with FieldModifyOp[V] with Optional[V] {
self: MongoField[V] with ObjectIn[V, QueryType] =>
override val rep = parent.Represented.byOption(g, p)
override def canEqual(other: Any): Boolean = other.isInstanceOf[OptionalEmbeddedField[_]]
}