Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
sindresorhus / esm-package.md
Last active May 8, 2024 22: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.
@pesterhazy
pesterhazy / promises-cljs.md
Last active October 10, 2023 18:09
Promises in ClojureScript

Chaining promises

Chaining promises in ClojureScript is best done using the thread-first macro, ->. Here's an example of using the fetch API:

(-> (js/fetch "/data")
    (.then (fn [r]
             (when-not (.-ok r)
               (throw (js/Error. "Could not fetch /data")))
             (.json r)))
@leobm
leobm / echo-ws-server.pl
Created June 21, 2020 15:52 — forked from willprice/echo-ws-server.pl
SWI-Prolog echo server with some JSON manipulation using websockets.
% INSTRUCTIONS
% =swipl echo-server.pl=
% =:- start_server.=
%
% Then navigate to http://localhost:3000 in your browser
:- module(echo_server,
[ start_server/0,
stop_server/0
@connorjclark
connorjclark / memory-test.ts
Created May 27, 2020 18:48
memory leak test
import { ChildProcess, spawn } from 'child_process';
import * as puppeteer from 'puppeteer';
const DEBUG = Boolean(process.env.DEBUG);
const CI = Boolean(process.env.CI);
const QUERY = Boolean(process.env.QUERY);
jest.setTimeout((QUERY ? 200 : 100) * 1000);
interface MemorySample {
@jarble
jarble / closure_example.pl
Last active February 8, 2024 09:05
An example of closures (or "nested predicates") in Prolog
%Since SWI-Prolog does not have a built-in implementation of closures, I wrote my own implementation here.
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :- predicate_with_nested(1,C),writeln(C).
call_local(Definition,Params) :-
copy_term(Definition,(Params :- Body)),
call(Body).
@fnky
fnky / ANSI.md
Last active May 8, 2024 15:42
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@ddevault
ddevault / Makefile
Last active February 20, 2024 14:17
Tiny Wayland compositor
WAYLAND_PROTOCOLS=/usr/share/wayland-protocols
# wayland-scanner is a tool which generates C headers and rigging for Wayland
# protocols, which are specified in XML. wlroots requires you to rig these up
# to your build system yourself and provide them in the include path.
xdg-shell-protocol.h:
wayland-scanner server-header \
$(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@
xdg-shell-protocol.c: xdg-shell-protocol.h
@Aerijo
Aerijo / tree_sitter_guide.md
Last active March 30, 2024 15:19
Guide to writing your first Tree-sitter grammar

Guide to your first Tree-sitter grammar

NOTE: The Tree-sitter API and documentation has changed and improved since this guide was created. I can't guarantee this is up to date.

About

Tree-sitter is the new way Atom is providing language recognition features, such as syntax highlighting, code folding, autocomplete, and more. In contrast to TextMate grammars, which work by regex matching, Tree-sitter will generate an entire syntax tree. But more on that can be found in it's own docs.

Here, we look at making one from scratch.

{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
module LMS where
import Data.Generics.Product
@adambene
adambene / coroutines-and-generators.js
Last active December 22, 2022 10:38
Coroutines and generators in JavaScript
function* delays() {
let a = yield delay(800, "Hello, I'm an");
console.log(a);
let b = yield delay(400, "async coroutine!");
console.log(b);
}
const coroutine = nextValue => iterator => {
const { done, value } = iterator.next(nextValue);