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
| 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 |
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
| 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))) |
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
| 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> |
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
| 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) |
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
| 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 |
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
| 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 | |
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
| 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"] |
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
| 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 + "!" |
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
| -- 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. |
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
| 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. |