This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::Arc; | |
use tokio::sync::Mutex; | |
// async tokio function to increment i32 behind arc mutex | |
async fn increment(remote: Arc<Mutex<i32>>) { | |
println!("trying to lock"); | |
let mut tvc = remote.lock().await; | |
println!("incremented"); | |
*tvc += 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::Deserialize; | |
use std::time::Duration; | |
#[derive(Deserialize, Debug)] | |
struct Product { | |
id: i32, | |
title: String, | |
description: String, | |
price: f64, | |
#[serde(rename = "discountPercentage")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
use std::thread; | |
use std::time::Duration; | |
struct Fork { | |
id: usize, | |
mutex: Mutex<()>, | |
} | |
impl Fork { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rand::Rng; | |
use rasciigraph::{plot, Config}; | |
fn main() { | |
let vec = (0..70) | |
.map(|_| rand::thread_rng().gen::<f64>() * 13.0 - 3.0) | |
.collect::<Vec<_>>(); | |
let plot = plot( | |
vec, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use chrono::prelude::*; | |
fn flexi_data_parse(text: &str) -> Option<NaiveDate> { | |
let text = text.replace(['.', '/'], "-"); | |
let fmts = ["%Y-%m-%d", "%Y-%b-%d", "%d-%b-%Y", "%b-%d-%Y", "%d-%m-%Y"]; | |
for fmt in fmts { | |
let parser = NaiveDate::parse_from_str(text.as_str(), fmt); | |
if parser.is_ok() { | |
return parser.ok(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::Display; | |
use std::str::FromStr; | |
#[derive(Debug)] | |
struct Rle { | |
data: Vec<u8>, | |
} | |
impl Rle { | |
fn decode(&self) -> String { |