This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Visitor<TElementTypes = any> { | |
visit(element: TElementTypes): void; | |
} | |
interface ConcreteItem { | |
accept(visitor: Visitor): void; | |
} | |
class ConcreteItemA implements ConcreteItem { | |
accept(visitor: Visitor): void { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* What does this script output? Watch out! | |
*/ | |
package main | |
import "fmt" | |
type mystring string | |
func (s mystring) String() string { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func BenchmarkConcMap100(b *testing.B) { | |
b.StopTimer() | |
input := createRange(100) | |
output := make([]int, 0, len(input)) | |
b.StartTimer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
NewerOlder