Skip to content

Instantly share code, notes, and snippets.

View robinp's full-sized avatar

Robin Palotai robinp

  • Budapest, Hungary
View GitHub Profile
@robinp
robinp / PostprocessingBakery2.java
Created July 27, 2012 08:57
complex bakery model using injection
interface PostprocessBreadStrategy {
Bread postprocess(Bread bread);
}
class PostprocessingBakery2 {
public Bakery2(BakeBreadStrategy bakeStrategy, PostprocessingBreadStrategy postprocessStrategy) {
// ...
}
@robinp
robinp / StrategyComposition.java
Created July 28, 2012 22:20
Naive way of composing strategies
interface ProduceStrategy {
Bread produce(Ingredients ingredients);
}
interface PostprocessStrategy {
Bread postprocess(Bread bread);
}
class ChainedPostprocessStrategy implements PosprocessStrategy {
@robinp
robinp / FunctionalBakery.java
Created July 28, 2012 22:42
Strategies are functions
class FunctionalBakery {
public Bakery2(F<Ingredients, Bread> bakeStrategy,
F<Bread, Bread> postprocessStrategy) {
// ...
}
public void produceBread() {
Ingredients ingredients = prepareIngredients();
Bread bread = bakeStrategy.f(ingredients);
@robinp
robinp / ComposingFunctions.java
Created July 28, 2012 22:50
Composing functions for baking fine stuff
class Phases {
public static final F<Bread, Bread> SLICE = new F<Bread, Bread>() { ... };
public static final F<Bread, Bread> BOX = new F<Bread, Bread>() { ... };
public static final F<Bread, Bread> SLICE_AND_BOX = SLICE.andThen(BOX);
}
class QuickAndDirty {
public static F<Ingredients, Bread> MAKE_BIO_BREAD = ...;
public static F<Ingredients, Bread> MAKE_BIO_BOXED_BREAD =
MAKE_BIO_BREAD.andThen(BOX);
}
@robinp
robinp / resolutions.scala
Created August 16, 2012 22:58
Chose element with best matching resolution
package com.treetide.utils.portable.gfx
import scalaz.{NonEmptyList, Order}
trait Resolutions {
type Resolution = (Int, Int)
trait HasResolution[F] {
def resolution(f: F): Resolution
@robinp
robinp / test.scala
Created August 25, 2012 16:32
MonadT ws liftM
type EitherThrowableT[F[+_], +A] = EitherT[F, Throwable, A]
type EitherTPromise[+A] = EitherT[Promise, Throwable, A]
def fetchTidied(fromUrl: String): EitherTPromise[JsoupDocument] = ...
def pageCount(jobs: JsoupDocument): Throwable \/ Int = ...
val message = for {
jobsPage <- fetchTidied(careersJobs)
@robinp
robinp / Zipper.scala
Created August 26, 2012 11:06
n-ary tree zipper
sealed trait TreeData
// ...
// could be made generic in data
case class Node(data: TreeData, children: List[Node])
sealed trait ZipCtx
case object Top extends ZipCtx
case class At(revLeft: List[Node], right: List[Node], ctx: ZipCtx, updata: TreeData) extends ZipCtx
@robinp
robinp / StateTRunner.scala
Created September 8, 2012 12:38
Runs a StateT while some state condition is met
object stateTRunner {
def runWhileUsing[F[+_], S, A, B](st: StateT[F, S, A])(init: S, p: S => Boolean, f: (S, A) => B)(implicit F: Monad[F]): F[List[B]] = {
def runWhile0(st: StateT[F, S, A], init: S, accum: List[B]): F[List[B]] = {
if (!p(init)) F.point(accum.reverse)
else F.bind(st.run(init)) {
case (s, a) => runWhile0(st, s, f(s, a) :: accum)
}
}
runWhile0(st, init, Nil)
@robinp
robinp / RunningMerge.scala
Created September 12, 2012 08:47
scala running merge of streams
object merging {
// Note: Tail-recursive stream functions should always be defined on objects, not traits.
// see http://stackoverflow.com/questions/12486762/scala-tail-recursive-stream-processor-function-defined-in-trait-holds-reference
/**
* Merges two ordered streams. The elements can be of different
* types, but they need a mapping to a common type C.
*
* The mapped streams must be ordered ascending.