This file contains 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
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
module Lib | |
( run | |
) where | |
import Text.Regex.PCRE.Heavy | |
newtype Word' = |
This file contains 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
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module Main where | |
newtype USD = USD Int deriving (Show, Num) | |
newtype EUR = EUR Int deriving (Show, Num) | |
main :: IO () | |
main = do | |
print $ (USD 12) + (USD 11) |
This file contains 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
sealed trait Currency | |
case object Dollar extends Currency | |
case object Euro extends Currency | |
case class Money[T <: Currency](value: Int, currency: T) { | |
def +(that: Money[T]): Money[T] = { | |
Money[T](this.value + that.value, currency) | |
} | |
} |
This file contains 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 org.example; | |
import java.util.Optional; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
class NPEChecked extends Exception implements Supplier<NPEChecked> { | |
private static final NPEChecked instance = new NPEChecked(); | |
@Override |
This file contains 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
case class IO[A](unsafeRun: () => A): | |
def map[B](f: A => B): IO[B] = | |
IO(() => f(unsafeRun())) | |
def flatMap[B](f: A => IO[B]): IO[B] = | |
IO(() => f(unsafeRun()).unsafeRun()) | |
val io: IO[Int] = IO(() => { | |
println("Running side effect!!!") |
This file contains 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 Monad[M[_]] { | |
def pure[A](a: A): M[A] | |
def flatMap[A, B](m: M[A])(f: A => M[B]): M[B] | |
} | |
enum MyOption[+T]: | |
case None | |
case Some(value: T) | |
given Monad[MyOption] with |
This file contains 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
// Inspired by Rock The JVM exercise video - https://youtu.be/hHMEMRGHmAI | |
let random = new System.Random() | |
let a: list<int> = [ for i in 1..1000 -> random.Next(1, 1000) ] | |
let rec insertSort (l: list<'a>) (comp: 'a -> 'a -> int) : list<'a> = | |
let rec insert (e: 'a) (l: list<'a>) : list<'a> = | |
if l.IsEmpty then [ e ] | |
else if comp e l.Head <= 0 then e :: l |
This file contains 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 SafeModule = | |
let strToInt (str: string) : int option = | |
match System.Int32.TryParse str with | |
| true, i -> Some i | |
| _ -> None | |
type TestflowBuilder() = | |
member this.Bind(x: 'b option, f: 'b -> 'a option) : 'a option = Option.bind f x | |
member this.Return(x: 'a) : 'a option = Some x |
This file contains 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 cats.* | |
import cats.syntax.all.* | |
import cats.data.* | |
import cats.effect.* | |
trait AppError(val msg: String) | |
case object InvalidConfig extends AppError("ERROR: invalid config :(") | |
final case class Config(port: Int) | |
type Result = String |
OlderNewer