Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@alaindet
alaindet / game.mjs
Created October 14, 2024 15:10
[WIP] CLI RPG v1.0
import { createInterface } from "node:readline";
const DIRECTION_AHEAD = "ahead";
const DIRECTION_LEFT = "left";
const DIRECTION_RIGHT = "right";
const DIRECTION_BACK = "back";
const MOVE_ATTACK = "attack";
const MOVE_FLEE = "flee";
@alaindet
alaindet / cached-promises.js
Created September 28, 2024 12:33
Cached Promises
// Thanks to
// https://github.com/microsoft/tsyringe/issues/66#issuecomment-566755746
// https://262.ecma-international.org/6.0/#sec-promise-resolve-functions
main();
async function main() {
const rnd = getRandom();
console.log(await rnd);
console.log(await rnd);
@alaindet
alaindet / range.js
Created September 15, 2024 23:14
Python's `range` in JavaScript
// This is a generator function
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
function* range(minOrMax: number, max?: number) {
const maxExists = max !== undefined;
const inf = maxExists ? minOrMax : 0;
const sup = maxExists ? max : minOrMax;
for (let i = inf; i < sup; i++) {
yield i;
}
@alaindet
alaindet / visitor-pattern.ts
Last active August 21, 2024 14:27
Visitor Pattern in TypeScript
interface Visitor<TElementTypes = any> {
visit(element: TElementTypes): void;
}
interface ConcreteItem {
accept(visitor: Visitor): void;
}
class ConcreteItemA implements ConcreteItem {
accept(visitor: Visitor): void {
@alaindet
alaindet / main.go
Created July 6, 2024 21:23
Go exercise #1
/**
* What does this script output? Watch out!
*/
package main
import "fmt"
type mystring string
func (s mystring) String() string {
@alaindet
alaindet / many-to-many.sql
Last active April 6, 2024 16:53
SQLite: Example for putting a many-to-many relationship in a single column
CREATE TABLE "users" (
"id" INTEGER NOT NULL UNIQUE,
"email" TEXT NOT NULL UNIQUE,
PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO "users" ("id", "email") VALUES
(1, "[email protected]"),
(2, "[email protected]"),
(3, "[email protected]");
@alaindet
alaindet / benchmarks_test.go
Created January 8, 2024 16:36
Go slice mapping concurrently
package main
import "testing"
func BenchmarkConcMap100(b *testing.B) {
b.StopTimer()
input := createRange(100)
output := make([]int, 0, len(input))
b.StartTimer()
@alaindet
alaindet / loop-over-promises.js
Last active November 14, 2023 15:03
Loop over Promises
/**
* How can you loop on promises, really?
*
* This experiment tests how promises and loops interact in JavaScript
*
* TL;DR: for await...of is the clear winner
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
*/
run(); // <-- Start here
@alaindet
alaindet / index.html
Created April 21, 2023 13:05
YALL: Yet Another Layout Library
<!--
Reference
https://codepen.io/alaindet/pen/MWmxrJb
-->
<h1>YALL: Yet Another Layout Library</h1>
<p>
This whole library weights about 5 Kb minified (less than 1 Kb gzipped) and serves the only purpose of <strong>helping you place elements on the page</strong>. YALL provides a responsive grid and a set of utility classes for spacing. That's it. No colors, no components, no typography. Just -SPACE-!
</p>
@alaindet
alaindet / ts-better-enums.ts
Last active November 14, 2023 14:22
TypeScript better enums
//
// Thanks to Matt Pocock's video "Enums considered harmful"
// https://www.youtube.com/watch?v=jjMbPt_H3RQ
//
// Please note that enums per se are great in any language,
// but TypeScript's enums are implemented in a convoluted and verbose way (as of version 4.9)
// since JavaScript (as of ES2022) does not natively support enums
//
// This can be moved elsewhere