Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
bkyrlach / constrained.hs
Created October 10, 2018 18:32
Constrained int...
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
@bkyrlach
bkyrlach / serialization.scala
Created October 2, 2018 17:21
Doesn't seem to work...
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)
@bkyrlach
bkyrlach / either.scala
Last active September 28, 2018 16:33
Useful categories implemented...
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]
@bkyrlach
bkyrlach / example3.scala
Last active October 25, 2018 03:15
Motivating state...
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)
}
@bkyrlach
bkyrlach / example1.scala
Created September 26, 2018 16:12
Some scala learning...
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")
trait Logging[F[_]] {
def info(s: String): F[Unit]
}
class LoggingIO extends Loggingp[IO] { ... }
trait Printer[F[_]] {
def println(s: String): F[Unit]
}
@bkyrlach
bkyrlach / builder.scala
Created September 24, 2018 13:14
Builder pattern problem...
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 = ???
@bkyrlach
bkyrlach / algebras.scala
Created September 19, 2018 12:20
Tagless final?
trait Logging[F[_]] {
def simple(s: String): F[Unit]
}
trait Fib[F[_]] {
def fib(n: Int): F[BigInt]
}
@bkyrlach
bkyrlach / a.scala
Last active September 6, 2018 13:03
Immutability...
// We both agree that this is immutable
val x = 3
@bkyrlach
bkyrlach / a.cs
Created August 27, 2018 17:56
LINQ
//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));