Skip to content

Instantly share code, notes, and snippets.

@lmuntaner
lmuntaner / app-use-state.js
Last active June 26, 2023 12:03
Building a React-Like Library - Part III: useState
const createElement = (tag, props, children ) => ({ tag, props, children });
const hooks = [];
let useStateCalls = -1;
const useState = (initialState) => {
useStateCalls += 1;
// Each hook is an array with two elements:
// - hook[0] is the state
// - hook[1] is the setState function
@lmuntaner
lmuntaner / index.html
Last active May 7, 2023 16:30
Building a React-Like Library
<html>
<body>
<div id="root"></div>
<!-- First article on building a react-like library -->
<script src="./static-dom.js"></script>
<body>
</html>
@lmuntaner
lmuntaner / app-final.js
Last active August 22, 2023 13:57
Building a React-Like Library - Part III: useState
const createElement = (tag, props, children ) => ({
tag,
props,
children,
element: null,
currentNode: null,
});
const hooks = [];
let useStateCalls = -1;
@lmuntaner
lmuntaner / gist:8f52ee13737aff93bfd997e3e9b76dce
Last active October 12, 2022 15:10
A Popular Example of Metaprogramming: ORMs
// Used in the article: https://www.gimtec.io/articles/metraprogramming-example-orm/
const pg = require('pg');
/**
* ORM Setup with metaprogramming
*/
const define = async (dbUrl, table) => {
try {
const getTableSchema = (table) => `select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='${table}';`;
const client = new pg.Client(dbUrl);
@lmuntaner
lmuntaner / app.js
Created April 29, 2022 12:42
Sluggish animations
// Long calculation
const getHighNumber = () => {
let x = BigInt(0);
for (let i = BigInt(0); i < BigInt(1_000_000); i++) {
x += i * i;
}
return x;
};
document.getElementById('sluggish').addEventListener('click', async () => {
@lmuntaner
lmuntaner / firstLesson.js
Created March 26, 2022 06:42
Timeout does not guarantee time to executoin
// Select all, copy-paste it in the console and try it out.
const expectedMilliseconds = 1000;
const startTime = new Date();
const logOne = () => console.log(1);
const logTwo = () => {
// This should be close to 0
console.log("Diff:", (new Date() - startTime) - expectedMilliseconds);
console.log(2);
}
@lmuntaner
lmuntaner / client.py
Created March 12, 2022 08:47
Sockets
from socket import AF_INET, SOCK_STREAM, socket
server_port = 12000
server_ip = 'localhost'
# Create a TCP socket
# AF_INET indicats undelying network IPv4
# SOCK_STREAM means TCP socket rather than UDP
# OS handles the port of the client
client_socket = socket(AF_INET, SOCK_STREAM)
# initiates TCP connection between client and server
# parameters are the address of the server
@lmuntaner
lmuntaner / test.js
Created January 26, 2022 11:29
Test Simple AST
const num = 10;
identifier := (A-Z,"_")
var_name := ":"<identifier>
command := "FD" | "BD" | "LT" | "RT" | "PEN"
positive_integer := \d+
expression := <positive_integer> | "UP" | "DOWN" | <var_name> | <positive_integer> ("+" | "-") <expression>
command_statement := (<command_key> | <identifier>) <expression>
loop_statement := "REPEAT" <positive_integer> { <command> } "END"
statement := <loop_statement> | <command_statement>
fun_definition := "TO" <identifier> [ <var_name> ] { "," <var_name> } { <statement> } "END"
@lmuntaner
lmuntaner / run_js.py
Last active December 7, 2021 12:48
NodeJS Competitor: RunJS 🚀
#!/usr/bin/python3
import dukpy
import sys
set_timeout_function = 'function setTimeout() { return; };'
if __name__ == '__main__':
filename = sys.argv[1]
(name, extension) = filename.split('.')
with open(filename, 'rt') as f: