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 / background.scala
Last active November 28, 2019 12:32
no ifs profit
/*
* This file has the setup and boilerplate.
* how to calculate the pay for a teacher, and the fee for a student.
* how to find the student or teacher just from a name string, and a
* `List` of names.
*/
type Fee = Double
type Pay = Double
type Balance = Double
@kryptt
kryptt / termination.scala
Last active January 23, 2020 13:09
constructive-programming-fixes
def sumOfAllNumbers(): Int = {
var numbers = Array(10,20,30,40,50);
var N:Int=0;
var sum: Int=0;
for ( N <- numbers ) {
sum+=N;
}
sum
}
// -- sanity check
@kryptt
kryptt / termination.scala
Last active January 23, 2020 16:10
Logic in your Types Answers
def sumOfAllNumbers(): Int =
Array(10,20,30,40,50).sum
// -- sanity check
sumOfAllNumbers() == 150
def evenElements[A](ls: List[A]): List[A] =
ls.view.zipWithIndex.filter(_._2 % 2 == 1).map(_._1).toList
// -- sanity check
@kryptt
kryptt / DiffExample.scala
Last active June 25, 2020 19:19
power of higher kinded types...
import cats.{Bifoldable, Eq, Foldable, Show}
import cats.data.Ior
import cats.instances.either._
import cats.instances.option._
import cats.instances.list._
import cats.syntax.bifoldable._
import cats.syntax.foldable._
import cats.syntax.align._
object DiffExample {
val BalanceSheetId: Lens[BalanceSheet, DatabaseID] =
GenLens[BalanceSheet](_.id)
def save(sheet: BalanceSheet): IO[DatabaseID] = ???
//Updates a given sheet with the new id from the database
def store(sheet: BalanceSheet): IO[BalanceSheet] =
save(sheet).map(BalanceSheetId.set(_)(sheet))
// We can re-use a simple function that goes from Time to a new Time
val adjustTime: DateTime => DateTime = ???
// With an appropriate traversal in scope we can apply the adjustTime function from this library to any incoming holder
def adjustTimes[T](implicit T: Traversal[T, DateTime]): T => T =
T.modify(adjustTime)
// Again we can keep a concrete alternative to adjustTime, we just increment it by duration:
def increaseTimeBy(duration: FiniteDuration): DateTime => DateTime =
_.add(duration)
checkAll("balanceSheetTimes", TraversalTests(sheetTimes))
/* 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]]
@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)
@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)
}