Skip to content

Instantly share code, notes, and snippets.

Avatar
🦀
Programming something in Rust, probably

Matthew J. Berger matthewjberger

🦀
Programming something in Rust, probably
View GitHub Profile
@matthewjberger
matthewjberger / jarvis.py
Last active March 27, 2023 00:32
A british voice assistant natural speech frontend to GPT-3
View jarvis.py
"""
GPT-3 British Voice Assistant
This project is a simple voice assistant that takes your natural speech as input
and returns a response generated by the GPT-3 AI model. The response is then
read out loud with a British accent.
Installation:
1. Create a virtual environment and activate it:
View main.rs
fn main() {
let s = "system/{id}/start/{mode}";
let substrings = extract_substrings(s);
assert_eq!(substrings, vec!["id", "mode"]);
let removed = remove_substrings(&s, &substrings);
assert_eq!(removed, "system/{}/start/{}");
}
fn extract_substrings(s: &str) -> Vec<&str> {
View main.rs
fn main() {
let input = "UpperCamelCase";
let snake_case = to_snake_case(input);
println!("{}", snake_case);
}
fn to_snake_case(input: &str) -> String {
input
.chars()
.enumerate()
View numbered-placeholders.rs
fn main() {
let input = "Some string {} another value {} third value {}";
let output = numbered_placeholders(input);
let expected_output = "Some string {0} another value {1} third value {2}";
assert_eq!(output, expected_output);
}
fn numbered_placeholders(s: &str) -> String {
let mut counter = 0;
let mut result = String::new();
View adjacency_list.rs
/// A compact graph representation. Edges are numbered in order of insertion.
/// Each adjacency list consists of all edges pointing out from a given vertex.
pub struct Graph {
/// Maps a vertex id to the first edge in its adjacency list.
first: Vec<Option<usize>>,
/// Maps an edge id to the next edge in the same adjacency list.
next: Vec<Option<usize>>,
/// Maps an edge id to the vertex that it points to.
endp: Vec<usize>,
}
@matthewjberger
matthewjberger / test.rs
Created February 17, 2023 02:00
Shepmaster's rust test timeout solution
View test.rs
use std::{sync::mpsc, thread, time::Duration};
#[test]
fn oops() {
panic_after(Duration::from_millis(100), || {
thread::sleep(Duration::from_millis(200));
})
}
fn panic_after<T, F>(d: Duration, f: F) -> T
@matthewjberger
matthewjberger / most-lines-added.ps1
Created February 16, 2023 08:05
Get the commit for a specific user that added more lines than all other commits
View most-lines-added.ps1
git log --shortstat --author="Matthew J. Berger" --pretty=format:"%H - %an, %ar : %s" | foreach {
if ($_ -match '([0-9a-fA-F]{40})') {
$commitHash = $matches[1]
}
if ($_ -match '(\d+)\s+insertion\S*.*(\d+)\s+deletion\S*') {
$insertions = [int]$matches[1]
$deletions = [int]$matches[2]
[pscustomobject]@{
CommitHash = $commitHash
Insertions = $insertions
@matthewjberger
matthewjberger / Cargo.toml
Last active February 11, 2023 22:27
Async winit
View Cargo.toml
[package]
name = "window"
version = "0.1.0"
edition = "2021"
[dependencies]
winit = "0.28.1"
async-std = "1.10.0"
@matthewjberger
matthewjberger / layered.rs
Last active February 11, 2023 02:01
layered architecture in rust
View layered.rs
// Layer 1: Domain
#[derive(Clone)]
pub struct User {
pub id: i32,
pub name: String,
pub email: String,
}
// Layer 2: Data Access
pub trait UserRepository {
@matthewjberger
matthewjberger / udev.sh
Created February 8, 2023 15:01
Monitor udev
View udev.sh
udevadm monitor