Skip to content

Instantly share code, notes, and snippets.

View lachezar's full-sized avatar
🏗️
Maker of unrepresentable illegal states

Lachezar Yankov lachezar

🏗️
Maker of unrepresentable illegal states
View GitHub Profile
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
@lachezar
lachezar / Sort.fsx
Last active June 21, 2024 11:31
Sorting with F#
// 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
@lachezar
lachezar / Monads.scala
Last active February 12, 2024 15:36
Scala 3 Monad type class for Option and Either
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
@lachezar
lachezar / IO.scala
Last active November 21, 2023 13:20
IO implementation exercise
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!!!")
@lachezar
lachezar / CheckedOptional.java
Created November 15, 2023 13:11
Java Optional that forces you to deal with the missing value on compile-time level instead of the runtime.
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
@lachezar
lachezar / currency_example.scala
Last active November 30, 2020 16:07
currency_example.scala
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)
}
}
@lachezar
lachezar / currency_example.hs
Last active September 5, 2020 15:12
Currency in Haskell
{-# 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)
@lachezar
lachezar / words.hs
Created October 5, 2019 19:21
Words in Haskell vs Typescript
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuasiQuotes #-}
module Lib
( run
) where
import Text.Regex.PCRE.Heavy
newtype Word' =
@lachezar
lachezar / config.exs
Created October 18, 2016 17:52
sample_config_kafka_ex
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
config :kafka_ex,
# a list of brokers to connect to in {"HOST", port} format
brokers: [
{"localhost", 9092}
],
# the default consumer group for worker processes, must be a binary (string)
@lachezar
lachezar / Devices.java
Last active September 14, 2015 15:17
Helper class to get the consumer friendly device name. Upvote http://stackoverflow.com/a/27836910/1048340
/*
* Copyright (C) 2015 Jared Rummler <jared.rummler@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software