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
| module Constrained (constrainInt,value,ZeroToFifty) where | |
| data ZeroToFifty = CInt { value :: Int } deriving (Show) | |
| constrainInt :: Int -> ZeroToFifty | |
| constrainInt n | n < 0 = error "Value cannot be lower than zero" | |
| constrainInt n | n > 50 = error "Value cannot be greater than fifty" | |
| constrainInt n = CInt n |
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 com.github.plokhotnyuk.jsoniter_scala.macros._ | |
| import com.github.plokhotnyuk.jsoniter_scala.core._ | |
| sealed class A | |
| case class B(n: Int) extends A | |
| case class C(s: String) extends A | |
| implicit codec: JsonValueCodec[A] = JsonCodecMaker.make[A](CodecMakerConfig()) | |
| val b: A = B(3) |
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 example | |
| object Program4 { | |
| sealed trait Either[+L,+R] | |
| case class Left[L](l: L) extends Either[L,Nothing] | |
| case class Right[R](r: R) extends Either[Nothing,R] | |
| sealed trait MonadOps[M[_],A] { | |
| def map[B](f: A => B): M[B] |
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 example | |
| object Program3 { | |
| // Niave implementation with very poor performance. | |
| // If you ask this for the 40th fib number, you'll be here awhile | |
| def fib1(n: BigInt): BigInt = n match { | |
| case n if n <= 2 => 1 | |
| case _ => fib1(n - 1) + fib1(n - 2) | |
| } |
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 example | |
| object Program { | |
| def main(args: Array[String]): Unit = { | |
| println("Hello, world.") | |
| val x = readLine().toInt | |
| x match { | |
| case 1 => println("one") | |
| case 2 => println("two") |
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 Logging[F[_]] { | |
| def info(s: String): F[Unit] | |
| } | |
| class LoggingIO extends Loggingp[IO] { ... } | |
| trait Printer[F[_]] { | |
| def println(s: String): F[Unit] | |
| } |
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
| class FooBuilder { | |
| def a(s: String): FooBuilder = ??? | |
| def b(n: Int): FooBuilder = ??? | |
| def c(config: SomeConfig): FooBuilder = ??? | |
| def build(): Foo = ??? | |
| } | |
| class BarBuilder { | |
| def a(s: String): BarBuilder = ??? |
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 Logging[F[_]] { | |
| def simple(s: String): F[Unit] | |
| } | |
| trait Fib[F[_]] { | |
| def fib(n: Int): F[BigInt] | |
| } |
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
| // We both agree that this is immutable | |
| val x = 3 |
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
| //LINQ code. | |
| var result = | |
| from a in new int[] {1,2,3} | |
| from b in new int[] {4,5,6} | |
| select a + b; | |
| //Sugar for this... | |
| var result2 = | |
| new int[] {1,2,3}.SelectMany(a => | |
| new int[] {4,5,6}.Select(b => a + b)); |