Skip to content

Instantly share code, notes, and snippets.

View lmammino's full-sized avatar
🦁
Roar!

Luciano Mammino lmammino

🦁
Roar!
View GitHub Profile
@lmammino
lmammino / goto.js
Created May 3, 2022 18:29
If you ever wanted to use GOTO in JavaScript (for whatever crazy reason)...
// prints the first 100 even numbers and exits
let x = 0
LABEL1: do {
console.log(x)
x = x + 2
// JUMP TO THE END OF THE DO-WHILE - A FORWARDS GOTO
if (x > 100) break LABEL1
// JUMP TO THE START OF THE DO WHILE - A BACKWARDS GOTO...
if (x <= 100) continue LABEL1
} while (true)
@lmammino
lmammino / cp.js
Last active January 20, 2023 17:28
Session about Node.js streams with Kelvin
import { createReadStream, createWriteStream } from 'fs'
const source = createReadStream('./assets/moby-dick.txt')
const dest = createWriteStream('./assets/moby-dick-decompressed.txt')
source
.pipe(dest)
@lmammino
lmammino / Cargo.toml
Last active February 22, 2022 19:37
Decode an EU DGC (greenpass) in Rust
[package]
name = "dgc-decode"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
base45 = "3.0.0"
ciborium = "0.2.0"
@lmammino
lmammino / neighbours.rs
Created September 2, 2021 08:13
relative neighbour pixels in generic multidimensional spaces using const generics
fn neighbour_at<const D: usize>(i: u32) -> [i32; D] {
let mut el = [0_i32; D];
let mut n = i;
for v in el.iter_mut().rev() {
*v = (n as i32 % 3) - 1; // -1 is needed because we offset the digits so that we are in range -1..1
n /= 3;
}
el
@lmammino
lmammino / db.js
Created August 14, 2021 15:23
Sample Fastify Plugin to connect to MariaDB/MySql
'use strict'
const fp = require('fastify-plugin')
const mysql = require('mysql2/promise')
const CONNECTION_STRING = process.env.DB_CONN_STRING
module.exports = fp(async function (fastify, opts) {
const db = await mysql.createConnection(CONNECTION_STRING)
@lmammino
lmammino / example.ts
Created August 3, 2021 19:03
Get your public IP in TypeScript
import { getMyPublicIp } from './utils.ts'
getMyPublicIp
.then(console.log)
.catch(console.error)
@lmammino
lmammino / dependant.js
Created June 29, 2021 17:32
Modulino pattern in Node.js
const x = require('./index.js')
@lmammino
lmammino / server.js
Created June 22, 2021 13:24
Node.js debug server
const { createServer } = require('http');
createServer((req, res) => {
console.log('--------');
console.log(JSON.stringify(req.headers, null, 2));
req.pipe(process.stdout);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ok:true}));
})
.listen(8000);
@lmammino
lmammino / event_listener.py
Last active June 18, 2021 08:24
Gzipping payload for custom metrics to cloudwatch with boto3
import boto3
import gzip
cw_client = boto3.client('cloudwatch')
event_system = cw_client.meta.events
def gzip_request_body(request, **kwargs):
gzipped_body = gzip.compress(request.body)
request.headers['Content-Encoding'] = 'gzip'
request.data = gzipped_body
@lmammino
lmammino / lib.rs
Last active May 12, 2021 14:14
Ex 11, year 2020 part 1 & part 2
use std::fmt;
use std::str::FromStr;
// [x - 1, y - 1] [ x , y - 1] [x + 1, y - 1]
// [x - 1, y ] (current) [x + 1, y ]
// [x - 1, y + 1] [ x , y + 1] [x + 1, y + 1]
const DIRECTIONS: [(i8, i8); 8] = [
(-1, -1),
(0, -1),
(1, -1),