Skip to content

Instantly share code, notes, and snippets.

View kryptt's full-sized avatar
:octocat:
Looking for inspiration

Rodolfo Hansen kryptt

:octocat:
Looking for inspiration
View GitHub Profile
@kryptt
kryptt / cats-eo.scala
Last active May 21, 2022 16:49
existential optics
package eo
import cats.{Applicative, Bifunctor, Functor, Traverse}
import cats.arrow._
import cats.implicits._
trait Accessor[F[_, _]] {
def get[A]: [X] => F[X, A] => A
}
@kryptt
kryptt / pipes.scala
Created September 28, 2021 10:04
Caching pipes
package kobo
package assist
import scalacache.{Cache, Mode}
import fs2.{INothing, Pipe, Stream}
import fs2.concurrent._
import cats.syntax.all._
import cats.data.{EitherT, OptionT}
@kryptt
kryptt / optional.cpp
Last active September 13, 2021 15:54
fp-in-cpp
#include <tl/expected.hpp>
#include <iostream>
#include <stdexcept>
#include <functional>
#include <variant>
// Logic in modern civilization
template<typename A, typename B>
using And = std::tuple<A, B>;
@kryptt
kryptt / SseClient.scala
Created June 10, 2021 13:51 — forked from izeigerman/SseClient.scala
Complete SSE client Implementation for Scala using http4s.
import cats.effect.{IO, Timer}
import fs2.{Pull, Stream}
import org.http4s._
import org.http4s.ServerSentEvent.EventId
import org.http4s.client.Client
import org.http4s.headers.{`Cache-Control`, Accept}
import scala.concurrent.duration._
import SseClient._
final class SseClient private (
@kryptt
kryptt / Main.hs
Last active January 11, 2021 21:01
Needs a Name UC1
{-# LANGUAGE OverloadedStrings #-}
module Example where
import Language.Marlowe
main :: IO ()
main = print . pretty $ contract
{- Define a contract, Close is the simplest contract which just ends the contract straight away
@kryptt
kryptt / Main.hs
Created January 11, 2021 20:57
Untitled Project
{-# LANGUAGE OverloadedStrings #-}
module Example where
import Language.Marlowe
main :: IO ()
main = print . pretty $ contract
{- Define a contract, Close is the simplest contract which just ends the contract straight away
@kryptt
kryptt / A.SumValidation-1of289.scala
Last active November 26, 2020 09:41
validation-examples
object ModelXValidation {
def validateMotor(motor: Motor): Boolean =
motor.engine.fold(validateElecticEngine, validateCombustionEngine)
def validateElectricEngine(engine: ElectricEngine): Boolean = engine match {
case eh:HybridElectricEngine => false
case ElectricEngine(coils) => expectedCoils.contains(coils)
}
@kryptt
kryptt / A.engine.scala
Last active November 26, 2020 09:48
type-system-savings
//A type A with Cardinality |A| = 5
class ElectricEngine(core: MotorCoils)
case class HybridElectricEngine(volume: EngineVolume, fuel: Petrol.type) extends ElectricEngine(`1800Coils`)
//A type B with Cardinality |B| = 8
case class CombustionEngine(volume: EngineVolume, fuel: FuelType, mod: Modification)
/* It is common for the system to have more complex structures that house transactions, and these complex structures are what the system is dealing with
It could be balance sheets as defined earlier, but lets pretend there are other structures used for Fraud detection as well...
They are optionally there along with some arbitray metadata, and we need to run a verifier across the transactions inside these structures
It would seem like a lot of very specific code is needed to get all this done; but in reality we already have all the combinators we need:
*/
def systemVerifier[S, Metadata](traversal: Traversal[S, Transaction], verifier: Pipe[IO, Transaction, Transaction]): Pipe[IO, (Option[S], Metadata), (Option[S], Metadata)] =
_.flatMap(
_1[(Option[S], Metadata), Option[S]]
checkAll("balanceSheetTimes", TraversalTests(sheetTimes))