This file contains hidden or 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
| /* | |
| * 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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.{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 { |
This file contains hidden or 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
| 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)) |
This file contains hidden or 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
| // 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) |
This file contains hidden or 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
| checkAll("balanceSheetTimes", TraversalTests(sheetTimes)) |
This file contains hidden or 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
| /* 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]] |
This file contains hidden or 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
| //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) |
This file contains hidden or 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
| 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) | |
| } | |