Skip to content

Instantly share code, notes, and snippets.

View missingfaktor's full-sized avatar
🥔

Rahul Goma Phulore missingfaktor

🥔
View GitHub Profile
@missingfaktor
missingfaktor / gist:1220623
Created September 15, 2011 22:07
Contravariant functor
scala> case class Person(name: String, age: Int)
defined class Person
scala> implicitly[math.Ordering[Int]] contramap ((_: Dog).age)
res124: scala.math.Ordering[Dog] = scala.math.Ordering$$anon$3@fc6ef0
scala> implicitly[Equal[Int]] contramap ((_: Dog).age)
res126: scalaz.Equal[Dog] = scalaz.Equals$$anon$2@df06c2
@missingfaktor
missingfaktor / gist:1220681
Created September 15, 2011 22:38
Copure and Cojoin
scala> def f[C[_] : Copure : Cojoin, A](a: C[A]) = (a.copure, a.cojoin)
f: [C[_], A](a: C[A])(implicit evidence$1: scalaz.Copure[C], implicit evidence$2
scala> f(Identity(2))
res130: (Int, scalaz.Identity[scalaz.Identity[Int]]) = (2,2)
scala> f(nel(11, 12, 12))
res131: (Int, scalaz.NonEmptyList[scalaz.NonEmptyList[Int]]) = (11,NonEmptyList(
NonEmptyList(11, 12, 12), NonEmptyList(12, 12), NonEmptyList(12)))
@missingfaktor
missingfaktor / gist:1221790
Created September 16, 2011 10:29
Using applicative in Haskell
Prelude> import Control.Applicative
Prelude Control.Applicative> (*) <$> [1, 2, 3] <*> [40, 50, 60]
[40,50,60,80,100,120,120,150,180]
Prelude Control.Applicative>
@missingfaktor
missingfaktor / gist:1221800
Created September 16, 2011 10:34
Using applicative in Scala
scala> (List(40, 50, 60) <*> (List(1, 2, 3) map ((_: Int) * (_: Int)).curried))
res2: List[Int] = List(40, 50, 60, 80, 100, 120, 120, 150, 180)
scala> (List(1, 2, 3) |@| List(40, 50, 60)) { _ * _ }
res4: List[Int] = List(40, 50, 60, 80, 100, 120, 120, 150, 180)
@missingfaktor
missingfaktor / gist:1222095
Created September 16, 2011 13:14
Typesafe equals from Scalaz
scala> case class Person(ssn: Long, name: String, age: Int)
defined class Person
scala> implicit val e = implicitly[Equal[Long]] contramap ((_ : Person).ssn)
e: scalaz.Equal[Person] = scalaz.Equals$$anon$2@19e8fc5
scala> val p = Person(32323L, "Rahul", 21)
p: Person = Person(32323,Rahul,21)
scala> p === p
@missingfaktor
missingfaktor / gist:1223770
Created September 17, 2011 08:59
A simple problem solved in Haskell
import Data.Maybe
lengths :: (Int -> Bool) -> [[a]] -> [Int]
lengths pred xs = mapMaybe lengthIfLong xs
where
lengthIfLong s = let n = length s
in if pred n
then Just n
else Nothing
@missingfaktor
missingfaktor / gist:1227213
Created September 19, 2011 18:35
All combinations
import Control.Arrow
combine :: [a] -> [[a]]
combine = sequence . uncurry replicate . (length &&& id)
{-
Test run:
Prelude Control.Arrow> combine ['A', 'C', 'G']
["AAA","AAC","AAG","ACA","ACC","ACG","AGA","AGC","AGG","CAA","CAC","CAG","CCA","CCC","CCG","CGA","CGC","CGG","GAA","GAC","GAG","GCA","GCC","GCG","GGA","GGC","GGG"]
@missingfaktor
missingfaktor / gist:1262539
Created October 4, 2011 19:27
Either monad
scala> def unsafe[A](f: => A): Either[Throwable, A] = try Right(f) catch { case x => Left(x) }
unsafe: [A](f: => A)Either[Throwable,A]
scala> def A(x: Int) = x + 10
A: (x: Int)Int
scala> def B(x: Int) = x.toString
B: (x: Int)java.lang.String
scala> def C(s: String) = s + "!"
-- A simple definition without using any fancy functions.
haskell>let ifMaybe pred value = if pred value then Just value else Nothing
haskell>:t ifMaybe
ifMaybe :: (a -> Bool) -> a -> Maybe a
haskell>ifMaybe (const True) 2
Just 2
haskell>ifMaybe (const False) 2
Nothing
-- Using functions from standard library.
Consider an example of Show[E] type class. For Show[E], it's possible to have a function f: Show[A] => (B => A) =>
Show[B]. The type constructors that support such a function are contravariant functors.
Now, the particular function f is not of interest to us. We are only interested in the fact that it's possible to
implement a function with that signature for the given data type.
I have observed that the data types that are contravariant functors can usually be made contravariant on their type
parameter. Show[-E] for example. If B <: A, then Show[A] <: Show[B]. A few more data types for which I have observed
this pattern: Equal[E], Comparator[E], Resource[E], Function1[E, F] (on E) etc.