Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / tokio_spawn.rs
Last active February 23, 2024 09:31
Rust example of tokio::spawn and JoinHandle processing
View tokio_spawn.rs
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");
@fancellu
fancellu / csv_demo.rs
Last active February 20, 2024 19:19
Rust simple CSV processing from a CV file, using csv crate
View csv_demo.rs
use std::{error::Error, process};
fn cities() -> Result<(), Box<dyn Error>> {
let mut csv_rdr = csv::Reader::from_path("data/cities.csv")?;
for result in csv_rdr.records() {
let record = result?;
println!("{:?}", record);
println!("{}",record.get(0).unwrap());
}
Ok(())
@fancellu
fancellu / reqwest_demo.rs
Created February 19, 2024 20:02
Rust reqwest http client async json demo with serde
View reqwest_demo.rs
use serde::Deserialize;
use std::time::Duration;
#[derive(Deserialize, Debug)]
struct Product {
id: i32,
title: String,
description: String,
price: f64,
#[serde(rename = "discountPercentage")]
@fancellu
fancellu / DiningPhilosophersIOApp.scala
Created February 16, 2024 14:23
Dining Philosophers with Cats Effect
View DiningPhilosophersIOApp.scala
import cats.effect.std.Semaphore
import cats.effect._
import cats.implicits._
import scala.concurrent.duration._
object DiningPhilosophersIOApp extends IOApp {
case class Fork(id: Int, lock: Semaphore[IO])
@fancellu
fancellu / DiningPhilosophers.scala
Last active February 16, 2024 12:17
Scala implementation of dining philosophers, after doing it in Rust
View DiningPhilosophers.scala
import java.util.concurrent.Executors
import java.util.concurrent.locks.{Lock, ReentrantLock}
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.*
import scala.concurrent.ExecutionContext.Implicits.global
case class Fork(id: Int, lock: Lock)
case class Philosopher(name: String, left: Fork, right: Fork)
@fancellu
fancellu / dining_philosophers.rs
Last active February 15, 2024 21:05
Rust dining philosophers problem, to eat, each must grab 2 forks, but we have same number of forks as philosophers
View dining_philosophers.rs
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
struct Fork {
id: usize,
mutex: Mutex<()>,
}
impl Fork {
@fancellu
fancellu / Intersect.scala
Created May 28, 2021 10:28
Finds the elements shared by 2 lists, FP style, recursively
View Intersect.scala
object Intersect extends App{
import scala.annotation.tailrec
// finds the elements shared by 2 lists, FP style, recursively
// O(n log n)
@tailrec
def scan(x: List[Int], y: List[Int], out: List[Int] = List.empty[Int]): List[Int] =
(x, y) match {
@fancellu
fancellu / TreeFunctorExample.scala
Last active February 15, 2024 11:20
Example of Tree Functor usage in Cats
View TreeFunctorExample.scala
import cats._
object TreeFunctorExample {
def main(args: Array[String]): Unit = {
sealed trait Tree[+T]
case class Leaf[+T](value: T) extends Tree[T]
case class Branch[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T]
@fancellu
fancellu / MyErrsIO.scala
Created March 21, 2022 12:45
A simple example of cat-effect and error handling, with MonadTransformers
View MyErrsIO.scala
import MyErrsIO.Controller.{Request, postTransfer}
import MyErrsIO.Models.Account
import cats.data.Validated.{Invalid, Valid}
import cats.data.{EitherT, OptionT, Validated, ValidatedNec}
import cats.effect._
import cats.implicits._
import scala.util.control.NonFatal
// A simple example of cat-effect and error handling, with MonadTransformers
@fancellu
fancellu / Fs2Queues.scala
Created July 14, 2022 11:27
FS2 cats.effect.Queue example using flatMap or for comprehension
View Fs2Queues.scala
import cats.effect._
import fs2._
import scala.concurrent.duration._
import cats.effect.std._
// FS2 cats.effect.Queue example using flatMap or for comprehension
// Both streams emit nothing, but are effectful, communicating via the queue and updating a sum via the ref
object Fs2Queues extends IOApp.Simple {