Skip to content

Instantly share code, notes, and snippets.

@fvilante
fvilante / dynamic_dispatch.cpp
Created August 11, 2023 11:50
C11 abstractions to emulate some Rust features
#include <stdio.h>
typedef double (*MathOperation)(double, double);
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
@fvilante
fvilante / closure_with_sugar.rs
Last active June 27, 2023 21:43
Closure Under de Hood in Rust
fn call_once<F: FnOnce(i32) -> i32>(f: F, x: i32) -> i32 {
f(x)
}
fn main() {
let outer = 10;
let add_outer = |x| x + outer;
let result = call_once(&add_outer, 66);
@fvilante
fvilante / convetion.rs
Last active April 19, 2023 17:04
How to convert enum discriminant to trait objects
use std::fmt::Display;
enum Storage {
Number(u32),
Text(String),
}
struct Data {
storage: Storage
@fvilante
fvilante / closure.rs
Last active January 12, 2023 22:23
Sketch of a Closure-like mecanism in no_std rust
// -----
struct Wrapper<A: Copy> {
value: A
}
impl<A: Copy> Wrapper<A> {
fn new(value: A) -> Self {
Self {
@fvilante
fvilante / profunctor.rs
Created October 1, 2022 14:28
Rust Profunctor designed for no_std embedded systems
extern crate alloc;
use alloc::boxed::Box;
struct Profunctor<'a,A,B> {
f: Box<dyn FnMut(A) -> B + 'a>
}
impl<'a,A, B> Profunctor<'a, A, B> {
fn new(f: impl FnMut(A) -> B + 'a) -> Self {
Self {
@fvilante
fvilante / eeprom.rs
Last active September 12, 2022 14:09
Low level function to read/write EEPROM on avr328p 8-bits microcontroler
// author: @FlavioVilante
// MIT License - Copyright 2022
use ruduino::prelude::without_interrupts;
use crate::microcontroler::register:: {
write_register,
read_register,
};
@fvilante
fvilante / github_repolines.js
Created August 6, 2022 07:27
Get the estimated size (in number of lines) of a given GitHub repository
// to be used with deno run-time
// the first argument must be the repo name
// example: deno run --allow-net .\github_repolines.js microsoft/typescript
async function countGithub(repo) {
const repoAddress = `https://github.com/${repo}`;
const repoApi = `https://api.github.com/repos/${repo}` + `/stats/contributors`;
const response = await fetch(repoApi);
const contributors = await response.json();
const lineCounts = contributors.map(contributor => (
@fvilante
fvilante / powershel.ps1
Last active May 27, 2022 12:03
powershell tips and tricks
#Get help
Get-Help Sort-Object
#Old dir
dir
Get-ChildItem
Get-ChildItem *.txt
Get-ChildItem -Directory
Get-ChildItem -File
@fvilante
fvilante / deno-deploy-jsx-sample.tsx
Last active September 1, 2021 17:45
deno deploy - using jsx
// You need to import `h` factory function as Deno Deploy
// uses it instead of `React.createElement`
import { h } from "https://x.lcas.dev/preact@10.5.12/mod.js";
import { renderToString } from "https://x.lcas.dev/preact@10.5.12/ssr.js";
let elapsedTime = 0
setInterval( () => { elapsedTime = elapsedTime + 1; }, 1000)
function App() {
@fvilante
fvilante / TDD_with_types.ts
Last active October 28, 2020 07:04
Unit Test With Types - Experimental
// ========================================================================================
// Utilities types
// ========================================================================================
type TU11 = '(T extends U) and (U extends T)'
type TU10 = '(T extends U) and (U NOT extends T)'
type TU01 = '(T NOT extends U) and (U extends T)'
type TU00 = '(T NOT extends U) and (U NOT extends T)'
type Ext<T,U,A,B> = T extends U ? A : B // In type system jargon we can say that "T is assignable to U".