Skip to content

Instantly share code, notes, and snippets.

View lffg's full-sized avatar

Luiz Felipe Gonçalves lffg

View GitHub Profile
macro_rules! cat {
($($str:expr),*) => {{
const LEN: usize = $($str.len() + )* 0;
to_str(&combine::<LEN>(&[$($str),*]))
}};
}
const fn combine<const LEN: usize>(list: &[&'static str]) -> [u8; LEN] {
let mut bytes = [0; LEN];
let mut i = 0;
function query(label) {
console.log(`(${label}) created 'lazy promise', but not started yet`);
return {
then(r) {
console.log(`(${label}) started!`);
// ... process query
// "return" them:
r([{ data: `result for ${label}` }]);
},
};
use std::collections::BTreeMap;
fn group<'a>(data: impl Iterator<Item = (&'a str, u32)>) -> BTreeMap<&'a str, u32> {
data.fold(BTreeMap::new(), |mut map, (date, amount)| {
*map.entry(date).or_default() += amount;
map
})
}
#[test]
@lffg
lffg / asm.md
Last active June 14, 2023 23:40

Basics of RISC-V

  • Notes.
    • See An Introduction to Assembly Programming with RISC-V.
    • ISA (Industry Standard Architecture) is the portion of the architecture which is visible to compiler writers. I.e., a contract.
    • There are 33 general-purpose unprivileged registers:
      • zero
      • t0 ~ t6 for temporary values, which are not to be persisted across calls.
      • s0 ~ s11 for saved values.
  • a0 ~ a7 for arguments and return values.
use std::{
io::{Cursor, Seek, SeekFrom},
ops::RangeBounds,
};
/// Tries to get a UTF-8 string from the given buffer at the provided range.
fn str_from_utf8_range(buf: &[u8], range: impl RangeBounds<usize>) -> Option<&str> {
buf.get((range.start_bound().cloned(), range.end_bound().cloned()))
.map(|slice| std::str::from_utf8(slice).expect("valid utf8"))
}
@lffg
lffg / bs.rs
Created November 2, 2022 00:21
use std::cmp::Ordering;
pub mod printer;
pub struct BinaryTree<T> {
root: Option<Node<T>>,
}
impl<T: Ord> BinaryTree<T> {
/// Creates a new binary tree.
experimental-features = nix-command flakes
build-users-group = nixbld
substituters = https://cache.nixos.org https://cachix.cachix.org https://nix-tools.cachix.org https://nix-community.cachix.org https//cache.nixos.org https://cachix.cachix.org https://nix-tools.cachix.org https://nix-community.cachix.org https://cache.nixos.org/
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cachix.cachix.org-1:eWNHQldwUO7G2VkjpnjDbWwy4KQ/HNxht7H4SSoMckM= nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=
trusted-substituters = https://cache.nixos.org https://cachix.cachix.org https://nix-tools.cachix.org https://nix-community.cachix.org
import { readdirSync } from "node:fs";
import { join } from "node:path";
import { cwd } from "node:process";
const MIGRATION_DIR = join(cwd(), "migrations");
const subDirsPaths = (path) =>
readdirSync(path, { withFileTypes: true })
.filter((dirEntry) => dirEntry.isDirectory())
.map((dirEntry) => join(path, dirEntry.name));
// Write a script that creates an array with 10000 random words between 3 and 5
// characters, and returns the number of words that are palindromes in that
// array. Notes: The code needs to be in javascript You’ll need to return just
// the number of words.
console.log(main());
function main() {
return Array.from({ length: 10_000 })
.map(RandomAlphabeticString(3, 5))
function* enumerate(iterable) {
let i = 0;
for (const item of iterable) {
yield [i++, item];
}
}
function* withIsLast(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr = iterator.next();