Skip to content

Instantly share code, notes, and snippets.

View gvolpe's full-sized avatar
🤓
https://leanpub.com/u/gvolpe

Gabriel Volpe gvolpe

🤓
https://leanpub.com/u/gvolpe
View GitHub Profile
@gvolpe
gvolpe / shared-state-in-fp.md
Last active March 15, 2022 20:27
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

import cats.Show
import cats.derived.semiauto.*
import cats.syntax.all.*
final case class User(name: String, age: Int) derives Show
object Demo:
@main def run =
println(User("Joe", 18).show) // outputs: User(name=Joe, age=18)
@gvolpe
gvolpe / ParseUrlParameters.scala
Created September 14, 2015 14:26
Parse URL Parameters
import java.net.URLDecoder
def parseUrlParameters(url: String) = {
url.split("&").map( v => {
val m = v.split("=", 2).map(s => URLDecoder.decode(s, "UTF-8"))
m(0) -> m(1)
}).toMap
}

Understanding Comparative Benchmarks

I'm going to do something that I don't normally do, which is to say I'm going to talk about comparative benchmarks. In general, I try to confine performance discussion to absolute metrics as much as possible, or comparisons to other well-defined neutral reference points. This is precisely why Cats Effect's readme mentions a comparison to a fixed thread pool, rather doing comparisons with other asynchronous runtimes like Akka or ZIO. Comparisons in general devolve very quickly into emotional marketing.

But, just once, today we're going to talk about the emotional marketing. In particular, we're going to look at Cats Effect 3 and ZIO 2. Now, for context, as of this writing ZIO 2 has released their first milestone; they have not released a final 2.0 version. This implies straight off the bat that we're comparing apples to oranges a bit, since Cats Effect 3 has been out and in production for months. However, there has been a post going around which cites various compar

import com.sksamuel.avro4s.SchemaFor
import com.sksamuel.avro4s.refined._
import io.estatico.newtype.Coercible
import io.chrisdavenport.fuuid.FUUID
import java.util.UUID
import scala.concurrent.duration.FiniteDuration
import io.circe.{Json, JsonObject}
import com.comcast.ip4s.Ipv4Address
object avro extends SchemaForCoercible {
[Dec16 23:16] [drm:amdgpu_cs_ioctl [amdgpu]] *ERROR* Failed to initialize parser -2!
[ +0.000908] gmc_v9_0_process_interrupt: 24 callbacks suppressed
[ +0.000010] amdgpu 0000:04:00.0: amdgpu: [gfxhub0] retry page fault (src_id:0 ring:0 vmid:6 pasid:32769, for process X pid 1073 thread X:cs0 pid 1095)
[ +0.000007] amdgpu 0000:04:00.0: amdgpu: in page starting at address 0x00008001018f0000 from client 27
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: VM_L2_PROTECTION_FAULT_STATUS:0x00640C51
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: Faulty UTCL2 client ID: 0x6
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: MORE_FAULTS: 0x1
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: WALKER_ERROR: 0x0
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: PERMISSION_FAULTS: 0x5
[ +0.000001] amdgpu 0000:04:00.0: amdgpu: MAPPING_ERROR: 0x0
> dmesg | rg amdgpu
[ 0.751743] stage-1-init: [Tue Dec 15 09:27:34 UTC 2020] loading module amdgpu...
[ 0.936858] [drm] amdgpu kernel modesetting enabled.
[ 0.936959] amdgpu: Topology: Add CPU node
[ 0.937028] fb0: switching to amdgpudrmfb from EFI VGA
[ 0.937091] amdgpu 0000:04:00.0: vgaarb: deactivate vga console
[ 0.937116] amdgpu 0000:04:00.0: enabling device (0006 -> 0007)
[ 0.937169] amdgpu 0000:04:00.0: amdgpu: Trusted Memory Zone (TMZ) feature disabled as experimental (default)
[ 0.937263] amdgpu: ATOM BIOS: 113-RENOIR-026
[ 0.937324] amdgpu 0000:04:00.0: amdgpu: VRAM: 512M 0x000000F400000000 - 0x000000F41FFFFFFF (512M used)
implicit def eitherEncoder[A: Encoder, B: Encoder]: Encoder[Either[A, B]] =
Encoder[Json].contramap[Either[A, B]](_.asJson)
implicit def eitherDecoder[A: Decoder, B: Decoder]: Decoder[Either[A, B]] =
Decoder[Json].emap[Either[A, B]] { js =>
js.as[A] match {
case Left(_) => js.as[B].map(_.asRight[A]).leftMap(_.message)
case Right(x) => Right(x.asLeft[B])
}
}
{
appimagePackage = { binName, version, url, sha256 ? fakeSha256
, meta ? { platforms = [ "x86_64-linux" ]; } }:
let
pname = "${binName}-appimage";
name = "${pname}-${version}";
src = fetchurl {
inherit url sha256;
name = "${name}.AppImage";
@gvolpe
gvolpe / gadt.md
Created January 31, 2019 06:40 — forked from smarter/gadt.md
GADTs in Scala

Generalized Algebraic Data Types in Scala

Basic GADTs

Here's an ADT which is not a GADT, in Haskell:

data Expr = IntExpr Int | BoolExpr Bool