Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / dining.go
Last active August 27, 2024 13:01
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 August 27, 2024 12:54
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 / WindowedAverage.scala
Created August 9, 2024 15:33
FS2 moving average demo
import cats.effect.{IO, IOApp}
import fs2.{Stream, text}
import fs2.io.file.{Files, Path}
import fs2.io.{stdout, stderr}
import scala.util.Try
object WindowedAverage extends IOApp.Simple:
private object Fahrenheit:
@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 / add_dicts_list.py
Created April 13, 2024 21:59
Python dictionary list adder
def add_dicts_list(dict_list):
"""
This function adds a list of dictionaries element-wise.
Args:
dict_list: A list of dictionaries with k->int elements.
Returns:
A new dictionary with the sum of the corresponding elements from all dictionaries in dict_list.
"""
@fancellu
fancellu / streamlit HF demo space
Created March 13, 2024 12:20
Streamlit sentiment analysis app on Huggingface spaces
https://huggingface.co/spaces/Fancellu/streamlit-demo
@fancellu
fancellu / image_renamer.py
Created March 2, 2024 12:02
Python Hugging Face image renamer using BeitForImageClassification (useful when you have thousands of badly named images)
from PIL import Image
from transformers import BeitImageProcessor, BeitForImageClassification
import os
# Define the directory containing images
image_dir = "e:/media"
# Load the model and processor
processor = BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
model = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
@fancellu
fancellu / sqlx_postgres.rs
Last active March 2, 2024 12:03
Rust sqlx example
use futures::TryStreamExt;
use sqlx::postgres::{PgPoolOptions, PgRow};
use sqlx::{Error, PgPool, Row};
use uuid::Uuid;
#[derive(sqlx::FromRow, Debug)]
struct People {
id: i32,
name: String,
}
@fancellu
fancellu / tokio_channels_demo.rs
Created February 27, 2024 13:26
Rust tokio channels demo
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;