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 / 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 / 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 / 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 {
@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 / 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 / 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 / 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 / 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 / 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)