Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / clinical.py
Created October 4, 2023 12:05
click but with nice types
import copy
from abc import ABC, abstractmethod
from contextlib import contextmanager, nullcontext
from typing import (
TYPE_CHECKING,
Any,
Callable,
ContextManager,
Generic,
Iterator,
@cqfd
cqfd / dumb.rs
Created November 27, 2021 01:18
pub fn goofy(ctx: Context<Goofy>) -> ProgramResult {
// Manually pay the fresh account some money
**ctx
.accounts
.payer // some account owned by us, with some money
.to_account_info()
.try_borrow_mut_lamports()? -= 2000000; // random big-enough amount
**ctx.accounts.new_account.try_borrow_mut_lamports()? += 2000000;
// Allocate the new_account some space (it has money now).
@cqfd
cqfd / pubkey.rs
Created October 10, 2021 00:04
Pubkey solana/anchor macro
#[proc_macro]
pub fn pubkey(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let s = parse_macro_input!(input as LitStr);
let pk: Pubkey = s.value().parse().unwrap();
let bytes = pk.to_bytes().map(|b| LitByte::new(b, Span::call_site()));
let output = quote! {
anchor_lang::solana_program::pubkey::Pubkey::new_from_array([#(#bytes,)*])
};
output.into()
}
module Mappers
data Identity a = MkIdentity a
Functor Identity where
map f (MkIdentity x) = MkIdentity (f x)
repapply : Nat -> (a -> a) -> (a -> a)
repapply Z f = id
repapply (S k) f = f . repapply k f
@cqfd
cqfd / gist:6b1a888db8130d36fa37a6e668c2958b
Created June 28, 2020 14:32
React + generator workflows
function NumberPicker({ onResult }: { onResult: (x: number) => void }) {
const inputEl: any = useRef(null)
return (
<React.Fragment>
<label>
Enter a number: <input type="number" ref={inputEl} />
</label>
<button onClick={() => onResult(Number(inputEl.current.value))}>
Ok
</button>
@cqfd
cqfd / preds.cpp
Last active August 22, 2018 23:52
Type predicates.
template <typename T, typename U>
struct Or : std::bool_constant<T::value || U::value> {};
template <template<typename> class Predicate, typename... Ts>
struct any : false_type {};
template <template<typename> class Predicate, typename T, typename... Ts>
struct any<Predicate, T, Ts...> : Or<Predicate<T>, any<Predicate, Ts...>> {};
template <typename T> struct is_std_vector : false_type {};
template <typename T> struct is_std_vector<vector<T>> : true_type {};
@cqfd
cqfd / stan.cpp
Last active August 20, 2018 15:01
Some Stan template examples.
// Variable templates
// There are some situations where they're not as flexible maybe as structs with boolean value members
// but kind of nice if all you're doing is type-level conditionals, since you don't need the ::value noise anymore.
template <typename T> constexpr bool is_constant = boost::is_convertible<T, double>::value;
// Variable templates work with partial specialization
template <typename T> constexpr bool is_constant_struct = is_constant<T>;
template <typename T> constexpr bool is_constant_struct<std::vector<T>> = is_constant_struct<T>;
template <typename T, int R, int C> constexpr bool is_constant_struct<Eigen::Matrix<T, R, C>> = is_constant_struct<T>;
@cqfd
cqfd / tricky.cpp
Last active August 8, 2018 19:31
Fun C++ lambda example
#include <iostream>
int g = 0;
auto f = [g=g](auto x) mutable {
static auto count = 0;
std::cout << "count: " << ++count << std::endl;
std::cout << "g: " << ++g << std::endl;
};
@cqfd
cqfd / particles.stan
Created July 31, 2018 21:38
Stan model for MacKay's Chapter 3 particle detector example.
data {
int N;
real<lower=1, upper=20> observations[N];
}
parameters {
real<lower=0> lambda;
}
model {
real Z = exp(-1/lambda) - exp(-20/lambda);
for (i in 1:N) {
@cqfd
cqfd / Monoid.cpp
Created July 28, 2018 03:06
C++ experiments
template <typename T>
struct LinkedList {
T head;
LinkedList *tail;
};
template<typename Monoid>
typename Monoid::T fold(LinkedList<typename Monoid::T> *l) {
typename Monoid::T acc = Monoid::mempty();
while (l) {