Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@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, "alice@example.com"),
(2, "bob@example.com"),
(3, "charlie@example.com");
@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
@alaindet
alaindet / data-source.ts
Created September 23, 2022 13:39
RxJS self-closing sources
import { BehaviorSubject, Observable, takeUntil } from 'rxjs';
import { OnceSource } from './once-source';
export type UpdaterFn<T> = (prev: T) => T;
/**
* # Data Source
*
* This wraps a data source in a RxJS BehaviorSubject and exposes an API for
@alaindet
alaindet / freeze-files.md
Created September 7, 2022 12:46
Freeze/unfreeze files

How to freeze files in Git

Frozen files are different that ignored files, so that

  • Ignored files are listed in .gitignore and Git never picks them up
  • Frozen files are previously committed files which are later ignored by Git

Frozen files can be configuration files that are frequently changed with secret values and should not be committed.

How to freeze files