Skip to content

Instantly share code, notes, and snippets.

View globulon's full-sized avatar
馃挱
I may be slow to respond.

patterngazer globulon

馃挱
I may be slow to respond.
View GitHub Profile
@globulon
globulon / Euler52-Clojure
Created January 31, 2012 17:05
Port in Clojure of missingfactor smart resolution of euler problem 52
(defn problem-fifty-two[]
(time (take 1 (drop-while #(not (= 1 (count (set (map (comp sort str (partial * %)) [2 3 4 5 6]))))) (iterate inc 1)))))
@globulon
globulon / Applicative.scala
Created February 5, 2012 09:40
First shot for applicative functor example
package com.promindis.patterns
trait ApplicativeHelper[F[_]] extends MonadHelper[F] {
def apply[U](data: U): F[U]
def apply[U](input: F[U]): Applicative[U, F]
}
trait Applicative[+T, F[_]] extends Monad[T, F] {
@globulon
globulon / UseApplicativeFunctor.scala
Created February 5, 2012 09:43
Usage of the first shot of applicative functors
package com.promindis.user
import com.promindis.patterns.{ApplicativeHelper, Applicative}
object Applicatives {
implicit object ApplicativeList extends ApplicativeHelper[List]{
override def flatten[T](m: List[List[T]]) = m.flatten
override def apply[U](data: U) = List(data)
@globulon
globulon / gist:1804196
Created February 11, 2012 21:04
First experiment in deriving the state monad from the monad definition
//Check whole project https://github.com/globulon/Scala-Patterns-Experiments
object SM {
case class State[T, S](f: (S) => (T,S)) {
def apply(s: S): (T, S) = f(s)
}
implicit def stateToComprehension[T, S](state: State[T,S]) = new {
implicit val functor = stateFunctor[S]()
val monad = stateMonad[S]()
@globulon
globulon / gist:1838926
Created February 15, 2012 20:52
Mandatory definitions for State Monad
implicit def stateFunctor[S]() = new Functor[({type 位[伪] = State[伪,S]})#位] {
def map[T, P >: T, U](source: State[T, S])(f: P => U): State[U, S] = new State[U, S]((s: S) => {
val (value, state) = source(s)
(f(value), state)
})
}
implicit def stateMonad[S]() = new Monad[({type 位[伪] = State[伪,S]})#位]{
@globulon
globulon / Monad and Functor.scala
Created February 15, 2012 20:53
Monad and Functor definitions
trait Functor[M[_]] {
def map[T,P >: T, U](source: M[T])(f: P => U): M[U];
}
trait Monad[M[_]] {
def apply[T](data: T): M[T]
def flatten[T](m: M[M[T]]): M[T]
@globulon
globulon / State definition.hs
Created February 15, 2012 21:00
State definition in Learn You a Haskell
s -> (a, s)
newtype State s a = State { runState :: s -> (a, s) }
@globulon
globulon / State.scala
Created February 15, 2012 21:06
State definition
case class State[+T, S](f: (S) => (T,S)) {
def apply(s: S): (T, S) = f(s)
}
@globulon
globulon / State Monad Kestrel Combinator.scala
Created February 15, 2012 21:48
State Monad Kestrel Combinator
implicit def stateToComprehension[T, S](state: State[T,S]) = new {
implicit val functor = stateFunctor[S]()
val monad = stateMonad[S]()
def map[U](f: T => U) = functor.map(state)(f)
def flatMap[U](f: T => State[U, S]) = monad.flatMap(state)(f)
}
@globulon
globulon / State Monad Usage.scala
Created February 15, 2012 21:50
State Monad usage
object Stack {
def push[Int](x: Int) = new State((state: List[Int]) => ((), x :: state))
def pop = new State((state: List[Int]) =>
state match {
case x :: xs => (Some(x), xs)
case _ => (None, state)
}
)