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
// 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)
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))
@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 {
@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 / 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 / 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 / traversal.scala
Last active August 17, 2020 10:02
Useful Lenses
class BalanceSheet {
val froms: List[Transaction]
val toss: List[Transaction]
/* ... */
}
class Transaction {
val issue: DateTime
val acknowledge: DateTime
/* ... */
@kryptt
kryptt / SVNXmlParserSpec.scala
Created September 25, 2019 07:28
SVN XML Parser Property Based Test Specification
package industrious
import io.dylemma.spac.xml._
import org.specs2.{ScalaCheck, Specification}
import org.scalacheck.{Gen, Prop}
class SvnXmlParserSpec extends Specification with ScalaCheck {
def is = s2"""
SVN XML Parser can
parse entries $parseEntries
@kryptt
kryptt / innerFlow.sc
Last active October 18, 2019 10:10
Inner Flow for Vitaly
#!/usr/bin/env amm
import $plugin.$ivy.`org.typelevel:::kind-projector:0.11.0`
import $ivy.{
`co.fs2::fs2-core:2.0.1`,
`com.github.julien-truffaut::monocle-generic:2.0.0`
}
import cats.Applicative
import fs2._
import monocle._
@kryptt
kryptt / tree.sc
Created January 30, 2018 08:59
Four different strategies for calculating tree depth. a tail recursive version, a continuation passing version and a recursion schema version
#!/usr/bin/amm
import $ivy.`org.scalaz::scalaz-core:7.2.18`
import $ivy.`com.slamdata::matryoshka-core:0.18.3`
import scala.annotation.tailrec
sealed trait Tree[+A]
object Tree {