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 / why-fp.md
Last active November 12, 2025 13:11
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 / 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 / Polymorphism.md
Last active January 30, 2025 18:34
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 / NeotypesSchemaDraft.scala
Last active December 13, 2022 22:21
Initial draft of the design for Neotypes-Schema — Explicit decoders
//> using scala "2.13.10"
//> using lib "org.typelevel::cats-effect:3.3.14"
//> using lib "co.fs2::fs2-core:3.4.0"
//> using lib "org.neo4j.driver:neo4j-java-driver:5.3.0"
import cats.effect.IO
import fs2.Stream
import org.neo4j.driver.summary.ResultSummary
import org.neo4j.driver.types.{IsoDuration => NeoDuration, Point => NeoPoint}
@BalmungSan
BalmungSan / monads.md
Created November 15, 2022 21:15
What is a Monad and why you shouldn't care

What is a Monad and why you shouldn't care

People trying to learn about FP will heard about the M word sooner than latter. Personally I believe that way sooner than what is required, and recommended; which is what makes folks to be confused about it.

This is my humble attempt to try to briefly explain the concept, while also arguing that you shouldn't really care too much about it. This all with the intention to make your FP journey more enjoyable 😄 — without further ado, let's start.

Motivation behind Monads

@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 / 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 {
@BalmungSan
BalmungSan / MultiTaskRunner.scala
Last active August 23, 2021 20:48
MultiTaskRunner - A CE3 program that runs multiple tasks in parallel and allow them to report their progress
package example
import cats.effect.IO
import cats.effect.kernel.Ref
import cats.syntax.all._
import scala.concurrent.duration._
object MultiTaskRunner {
/** Allows a task to report its progress. */
@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 / HighLowPriorityRunner.scala
Last active June 24, 2021 14:29
HighLowPriorityRunner - A CE3 program to schedule high & low priority jobs to be run on a dedicated EC
package example
import cats.effect.Async
import cats.effect.std.Queue
import cats.effect.syntax.all._
import cats.syntax.all._
import scala.concurrent.ExecutionContext
object HighLowPriorityRunner {