Skip to content

Instantly share code, notes, and snippets.

@jtt9340
jtt9340 / slackbot.rs
Last active October 4, 2020 03:46
A basic example of using Rust to make a Slack bot that tells a (bad) knock-knock joke
//! Together we will build a Slack bot that makes a simple exchange between the user and the
//! bot. The user will first mention the bot, and then the bot will tell a knock-knock joke.
//!
//! Here is how the exchange will look.
//! 🙋‍♂️ *User:* `@bot` Tell me a joke
//! 🤖 *Bot:* Knock, knock.
//! 🙋‍♂️ *User:* Who's there?
//! 🤖 *Bot:* Underwear
//! 🙋‍♂️ *User:* Underwear who?
//! 🤖 *Bot:* Ever underwear you're going?
@jtt9340
jtt9340 / dimensional_analysis.rs
Created December 14, 2021 00:35
An exploration of using Rust's algebraic datatype-style enums to implement dimensional analysis
// Compile with: rustc --test angle.rs
/// This module explores the idea of using Rust's enums-as-algebraic-data-types to model
/// dimensional analysis. For a given quantity, each of the possible units is a variant of that
/// enum. Methods can convert between the different units, so a function expecting a certain unit
/// can just accept that quantity and call one of the unit-converting methods.
///
/// The inspiration for this module was [F#'s units of measure][fsuom], which is built into F#'s
/// type system. A unique feature of that implementation of dimensional analysis is that it can
/// understand multiplication and division of units, something that this implementation cannot. For
@jtt9340
jtt9340 / text.rs
Created December 14, 2021 01:25
Convenient text manipulation functions
// Compile with: rustc -L dependency=fastrand/target/release/deps --extern fastrand=fastrand/target/release/deps/libfastrand-0ce9eb3dc1d79119.rlib text.rs
extern crate fastrand;
use std::{env, io};
/// Given a string slice, return a string where each character in the given slice has a 60% chance
/// of being upper case, regardless of the case of the corresponding character in the original
/// string. Otherwise, the character is lower case.
///
@jtt9340
jtt9340 / pi.rs
Created September 7, 2022 02:08
A port of the "concurrent pi" Go example to both native threads and futures in Rust
use std::time::Instant;
use std::thread;
use std::sync::mpsc::channel;
use async_std::task;
use async_std::channel::unbounded;
const NUM_ITERATIONS: u32 = 5000;
#[allow(unused_variables)]
async fn async_pi(n: u32) -> f64 {
@jtt9340
jtt9340 / day_3.clj
Created January 8, 2023 03:12
Advent of Code 2022 Day 3 in Clojure
(ns aoc2022.day-3
(:gen-class)
(:require [clojure.set :as set])
(:import (java.lang Character)
(java.io BufferedReader)))
(defn letter->priority
"Get the priority of a letter"
[letter]
(let [ord (int letter)]