Skip to content

Instantly share code, notes, and snippets.

View calvinlfer's full-sized avatar
🥳

Calvin Lee Fernandes calvinlfer

🥳
View GitHub Profile
@calvinlfer
calvinlfer / Decoder.scala
Created November 21, 2021 22:39
ZIO Schema DynamicValue example
import zio.Chunk
import zio.schema._
import zio.prelude._
import scala.collection.immutable.ListMap
trait Decoder[+A] {
def decode(in: Json): Either[String, A]
}
@calvinlfer
calvinlfer / ManualUDTApp.scala
Created October 6, 2021 15:54
An example of how to serialize nested datatypes within a Cassandra User Defined Type with cassandra4io and the underlying Datastax driver
import cats.effect._
import com.datastax.oss.driver.api.core.CqlSession
import com.datastax.oss.driver.api.core.`type`.UserDefinedType
import com.datastax.oss.driver.api.core.cql.BoundStatement
import com.datastax.oss.driver.api.core.data.UdtValue
import com.datastax.oss.driver.internal.core.`type`.{DefaultListType, DefaultSetType}
import com.ringcentral.cassandra4io.CassandraSession
import com.ringcentral.cassandra4io.cql._
import java.net.InetSocketAddress
@calvinlfer
calvinlfer / JsonSchema.scala
Created July 5, 2021 14:48
A simple JSON Schema deriver
package io.kaizensolutions.jsonschema
import io.circe.Json
import io.circe.syntax._
import magnolia.{CaseClass, Magnolia}
import scala.language.experimental.macros
trait JsonSchema[A] {
def generate: Json = Json.fromFields(JsonSchema.schema +: intermediate)
@calvinlfer
calvinlfer / safer.scala
Created June 11, 2021 20:25
Refined types in Scala 3 with ZIO Prelude
import scala.compiletime._
import zio.prelude._
package safer:
type Digit5OrMore = Digit5OrMore.Type
object Digit5OrMore extends Subtype[Int]:
private inline def check(i: Int): Boolean = i >= 5
private inline def renderError(i: Int): String = s"input $i must be at least 5"
inline def compiletime(inline i: Int): Digit5OrMore =
@calvinlfer
calvinlfer / DeduplicateByHash.scala
Created March 20, 2021 03:55
An MD5 based file deduplicator using ZIO and better files
import better.files._
import zio._
import zio.console._
object DeduplicateByHash extends App {
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
val program =
for {
raw <- Task.fromEither(
Either.cond(
@calvinlfer
calvinlfer / PrimeExample.scala
Created March 6, 2021 20:07
Generating prime numbers in pure functional Scala using FS2 Streams
object PrimeExample extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
val simplePrimes: Stream[Pure, Int] =
Stream
.iterate(1)(_ + 1)
.flatMap(candidate =>
Stream
.range(2, candidate - 1) // can further optimize using sqrt(candidate)
.exists(i => candidate % i == 0)
@calvinlfer
calvinlfer / CustomDiff.scala
Last active March 13, 2022 15:42
This shows how to do a Diff + Merge for any datatype provided you have the appropriate Diff and Merge all the primitive instances used by the datatype. This is done using Shapeless 2.3.3. Diff and Merge go hand-in-hand
// Unhappy with the default behavior of a particular datatype?
// You can override it as you wish, you must write a Diff and merge for the datatype you wish to override
final case class Address(
streetNumber: Int,
streetName: String,
city: String,
country: String
)

Keybase proof

I hereby claim:

  • I am calvinlfer on github.
  • I am calvinlfer (https://keybase.io/calvinlfer) on keybase.
  • I have a public key ASA7CwvEM3dYYzgHu4ZtBrxdSZBngyS79VoMI9x_ZKOnRAo

To claim this, I am signing this object:

@calvinlfer
calvinlfer / example.scala
Created May 8, 2020 18:21
Final Encoding and Initial Encoding
object ecommerce_marketing {
type Event = Map[Attribute, Value]
sealed trait Attribute
object Attribute {
case object EventType extends Attribute
case object UserName extends Attribute
case object ShoppingCartId extends Attribute
case object Email extends Attribute
case object WebSession extends Attribute
@calvinlfer
calvinlfer / readme.md
Last active February 4, 2020 15:42
Understanding covariance and contravariance rules

Understanding Covariance and Contravariance

There are at least 3 aspects to consider when dealing with contravariance and covariance when using container types.

def example(x: Container[T]): Container[T]
  1. What can you feed in
  2. What can point to the result
  3. What can the function return