Skip to content

Instantly share code, notes, and snippets.

View Krasnyanskiy's full-sized avatar

Sasha Krasnyanskiy

  • Cupertino, California
View GitHub Profile
@Krasnyanskiy
Krasnyanskiy / FoldLeftTest.scala
Last active July 9, 2016 21:48
-scala: foldl (2 versions)
val seq = Seq(1, 2, 3, 4, 5)
val res0 = seq.foldLeft(0)(_ + _)
val res1 = seq./:(0)(_ + _) // (seq /:) (0)(_ + _)
assert(res0 == res1)
@Krasnyanskiy
Krasnyanskiy / Booking.scala
Last active June 28, 2016 09:05
-scala: snippet
bookingDao.findById(bookingId).flatMap {
case \/-(booking) if booking.professionalId != userProfileId =>
-\/(Unauthorized).future
case success@ \/-(booking) if pendingStatuses.contains(booking.stateInfo.status) =>
success.future
case \/-(booking) =>
makeBookingData(userContext.deviceType, Booking.clientId)(booking).flatMapDisjunction { bookingData => -\/(Conflict(bookingData)) }
case s =>
s.future
}
@Krasnyanskiy
Krasnyanskiy / RangeTest.scala
Created June 25, 2016 19:54
-scala: time range
/**
* @author Alexander Krasniansky
*/
object RangeTest extends App {
import com.github.nscala_time.time.Imports._
case class Event(
name : String,
startTime: DateTime,
@Krasnyanskiy
Krasnyanskiy / Main.scala
Created June 16, 2016 18:13
-scala: akka test
import akka.actor._
object Main extends App {
val system = ActorSystem("mySystem")
val input = system.actorOf(Props[Input])
val output = system.actorOf(Props[Output])
val state = system.actorOf(Props(classOf[State], input, output))
class Output extends Actor {
@Krasnyanskiy
Krasnyanskiy / CodeCollisionService.scala
Last active June 14, 2016 19:01
-scala: type alias
sealed trait CodeCollisionService {
type CodeCollisionFunc = (String, Int) => Seq[Double]
def calculateCollision: CodeCollisionFunc
}
object CodeCollisionService extends CodeCollisionService {
import il.finkel.spark.CodeCollisionService.{
CodeCollisionFunc => <%>
}
@Krasnyanskiy
Krasnyanskiy / App.java
Created June 14, 2016 16:53
-java: generics
public <A extends B, B> void foo(A a, B b) {
b = a;
}
@Krasnyanskiy
Krasnyanskiy / findPrime.js
Created June 13, 2016 18:20
-js: find prime
function findPrime(array) {
var sieve = [], i, j, max = 1000000;
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
var parsed = ('' + i).split('').map(function (e) {
return parseInt(e, 10);
});
if (JSON.stringify(parsed) === JSON.stringify(array)) {
return i;
}
@Krasnyanskiy
Krasnyanskiy / TimestampMap.java
Last active June 11, 2016 22:51
-java: my questions to Java interns (Design + Correctness)
public class TimestampMap<V> extends HashMap<Timestamp, V> {
public static <T> TimestampMap<T> of(T... ts) {
return new TimestampMap<T>() {
@Override
public T put(Timestamp k, T v) {
for (T t : ts) {
this.put(now(), t); // potential SO (!) + useless initialization | will super.* fix the SO error?
}
return null;
@Krasnyanskiy
Krasnyanskiy / A.scala
Created June 11, 2016 19:43
-scala: aux pattern
class User
object User {
type Aux[I, A] = User {
type IdType = I
type Address = A
}
}
@Krasnyanskiy
Krasnyanskiy / App.java
Last active June 5, 2016 12:14
-java: switch (default)
public int foo(int v) {
int counter = 0;
switch (v) {
default: counter += 1;
case 1: counter += 2;
case 2: counter += 3;
}
return counter;
}