Skip to content

Instantly share code, notes, and snippets.

View calvinlfer's full-sized avatar

Cal calvinlfer

View GitHub Profile
@calvinlfer
calvinlfer / Handler.scala
Created August 15, 2018 03:16
AWS Lambda that consumes a Kinesis Data Stream where events are aggregated. It deaggregates them, transforms them and pushes them to Redis (built on FS2 Streams, tagless final and cats.effect.IO)
package com.freckle.segmentation
import java.nio.charset.StandardCharsets.UTF_8
import java.util.concurrent.TimeUnit
import cats.effect._
import com.amazonaws.kinesis.deagg.RecordDeaggregator
import com.amazonaws.services.lambda.runtime.events.KinesisEvent
import com.amazonaws.services.lambda.runtime.{Context, RequestHandler}
import models._
@calvinlfer
calvinlfer / README.md
Last active December 18, 2024 10:53
Developing an understanding of the FS2 Pull datatype

Developing an understanding of the FS2 Pull data type

In Pull[F, O, R], R is the return type. Pull represents a computation that emits some values on the Stream (of type O) and returns a new thing (R). In order to convert a Pull to a Stream, R must be Unit. This is because an FS2 Stream does not have the ability to terminate with a return value.

image

See here for the conversation

  • Stream[F, O] is monadic over O which are the output values emitted
  • Pull[F, O, R] is monadic over R which is used for stateful transformations
import fs2._
import cats.effect._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
import scala.language.higherKinds
object UnderscoreFS2QueueDequeuePost extends App {
implicit val ec: ExecutionContext =
scala.concurrent.ExecutionContext.Implicits.global
@calvinlfer
calvinlfer / Signals.scala
Created August 27, 2018 22:12
FS2 Signals
import cats.effect._
import fs2._
import fs2.async._
def log[F[_]: Sync, A](prefix: String): Pipe[F, A, A] =
inputStream => inputStream.evalMap(a => Sync[F].delay {
println(s"$prefix> $a")
a
})
@calvinlfer
calvinlfer / day-1.scala
Created October 2, 2018 04:10
Functional Scala: Toronto edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.essentials
import java.time.{Instant, ZonedDateTime}
import scala.util.Try
object types {
type ??? = Nothing
@calvinlfer
calvinlfer / day-2.scala
Last active October 4, 2018 13:25
Functional Scala: Toronto Edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.abstractions
import scalaz._
import Scalaz._
import scala.language.higherKinds
object algebra {
@calvinlfer
calvinlfer / day-3.scala
Last active June 21, 2021 12:09
Functional Scala: Toronto edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.effects
import scalaz.zio._
import scalaz.zio.console._
import scala.annotation.tailrec
import scala.concurrent.duration._
@calvinlfer
calvinlfer / day-4.scala
Created October 4, 2018 21:44
Functional Scala: Toronto edition
// Copyright(C) 2018 - John A. De Goes. All rights reserved.
package net.degoes.applications
import java.util.concurrent.ExecutorService
import scalaz.zio._
import scalaz.zio.console._
import scalaz.zio.interop.scalaz72._
import scalaz._
@calvinlfer
calvinlfer / fpdesign.scala
Created October 5, 2018 16:13
Functional Design of a Loyalty points system
object fpdesign {
/**
* Business requirements
* ---------------------
* Loyalty Points management
*
* Entities (data)
* - Customer
* - Loyalty Points
* - Loyalty Point Account
@calvinlfer
calvinlfer / Main.scala
Created October 6, 2018 02:53
Purely functional guessing using ZIO
package com.github.calvin
import scalaz.zio._
import scalaz.zio.console._
object Main extends scalaz.zio.App {
override def run(args: List[String]): IO[Nothing, ExitStatus] = {
def promptForNumber: IO[Exception, Int] = {
def toNumber(input: String): Option[Int] = {
def isNumber(input: String): Boolean = (input.length == 1) && input.head.isDigit