Skip to content

Instantly share code, notes, and snippets.

View alii's full-sized avatar
🗿
compuoter

Alistair Smith alii

🗿
compuoter
View GitHub Profile
@futurGH
futurGH / bubbleSort.ts
Last active March 7, 2024 00:10
A TypeScript type-only bubble sort
// https://www.typescriptlang.org/play#code/C4TwDgpgBAyg9gJ2BAJlAvFAQgVwEZ4A2E8SAPANoCMANFACx21QDMdATHQLQCcdAHHQDs3TlABsdRlCrSu7ZlUEzmQtlHbsAugD4AsACgA9EahmAegH5DhkyahdHT5y65YAqliwAZAKKwAeQAlABVXcKcbA1BIbHwiEkRgMgA5KAgAD2QAOxQAZyhsnABbPAgECl0MQzNcAmJSYAAFAEM8vNSddKyIXIKAS2yAM3KoAOyIVvbunPzCkrKKrRqzKEs4+sSkKY7xyba8rszZgrqExp3OldW1jfOky5T9AxuzAC47hqSyPZ3nm4+EwAbuUojFoGcvtsDqkZr05kVSuVKl10Cs0sd4QUKIMRggoAAxfoIPLAOF9eZIhB0XGjGAQADGcFy5IRC3KdAAdNzafighBSayCojFpVli9Vut+YLMRTKtdJVBvAK8iEABYtbJkIkk4B0elM3JHHoU4AIHAQBU3dYUHWk-WM5kocWvVYfCgGp10O3AF2vD7K9rqzXa4n22COo1CqBmi1WxW2sN6qDczmQrbNGEeyMoLnc6W+nR+13uz25PNp+JQzPtSg+isF3TFqCAiAghBROymCLhdwhACS3n7IQAmlBR01fDAe64weBoIHVRqtQBBaMijnYdfshColZr2VzLAK9ZDFqEPKWiXvKAAAwAJABvFcAX1v0dvXCfvKgK7weQPE02SpN943WB9HywN8Py-R8fz-PIsG3EDb3jMx1kXYNV3-LA6AQlcunsNcyCgAAGOgkJI0i0JbGNzWgIioCoiioB0TBqOvN07yfKD30PApPyfDcEFAzj0KgM8LwY0w1zYsiWKo+MPhgfAzRaBlkhXCjjROQo21GewkK6FcaPWWMr1dG9JMvOdYhXFAUDIQDdOElj+MpRZUSgChUxCHAwGIAIhmVbIAHNgDVJydArPyAogIKQvCyKsCLCgACJiDCiK0q0ABuWzoBUvA1I0pzkMWNygOFHc
@bfu4
bfu4 / ObtuseReflection.java
Last active October 10, 2021 21:47
aids way to construct an object from a class and argument type fuck box types fr
public <T> T makeObject(Class<T> clazz, Object... args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] types = Arrays.stream(args).map(cls -> getPrimitiveType(cls.getClass())).toArray(Class[]::new);
Constructor<T> constr = getConstructor(clazz, types);
constr.setAccessible(true);
return constr.newInstance(args);
}
@SuppressWarnings("unchecked")
public <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... types) {
return (Constructor<T>) Arrays.stream(clazz.getDeclaredConstructors())
@sindresorhus
sindresorhus / esm-package.md
Last active July 22, 2024 10:50
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@dustinrouillard
dustinrouillard / snowflake.sql
Created January 17, 2021 02:34
PostgreSQL Snowflake ID Generator Function
CREATE SEQUENCE IF NOT EXISTS public.global_id_sequence;
CREATE OR REPLACE FUNCTION id_generator(OUT result BIGINT) AS $$
DECLARE
epoch BIGINT := 1610850820000;
seq_id BIGINT;
now_millis BIGINT;
shard_id INT := 1;
BEGIN
SELECT nextval('public.global_id_sequence') % 1024 INTO seq_id;
@Rich-Harris
Rich-Harris / footgun.md
Last active July 8, 2024 03:54
Top-level `await` is a footgun

Edit — February 2019

This gist had a far larger impact than I imagined it would, and apparently people are still finding it, so a quick update:

  • TC39 is currently moving forward with a slightly different version of TLA, referred to as 'variant B', in which a module with TLA doesn't block sibling execution. This vastly reduces the danger of parallelizable work happening in serial and thereby delaying startup, which was the concern that motivated me to write this gist
  • In the wild, we're seeing (async main(){...}()) as a substitute for TLA. This completely eliminates the blocking problem (yay!) but it's less powerful, and harder to statically analyse (boo). In other words the lack of TLA is causing real problems
  • Therefore, a version of TLA that solves the original issue is a valuable addition to the language, and I'm in full support of the current proposal, which you can read here.

I'll leave the rest of this document unedited, for archaeological

@jboner
jboner / latency.txt
Last active July 22, 2024 11:30
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD