Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
@folone
folone / gist:3911467
Created October 18, 2012 12:27
Writer exercise
import scalaz._, Scalaz._
case class Writer[W: Monoid, +A](run: (A, W)) {
def map[B](f: A ⇒ B): Writer[W, B] =
Writer {
val (a, w) = run
(f(a), w)
}
def flatMap[B](f: A ⇒ Writer[W, B]): Writer[W, B] =
@folone
folone / gist:3911559
Created October 18, 2012 12:43
Reader exercise (inspired by gist from @tonymorris: https://gist.github.com/3884189)
case class Reader[T, +A](run: T ⇒ A) {
def map[B](f: A ⇒ B): Reader[T, B] =
Reader((r: T) ⇒ f(run(r)))
def flatMap[B](f: A ⇒ Reader[T, B]): Reader[T, B] =
Reader((r: T) ⇒ f(run(r)).run(r))
def &&&[B](x: Reader[T, B]): Reader[T, (A, B)] =
for {
a ← this
@folone
folone / NewYear.hs
Last active December 10, 2015 10:28
import Control.Applicative
data NewYear = NewYear Int
deriving (Eq, Show)
data Happy a = Happy a
deriving (Eq, Show)
instance Functor Happy where
fmap f (Happy a) = Happy $ f a
import scalaz._, Scalaz._
case class Happy[A](a: A)
case class NewYear(year: Int)
implicit val happyInstance = new Applicative[Happy] {
def point[A](a: ⇒ A) = Happy(a)
def ap[A, B](fa: ⇒ Happy[A])(f: ⇒ Happy[A => B]) =
f match {
case Happy(f) ⇒ fa match {
% ghci
GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let n = map show [1..10]
Prelude> let c = \x y -> concat ["(", x, "+", y, ")"]
Prelude> foldl c "0" n
"((((((((((0+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)"
Prelude> foldr c "0" n
shapeless [0849b6f...] % sbt
[info] Loading project definition from /home/folone/workspace/shapeless/project
[info] Set current project to shapeless (in build file:/home/folone/workspace/shapeless/)
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 2 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
@folone
folone / gist:4945168
Last active April 27, 2017 13:10
A taste of dependent types in scala with shapeless.
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
@folone
folone / gist:4946543
Last active December 13, 2015 17:18
Hanoi towers at compile time become very easy with dependent types. https://gist.github.com/travisbrown/3772462
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless.SingletonTypes._
@folone
folone / 0.prelude.md
Last active December 13, 2015 22:59
@folone
folone / adjunction.scala
Last active December 15, 2015 07:18
- Things, that can be described as Adjunctions - Things, that can be described as Cofree - Things, that can be described as Free monads, inspired by http://yow.eventer.com/yow-2012-1012/lambda-the-ultimate-dependency-injection-framework-by-runar-bjarnason-1277
type State[E] = Adjunction[({type λ[α] = Writer[E, α]})#λ, ({type λ[α] = Reader[E, α]})#λ]
type Store[E] = Adjunction[({type λ[α] = Reader[E, α]})#λ, ({type λ[α] = Writer[E, α]})#λ]
type Cont = Adjunction[Function0, Function0] // Function0 -| Function0