Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / .block
Last active February 8, 2025 21:03
Force directed graph for D3.js v4 with labelled edges and arrows
license: gpl-3.0
height: 600
@fancellu
fancellu / ConsumerExample.scala
Last active November 28, 2024 13:00
Kafka Producer/Consumer Example in Scala
import java.util
import org.apache.kafka.clients.consumer.KafkaConsumer
import scala.collection.JavaConverters._
object ConsumerExample extends App {
import java.util.Properties
@fancellu
fancellu / MyIO.scala
Last active October 5, 2024 13:24
Simple example of an IO effect Monad, no Cats, no ZIO
import scala.util.Random
// Simple example of an IO effect Monad, no Cats
object MyIO extends App{
// IO encapsulates a side effecting operation
final class IO[A](val run: () => A) {
def map[B](f: A => B): IO[B] = IO(f(run()))
@fancellu
fancellu / dining.go
Last active September 30, 2024 21:49
Dining philosophers in golang (I have other impls in Rust, Scala, Scala+Cats effect here)
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"unsafe"
)
@fancellu
fancellu / main.go
Created August 24, 2024 10:55
Golang goroutine channel sync and timeout example
package main
import (
"fmt"
"time"
)
func greet(st string, done chan bool) {
fmt.Println(st)
done <- true
@fancellu
fancellu / mapf.go
Last active September 30, 2024 21:48
Example of a generic FP map function [T=>U], and filter(), that golang doesn't have out of the box
package main
import (
"fmt"
"strconv"
)
// Maps over collection of T and applies function f to each item
// Returns a new slice with the transformed items
func mapf[T any, U any](items []T, f func(T) U) []U {
@fancellu
fancellu / FakeConsoleExample.scala
Created September 27, 2024 20:58
FakeConsoleExample zio supplying a fake Console
import zio._
object FakeConsoleExample extends ZIOAppDefault {
private val program = ZIO.serviceWithZIO[Console] { console =>
for {
_ <- console.printLine("Going to the grocery store")
input <- console.readLine("How are you? ")
_ <- console.printLine(s"You said: $input")
} yield ()
@fancellu
fancellu / InterruptableReadLine.scala
Created September 27, 2024 21:16
InterruptableReadLine ZIO example of a readLine that can be timedout
import zio._
import scala.{ Console => SConsole }
import scala.io.StdIn
import java.io.{ BufferedReader, IOException }
import scala.util.Try
object InterruptableReadLine extends ZIOAppDefault {
def altReadLine(reader: BufferedReader = SConsole.in) =
ZIO
@fancellu
fancellu / BreakDemo.scala
Created April 25, 2024 17:52
Scala3 boundary/break/label demo (uses Scala 3.4.1)
import scala.annotation.targetName
import scala.util.boundary
import scala.util.boundary.{Label, break}
// Works in Scala 3.4.1
def firstIndex[T](xs: List[T], p: T): Int = {
boundary:
for (x, i) <- xs.zipWithIndex do if (x == p) break(i)
-1
@fancellu
fancellu / tokio_spawn.rs
Last active August 17, 2024 19:02
Rust example of tokio::spawn and JoinHandle processing
async fn hello(name: &str) -> String {
// pretend to be doing some work
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
format!("Hello {}", name)
}
fn blocking() -> String {
println!("Blocking");