Skip to content

Instantly share code, notes, and snippets.

View sadache's full-sized avatar

Sadek Drobi sadache

View GitHub Profile
@sadache
sadache / gist:8804038
Last active August 29, 2015 13:56
A naive, potentially buggy and largely improvable implementation of Enumerator[Array[Byte]] to InputStream
import java.io.InputStream
import play.api.libs.iteratee.{ Enumerator, Iteratee }
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{ Success, Failure }
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.atomic.AtomicInteger
def toInputStream(chunks: Enumerator[Array[Byte]], bufferSize: Int): InputStream = new InputStream {
@sadache
sadache / gist:d357ced8f6c942bca81a
Last active August 29, 2015 14:02
Faster with much less memory consumption JSON object reader
def objectReader[T1,T2,T3,T4,R](t1: String, t2: String, t3: String, t4: String)(f: (T1,T2,T3,T4) => R)(implicit readsT1:Reads[T1], readsT2:Reads[T2], readsT3:Reads[T3], readsT4:Reads[T4]): Reads[R] = {
def orElse[A](a:A, default: =>A) = if(a!=null) a else default
Reads[R]{
case JsObject(fields) =>
var t1V:JsResult[T1] = null.asInstanceOf[JsResult[T1]]
var t2V:JsResult[T2] = null.asInstanceOf[JsResult[T2]]
@sadache
sadache / default-currying-lifting-idea-in.scala
Created November 20, 2009 05:17
default-currying-lifting-idea-in.scala The idea deserves maybe writing a whole paper about. But this illustrates the basic concept.
object Test {
implicit def lift0[A,B >: Any](a:A):B=>A=_=>a
implicit def lift1[A>: Any,B>: Any,C >: Any](f:A=>B):(C=>A)=>C=>A=>B = h=>c =>f (h(c))
implicit def lift2[A<:Any,B<:Any,C<:Any,D >: scala.Nothing <:scala.Any](f:Function2[A,B,C]):Function3[Function1[D,A],Function1[D,B],D,C] =( h ,g, d)=>f (h(d),g(d))
implicit def curry2[a1 >: scala.Nothing <: scala.Any, a2 >: scala.Nothing <: scala.Any, b >: scala.Nothing <: scala.Any] (f:Function2[a1,a2,b]): scala.Function1[a1, scala.Function1[a2, b]] =Function.curried(f)
implicit def curry3[a1 >: scala.Nothing <: scala.Any, a2 >: scala.Nothing <: scala.Any, a3 >: scala.Nothing <: scala.Any, b >: scala.Nothing <: scala.Any] (f:Function3[a1,a2,a3,b]): scala.Function1[a1, scala.Function1[a2,scala.Function1[a3,b]]] =Function.curried(f)
//implicit def self[A](a:A):A=a
def main(args:Array[String]){
class Program
{
static IDictionary<string,int> zipCodes= new Dictionary<string,int>{
{"Paris",75}
};
static IDictionary<int, int> population = new Dictionary<int, int>{
{75,100}
};
static int? GetInterstingNumber(string cityName){
object Application {
def main(args :Array[String]){
println(Simulator(5,5).execute((Orientations.N,(1,2)),List('G','A','G','A','G','A','G','A','A')))
println(Simulator(5,5).execute((Orientations.E,(3,3)),List('A','A','D','A','A','D','A','D','D','A')))
}
object Orientations {
type Orientation=Char
module Space.Rovers
open System
type Direction= N | E | S | W
let direction= function 'N' ->N |'E'->E |'S'->S |'W'->W
|_-> raise (new ArgumentOutOfRangeException())
let swap (x,y)=(y,x)
let rec neighbours = function N -> (W,E)
namespace TTest
{
using CircularList;
using WithCompus = Char;
//using RobotState = Tuple<IEnumerable<char>, Position>;
class Program
{
static void Main(string[] args)
{
WithCompus meWithCompusToNorth = 'N';
module Main (
) where
import Control.Monad.Either
f :: (Monad m) ⇒ Int → m Int
f 0 = fail "I don't like zeros"
f x = return x
Sandwiches:
* Jay's Cheesesteaks (many seitan flavors)
* Ike's Place (tons of amazing veggie sandwiches)
* Toaster Oven (Potbelly's clone)
Mexican:
* Papalote (their house salsa is the best IN THE WORLD)
* El Buen Sabor (my favorite super veggie burrito)
* Taqueria Cancún (classic mission burrito; open till 2am on fri/sat, 1am other days, I think)
module UrlPatterns
open System.Text.RegularExpressions
let t2 a b=(a,b)
let matchUrl pattern:string->(Map<string,string>* string ) Option=
let regex=(new Regex(pattern))
let names= regex. GetGroupNames()
in fun url -> let m=regex.Match(url)
if not(m.Success) then None
else let groups= m.Groups in
let paramMap=[for i in 0..groups.Count do