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 / Generators.md
Last active November 7, 2022 20:53
Generators in JavaScript and Haskell

ES6. Using yield and yield*.

function* a() {
  var i = yield "first thing from a";
  var j = yield "second thing from a";
  return [i, j];
}

function* b() {
@cqfd
cqfd / the_ascii_monad.txt
Created July 28, 2011 01:12
Monads, explained with Ascii art.
Monads are composed of three ingredients.
First, for a given monad m, there are monadic values of type m a, for some type
variable a. In Haskell, if a variable x has type t, we write x :: t.
Here's a picture of a monadic value of type m a.
+--------\
| | \
| m | a > :: m a
import RxSwift
import RxSwiftExt
class Signal<E>: ObservableConvertibleType {
private let _out: Variable<E>
var value: E { return _out.value }
init(out: Variable<E>) {
_out = out
}
func map<R>(selector: E -> R) -> Signal<R> {
@cqfd
cqfd / memoization.rb
Created July 10, 2013 16:41
Ruby memoization exercise
require 'minitest/autorun'
module Memoization
def memoize(f, injected_cache={})
m = instance_method(f)
define_method(f) do |*args|
cache = injected_cache.clone
define_singleton_method(f) do |*args|
unless cache[args]
puts "Actually calculating..."
@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 / learn-c-with-gdb.org
Created August 17, 2012 00:02
Learning c with gdb

High-level

I want to write about why GDB is a great tool for learning C. At least part of the difficulty in learning C is that the language isn’t as interactive as using Python or Ruby.

TL;DR You can kinda use gdb as a repl for c

What’s the smallest possible program we could debug to learn about pointers?

@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>