Skip to content

Instantly share code, notes, and snippets.

@ckampfe
ckampfe / begin_immediate.rs
Created July 19, 2024 18:47
begin immediate support for SQLite in sqlx
use sqlx::sqlite::SqliteQueryResult;
use sqlx::{Executor, SqliteConnection};
use std::future::Future;
use std::ops::{Deref, DerefMut};
pub(crate) trait SqliteConnectionExt {
fn begin_immediate(&mut self) -> impl Future<Output = sqlx::Result<Transaction>>;
}
impl SqliteConnectionExt for SqliteConnection {
#!/usr/bin/env awk
BEGIN {
print "principal", "interest_rate", "term", "monthly_payment", "total_paid", "interest_paid"
}
NR == 1 && NF == 0 {
print "use:"
print "\tawk -f amortize"
CREATE TABLE f (id integer primary key, value text, inserted_at default current_timestamp);
INSERT INTO f VALUES(1,'{"a":1}','2022-05-16 04:24:48');
INSERT INTO f VALUES(2,'{"a":2}','2022-05-16 04:25:11');
INSERT INTO f VALUES(3,'{"a":2, "b":3}','2022-05-16 04:25:15');
INSERT INTO f VALUES(4,'{"a":2, "b":99}','2022-05-16 04:25:18');
INSERT INTO f VALUES(5,'{"a":3}','2022-05-16 04:25:32');
g = :digraph.new()
v1 = :digraph.add_vertex(g, "v1")
v2 = :digraph.add_vertex(g, "v2")
v3 = :digraph.add_vertex(g, "v3")
v4 = :digraph.add_vertex(g, "v4")
:digraph.add_edge(g, v2, v1)
:digraph.add_edge(g, v3, v2)
:digraph.add_edge(g, v4, v2)
@ckampfe
ckampfe / get_in.rs
Last active September 21, 2021 01:07
access a deeply nested enum/map combo
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
pub enum Bencode<'a> {
String(&'a [u8]),
Integer(isize),
List(Vec<Bencode<'a>>),
Dictionary(BTreeMap<&'a [u8], Bencode<'a>>),
}
#[macro_export]
macro_rules! get_in {
@ckampfe
ckampfe / example.exs
Created May 20, 2021 21:52
elixir scripting
#!/usr/bin/env elixir
Mix.install(httpoison: "~> 1.0", floki: "~> 0.30")
%_{body: body} = HTTPoison.get!("https://fastradius.com", [], follow_redirect: true)
body
|> Floki.parse_document!()
|> Floki.find("a")
|> Floki.attribute("href")
@ckampfe
ckampfe / lib.rs
Last active April 25, 2021 01:59
rust const Sieve of Eratosthenes
pub fn primes<const N: usize>() -> Vec<usize> {
let primes_array = sieve_of_eratosthenes::<N>();
std::array::IntoIter::new(primes_array)
.enumerate()
.filter_map(|(i, is_prime)| if is_prime { Some(i + 2) } else { None })
.collect()
}
pub const fn sieve_of_eratosthenes<const N: usize>() -> [bool; N] {
@ckampfe
ckampfe / bench.exs
Last active September 25, 2022 19:55
regex_str = "[a-z]+\\s+\\d+"
small_hit_str = "abc 123 def 456"
big_hit_str = Enum.reduce(1..1000, "", fn _val, acc ->
"#{acc}#{small_hit_str}"
end)
small_miss_str = "abc abc abc abc"
big_miss_str = Enum.reduce(1..1000, "", fn _val, acc ->
"#{acc}#{small_miss_str}"
end)
#!/usr/bin/env bash
last_n=$1
commits=$(git log -n "$last_n" | awk '$1 == "commit" { print $2 }')
for commit in $commits
do
git checkout "$commit" >/dev/null 2>&1
@ckampfe
ckampfe / main.rs
Last active September 15, 2020 23:24
use serde::Deserialize;
use std::error::Error;
#[derive(Debug, Deserialize)]
struct ABView {
a: usize,
b: String,
}
#[derive(Debug, Deserialize)]