Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

Block or report user

Report or block fancellu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@fancellu
fancellu / CaseClassEquality.scala
Last active Jul 13, 2019
case class equality: Making scala only check some params, not all
View CaseClassEquality.scala
case class Product(name: String)(val price: Double)
// we can choose to only supply the name, i.e. partially applied
val banana=Product("banana") _
val banana10=banana(10)
val bananafree=banana(0)
val apple=Product("apple")(5)
val list=List(banana10, apple, bananafree)
@fancellu
fancellu / Validate.scala
Last active Jul 13, 2019
Example of simple multiclause validation, with early fail, with a couple of implementations (Yes, I know an Either would be better)
View Validate.scala
import scala.annotation.tailrec
case class Input(currency: String, amount: Int)
case class Output(passed: Boolean, code: Option[String])
val checkCurrency: Input => Output = { input =>
if (input.currency != "ETH") {
Output(false, Some("INVALID_CURRENCY"))
}
@fancellu
fancellu / MonadT.scala
Created Jun 27, 2019
Simple example of Cats OptionT monad transformer usage vs plain Scala
View MonadT.scala
import cats.data._
import cats.implicits._
val li=List(Option(1),Option(2), None)
println(OptionT(li).map(_+10).value)
//List(Some(11), Some(12), None)
println(li.map(_.map(_+10)))
//List(Some(11), Some(12), None)
@fancellu
fancellu / Heavy.scala
Created May 27, 2019
Simple Scala closure example showing how we can cache object setup
View Heavy.scala
object Heavy extends App{
val heavy: Int => Int ={
val li=1 to 10000
println("creating")
(i:Int) => li(i)
}
val h=heavy
println(h(0))
println(h(1))
View index.html
<html>
<head>
<!-- Do you need Material Icons? -->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/quasar/dist/quasar.min.css" rel="stylesheet" type="text/css">
</head>
<body>
@fancellu
fancellu / TimestampFormat.scala
Last active Jun 27, 2019
play.json formatter for java.sql.Timestamp and java.time.LocalDateTime
View TimestampFormat.scala
package models.database
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import play.api.libs.json.{Format, JsString, JsSuccess, JsValue}
object Common {
@fancellu
fancellu / RowParser.scala
Last active Mar 4, 2019
Little Typeclass example
View RowParser.scala
case class Row(rowstring: String)
trait RowParse[A]{
def parse(row:Row):A
}
case class TwoPart(v1: String, v2: String)
case class ThreePart(v1: String, v2: String, v3: String)
@fancellu
fancellu / Robots.scala
Last active Jul 15, 2019
Robot Wharehouse
View Robots.scala
import scala.util.Try
object Robots extends App{
object Direction extends Enumeration{
type Direction=Value
val N,E,S,W=Value
}
final val MAX_X=10
@fancellu
fancellu / Censor.scala
Created Dec 4, 2018
Scala uJson code to censor certain field names
View Censor.scala
import ujson.{ Obj, Value }
object Censor{
def updateAll(root: Value, target: (String => Boolean), newValue: Value): Value = {
def update(value: Value, parent: Value = null, valueName: String = null): Unit = {
value match {
case Obj(values) =>
values.map { case (name, v) => update(v, value, name) }
@fancellu
fancellu / FoldingValidationExamples.scala
Created Nov 13, 2018
Simple chaining validation example with pure Scala
View FoldingValidationExamples.scala
import scala.util.{Failure, Try}
val li=List(1,2,3,4)
type Transform[F[_],A]=F[A] => F[A]
def filterSmall: Transform[Try,Int] ={ ti=>
ti.flatMap(i=>if (i>1) Try(i) else new Failure(new Exception(s"$i is too small")))
}
You can’t perform that action at this time.