Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
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 {
@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
@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 / 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:3911448
Created October 18, 2012 12:21
State exercise
case class State[S, +A](run: S ⇒ (A, S)) {
def map[B](f: A => B): State[S, B] =
State(s ⇒ {
val (a, t) = run(s)
(f(a), t)
})
def flatMap[B](f: A ⇒ State[S, B]): State[S, B] =
State(s ⇒ {
val (a, t) = run(s)
@folone
folone / TReader.scala
Created October 15, 2012 10:57 — forked from tonymorris/TReader.scala
Reader monad in Scala
case class T() // your data type that you want to "implicitly" thread through
case class TReader[+A](run: T ⇒ A) {
def map[B](f: A ⇒ B): TReader[B] =
TReader((r: T) ⇒ f(run(r)))
def flatMap[B](f: A ⇒ TReader[B]): TReader[B] =
TReader((r: T) ⇒ f(run(r)).run(r))
def &&&[B](x: TReader[B]): TReader[(A, B)] =
@folone
folone / haskell.hs
Last active October 10, 2015 11:58
Pointers
class Monad m => Ref r m where
ref :: a -> m (r a)
(*) :: r a -> m a
(*=) :: r a -> a -> m ()
(*$) :: r a -> (a -> a) -> m ()
r *$ f = (r *) >>= (*=) r . f
instance Ref IORef IO where
ref = newIORef
(*) = readIORef
\documentclass{tufte-handout}
%\geometry{showframe}% for debugging purposes -- displays the margins
\usepackage{amsmath}
% Set up the images/graphics package
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}
\graphicspath{{graphics/}}
@folone
folone / hello-ski.hs
Created August 27, 2012 08:16 — forked from shangaslammi/hello-ski.hs
Hello World using Applicative instance for ((->) r) as a computational basis
$ ghci
GHCi, version 7.4.2: 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> :l hello-ski.hs
[1 of 1] Compiling Main ( hello-ski.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello world!
@folone
folone / nodeseq.scala
Created July 31, 2012 14:04
NodeSeq instance for scalacheck
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.xml._
import scala.xml._