This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]})#位]{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s -> (a, s) | |
| newtype State s a = State { runState :: s -> (a, s) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class State[+T, S](f: (S) => (T,S)) { | |
| def apply(s: S): (T, S) = f(s) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| ) |
OlderNewer