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 / SimgleAbstractMethodSyntax.scala
Created Jul 17, 2019
Some nice tricks with single abstract method interface, Scsala 2.11+
View SimgleAbstractMethodSyntax.scala
object SimgleAbstractMethodSyntax extends App{
// Some nice tricks with single abstract method interface, Scsala 2.11+
trait DoIt{
def doSomething(x:Int): Int
}
// I am creating an instance of DoIt, and supplying doSomething
val doit: DoIt= _+1
@fancellu
fancellu / InterestCalculator.scala
Created Jul 17, 2019
Scala Interest calculator based on bands, rounded to precision
View InterestCalculator.scala
case class Band(top: Double, interest: Double)
class InterestCalculator(bands: List[Band], precision: Int = 2) {
def calc(amount: Double) = {
val interest = bands.collectFirst {
case Band(top, interest) if top >= amount => interest * amount
}.getOrElse(0.0)
@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
You can’t perform that action at this time.