Skip to content

Instantly share code, notes, and snippets.

View BalmungSan's full-sized avatar
👨‍🏫
Learning & Teaching

Luis Miguel Mejía Suárez BalmungSan

👨‍🏫
Learning & Teaching
View GitHub Profile
@BalmungSan
BalmungSan / UTCZonedDateTimeMongoCodec.scala
Last active February 19, 2022 08:00
Custom MongoDB codec for reading/writing ZonedDateTime instances in UTC
import cats.effect.IO
import io.circe.Encoder
import io.circe.generic.semiauto.deriveEncoder
import io.circe.java8.time.encodeZonedDateTimeDefault
import io.circe.syntax.EncoderOps
import org.bson.{BsonReader, BsonType, BsonWriter}
import org.bson.codecs.{DecoderContext, Codec, EncoderContext}
import org.bson.codecs.configuration.{CodecConfigurationException, CodecRegistries}
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.codecs.{DEFAULT_CODEC_REGISTRY, Macros}
@BalmungSan
BalmungSan / StreamAsJsonArrayEncoder.scala
Created August 19, 2018 14:23
Http4s EntityEncoder for returning Streams as JSONArrays using Circe.
import cats.Applicative
import cats.effect.IO
import fs2.Stream
import io.circe.Encoder
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax.EncoderOps
import org.http4s.{EntityEncoder, MediaType}
import org.http4s.dsl.io._
import org.http4s.headers.`Content-Type`
@BalmungSan
BalmungSan / Polymorphism.md
Last active September 11, 2024 00:00
Polymorphism in Scala.

Polymorphism in Scala

This document aims to show and compare three alternatives for achieving polymorphism in Scala.

  • Subtyping, common in object-oriented languages like Java.
  • Duck typing, common in dynamically typed languages like Python.
  • Typeclasses, common in functional languages like Haskell.

Additionally, when implementing the typeclass pattern in Scala,

@BalmungSan
BalmungSan / Streaming.scala
Created June 25, 2019 21:15
10 code snippets to introducing cats.effect.IO & fs2.Stream
// IO: A Monad for side-effects.
import $ivy.`org.typelevel::cats-effect:1.3.1`
import cats.effect.IO
import scala.concurrent.ExecutionContext
implicit val IOTimer = IO.timer(ExecutionContext.global)
implicit val IOShift = IO.contextShift(ExecutionContext.global)
// ----------------------------------------------
@BalmungSan
BalmungSan / Shuffle.scala
Created December 29, 2019 18:15
Simple Ammonite-Scala script to shuffle a list of participants of "amigo secreto".
#!/usr/bin/env amm
import $ivy.`com.sun.mail:javax.mail:1.6.2`
import javax.mail._
import javax.mail.internet._
import java.time.{ZoneId, ZonedDateTime}
import java.util.{Date, Properties}
import scala.jdk.CollectionConverters._
final case class Participant(name: String, email: String)
@BalmungSan
BalmungSan / NeotypesDemo.scala
Created January 13, 2020 22:01
Neotypes demo for ScalaMDE 2019
/** Neotypes demo for ScalaMDE 2019. */
// Imports.
// // FS2.
import fs2.Stream
// // Neotypes.
import neotypes.{GraphDatabase, Session}
import neotypes.implicits.all._
import neotypes.cats.effect.implicits._
@BalmungSan
BalmungSan / EmberStacktrace
Last active January 14, 2020 20:30
Stacktrace produced when attempting to gracefully stop an ember server
java.util.concurrent.RejectedExecutionException: Task cats.effect.internals.IOShift$Tick@1d4aa74b rejected from java.util.concurrent.ThreadPoolExecutor@3a57c4b4[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 3]
at java.base/java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2055)
at java.base/java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:825)
at java.base/java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1355)
at scala.concurrent.impl.ExecutionContextImpl.execute(ExecutionContextImpl.scala:21)
at cats.effect.internals.IOShift$$anon$1.apply(IOShift.scala:28)
at cats.effect.internals.IOShift$$anon$1.apply(IOShift.scala:26)
at cats.effect.internals.IORunLoop$RestartCallback.start(IORunLoop.scala:341)
at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:119)
at cats.effect.internals.IORunLoop$.start(IORunLoop.scala:34)
@BalmungSan
BalmungSan / why-fp.md
Last active December 31, 2022 04:54
Why Functional Programming?

Why Functional Programming?

Probably not the most poetic nor academic or well written phrase, but let me express in plain English why I end up choosing Functional Programming.

Over my time coding (which I have to clarify that has been short) I have learned two important things about code.
(mostly from the experience of many more veteran colleagues)

  1. The best line of code is the one that is not written. Why? Because, every single line of code that you write is instant technical debt. No matter, how well done it is, how well designed it was, how much of 1000x programmer you are, all the code is debt. All the code will become obsolete at some point in time, all the code has to be refactored.
@BalmungSan
BalmungSan / StopIfNoNewElementsAfterDuration.scala
Created November 5, 2020 19:55
Stops an fs2 Stream if it didn't produced any new elements after some timeout
import cats.syntax.all._
import cats.effetc.{Concurrent, Timer}
import cats.effect.concurrent.Ref
import fs2.Stream
import fs2.concurrent.SignallingRef
import scala.concurrent.duration.FiniteDuration
def stopIfNoNewElementsAfter[F[_] : Concurrent : Timer, A](
stream: Stream[F, A],
@BalmungSan
BalmungSan / RockPaperScissors.scala
Created June 19, 2021 16:59
Ammonite script to play Rock-Paper-Scissors
// scala 2.13.6
sealed trait RockPaperScissorsMove extends Product with Serializable
object RockPaperScissorsMove {
final case object Rock extends RockPaperScissorsMove
final case object Paper extends RockPaperScissorsMove
final case object Scissors extends RockPaperScissorsMove
}
trait Game {