This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
from abc import ABC, abstractmethod | |
from contextlib import contextmanager, nullcontext | |
from typing import ( | |
TYPE_CHECKING, | |
Any, | |
Callable, | |
ContextManager, | |
Generic, | |
Iterator, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
NewerOlder