Skip to content

Instantly share code, notes, and snippets.

Avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / parser.rs
Created Nov 24, 2021
An example nom parser for reddit
View parser.rs
use nom::{
branch::alt,
character::complete::{alpha1, char, digit1, multispace1},
combinator::eof,
IResult, Parser,
};
use nom_supreme::{error::ErrorTree, multi::collect_separated_terminated, ParserExt};
#[derive(Debug, Clone)]
struct Structure<'a> {
@Lucretiel
Lucretiel / lisplusplus.cpp
Created Oct 19, 2021
Demo of lisp implemented by overloading the comma operator
View lisplusplus.cpp
#include "liscpp.h"
using namespace std;
int main()
{
ReadEvalPrint(
(Let,
tok(fold), (Lambda, tok(F), tok(I), tok(L),
@Lucretiel
Lucretiel / fetch_paginated.js
Last active Oct 6, 2021
Utility that fetches from a paginated resource by repeatedly fetching & concatenating pages
View fetch_paginated.js
// Fetch several rows from 1 or more pages from an API. Concatenate all
// the rows together.
function fetch_paginated(
// The URL to initiate the query
initial_url,
// A fetch function that retrieves a single page. Intended to wrap a fetch()
// API call (so that you can add your own headers, auth, response parsing,
// etc)
fetcher,
@Lucretiel
Lucretiel / fetch_paginated.ts
Last active Nov 6, 2021
Utility that fetches from a paginated resource by repeatedly fetching & concatenating pages
View fetch_paginated.ts
// Fetch several rows from 1 or more pages from an API. Concatenate all
// the rows together.
function paginated<Request, Response, Row>(
// A fetch function that retrieves a single page. Intended to wrap a fetch()
// API call (so that you can add your own headers, auth, response parsing,
// etc)
fetcher: (url: Request, cancel?: AbortSignal) => Promise<Response>,
// Given a response, get the URL of the next page, or null if this was the
// last page.
@Lucretiel
Lucretiel / atomic_transaction.rs
Created Aug 26, 2021
An example of a function that processess an atomic as a transaction, using 0 to indicate a locked state
View atomic_transaction.rs
pub fn use_atomic(value: &AtomicU64, op: impl FnOnce(NonZeroU64) -> NonZeroU64) {
loop {
let current = value.swap(0, Ordering::AcqRel);
match NonZeroU64::new(current) {
Some(current) => {
let computed = op(current);
// It might be more appropriate to use compare_exchange here
value.store(computed.get(), Ordering::Release);
break;
View generate_demo.rs
fn main() {
let data = vec![1, 2, 3, 4];
let iter = generate::generate![1, 2, ..data];
let data: Vec<i32> = iter.collect();
println!("{:#?}", data);
}
@Lucretiel
Lucretiel / retry_on_panic.rs
Last active May 5, 2021
A future adapter that retries the future if it panics
View retry_on_panic.rs
use std::{
future::Future,
panic::{catch_unwind, UnwindSafe},
pin::Pin,
task::{Context, Poll},
};
use pin_project::pin_project;
#[pin_project]
View percent-cow.rs
impl<'i, B> FromPercentEncoded<'i> for Cow<'i, B>
where
B: ToOwned,
&'i B: FromPercentEncoded<'i>,
<&'i B as FromPercentEncoded<'i>>::StrError: MaybeCannotGrowError,
<&'i B as FromPercentEncoded<'i>>::BytesError: MaybeCannotGrowError,
B::Owned: FromPercentEncoded<'i>,
{
type BytesError = CowError<
<B::Owned as FromPercentEncoded<'i>>::BytesError,
@Lucretiel
Lucretiel / webtoon-enhance.js
Created Apr 26, 2021
Enhancer script for webtoons.com
View webtoon-enhance.js
// ==UserScript==
// @name Webtoon Enhancer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.webtoons.com/en/*
// @icon https://www.google.com/s2/favicons?domain=webtoons.com
// @grant none
// @require https://raw.githubusercontent.com/Lucretiel/comics-enhance/master/enhance.js
@Lucretiel
Lucretiel / autodefault_demo.rs
Created Apr 10, 2021
Example code using autodefault
View autodefault_demo.rs
use autodefault::autodefault;
#[derive(Debug, Default)]
struct Big1 {
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
f: i32,