Skip to content

Instantly share code, notes, and snippets.

import zio.*
import zio.prelude.*
import zio.prelude.fx.*
object BaseSyntax:
type Program[S, R, E, A] = ZPure[Nothing, S, S, R, E, A]
type LoggedProgram[W, S, R, E, A] = ZPure[W, S, S, R, E, A]
@jmatsushita
jmatsushita / README
Last active June 25, 2024 02:19
Setup nix, nix-darwin and home-manager from scratch on an M1 Macbook Pro
###
### [2023-06-19] UPDATE: Just tried to use my instructions again on a fresh install and it failed in a number of places.
###. Not sure if I'll update this gist (though I realise it seems to still have some traffic), but here's a list of
###. things to watch out for:
### - Check out the `nix-darwin` instructions, as they have changed.
### - There's a home manager gotcha https://github.com/nix-community/home-manager/issues/4026
###
# I found some good resources but they seem to do a bit too much (maybe from a time when there were more bugs).
# So here's a minimal Gist which worked for me as an install on a new M1 Pro.
@dnatic09
dnatic09 / TestContainersApiTestHarness.scala
Created May 22, 2020 01:53
Example test harness using TestContainers-Scala
trait TestContainersApiTestHarness extends FlatSpec with Matchers with BeforeAndAfterAll {
private val REDIS_PORT = 6379
private val container = GenericContainer("redis:5.0.8-alpine",
exposedPorts = Seq(REDIS_PORT),
waitStrategy = Wait.forListeningPort()
)
container.start()
private val mappedRedisExternalPort = container.mappedPort(REDIS_PORT)
private val redisClient = new RedisClient("localhost", mappedRedisExternalPort)
private val dao = new RedisTestContainersDao(redisClient)

Easy Scala Publication

The following describes how you can publish artifacts for any sbt project using the GitHub Package Registry and the sbt-github-packages plugin.

Step 1: Create a GitHub Token

In your GitHub account, go to Settings > Developer settings > Personal access tokens, then click on Generate new token (or click here). Fill in some sort of meaningful name (I chose Dev) and click on the write:packages checkbox:

the new personal access token page with the above steps having been followed

import cats.{ ApplicativeError, MonadError }
import cats.data.{ Kleisli, OptionT }
import cats.effect.Sync
import cats.effect.concurrent.Ref
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s._
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe._
@sigma23
sigma23 / covert_json_array_to_jsonlines_jq_or_python.txt
Last active May 15, 2023 19:22
Convert from json array to jsonlines using jq and python
# Run this from the bash command prompt. Make sure that jq is installed https://github.com/stedolan/jq/wiki/Installation
# json_temp.json has the file in the form [{...}, {...}, {...}] and coverts to {...}\n{...}\n
jq -c '.[]' json_temp.json > json_temp.jsonl
# From within python can do this:
pip install jsonlines
import json
import jsonlines
@SystemFw
SystemFw / Test.scala
Last active June 7, 2019 05:15
Shapeless: Convert between any two compatible case classes, selecting a subset of the fields
object Test {
case class User(name: String, age: Int)
case class UserDTO(name: Option[String], age: Option[Int])
import conversions._
def a = User("John", 24).convertTo[UserDTO](Set("name"))
// res0: Test.UserDTO = UserDTO(Some(John),None)
case class WrongFieldNames(surname: Option[String], age: Option[Int])
@SystemFw
SystemFw / Free conversation.md
Last active October 17, 2023 09:57
Explaining some of the mechanics of interpretation of Free programs

Balaji Sivaraman @balajisivaraman_twitter

Hi all, I need some help understanding a piece of Doobie code from the examples. It is the StreamingCopy one: (https://github.com/tpolecat/doobie/blob/series/0.4.x/yax/example/src/main/scala/example/StreamingCopy.scala). I am using a modified version of the fuseMap2 example from that file. Here’s how I’ve modified it for my requirements:

  def fuseMap[F[_]: Catchable: Monad, A, B](
      source: Process[ConnectionIO, A],
      sink: Vector[A] => ConnectionIO[B],
      delete: ConnectionIO[Unit]
  )(
 sourceXA: Transactor[F],
package fs3
import cats.Monad
sealed abstract class Free[F[_], R] {
def flatMap[R2](f: R => Free[F, R2]): Free[F, R2] = Free.Bind(this, f)
}
object Free {
case class Pure[F[_], R](r: R) extends Free[F, R]
case class Eval[F[_], R](fr: F[R]) extends Free[F, R]
@mpilquist
mpilquist / example.md
Last active May 17, 2021 13:17
Properly scheduling effect evaluation in FS2

TL;DR - Use fs2.time.sleep_[Task](delay) ++ Stream.eval(effect) instead of Stream.eval(effect.schedule(delay)).

FS2 never interrupts evaluation of an effect. This can lead to surprising behavior when using the schedule method on Task. Consider this test driver:

def testInterruption[A](effect: Stream[Task, A]): Stream[Task, A] = {
  val logStart = Stream.eval_(Task.delay(println("Started: " + System.currentTimeMillis)))
  val logFinished = Stream.eval_(Task.delay(println("Finished: " + System.currentTimeMillis)))
  val interruptSoonAfterStart =
 Stream.eval(async.signalOf[Task,Boolean](false)).flatMap { cancellationSignal =>