Skip to content

Instantly share code, notes, and snippets.

@hax
hax / iterator-with-message.md
Last active April 26, 2024 04:51
An idea from combination of `[...rest, last]` syntax, reverse iterator and `function.sent`
@hax
hax / channel.js
Created May 13, 2020 15:12
A experimental Channel in JS
// kotlin channel: https://kotlinlang.org/docs/reference/coroutines/channels.html
// golang channel
class Channel {
constructor() {
this._s = null
this._r = null
}
send(v) {
if (this._r) {
@hax
hax / README.md
Created January 18, 2022 12:52
Experimental API

NOTE: This proposal was mainly written 2 years ago, many examples may already outdated, but the whole idea should still apply.

Experimental API

Motivation

I have a dream, that the new JS APIs could be developed like good open source libraries, have reference implementation maintained by champions and volunteers in the whole lifecycle from stage 1 to stage 4, clearly marked as "experimental feature" not "polyfill", follow semver, and available in all platforms (all engines, browsers and node.js) automatically, accept issues and PRs in official github repo, can have branches to test new ideas, allow forks and healthy competition...

Problems

Array (a collection of elements accessed by integer indices) is probably one of the most commonly used data structures by programmers, and slice is one of the most commonly used array methods. (This statistic also indicates that splice seems to be used more than we thought.)

Method Search Results
push more than 2913780 results from 110260 repositories
forEach 1303701 results from 94472 repositories
slice 743508 results from 76749 repositories
indexOf 621518 re
@hax
hax / index.js
Created September 1, 2022 10:57
JS trick to mimic C# obj.onevent += delegate
const HandlerID = Symbol()
export function defineEvent(obj, name) {
const handlers = new Set()
const fn = function () {
for (const handler of handlers) {
try {
handler()
} catch (e) {
Promise.reject(e)
@hax
hax / safe_json_encode.php
Last active May 13, 2021 08:30
PHP safe json encode
<?php
function safe_json_encode($data) {
// We might have been tolerant to some common cases such as convert
// INF/NAN as 0 by using JSON_PARTIAL_OUTPUT_ON_ERROR option, but
// sadly `json_last_error()` only get the last error means it may
// override worse errors such as malfored utf-8 which we can't ignore!
// Poor H P !!
$result = @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
@hax
hax / json-slashs-hint.md
Last active September 23, 2020 07:01
JSON `\//` Hint

JSON \// Hint

Problems

JSON.stringify({x: 1234567890n}) // throw!

You can custom it

@hax
hax / 答工业聚.md
Created May 19, 2020 07:48
Explanation of my arrow switch (pattern match) idea

Rough exmaple:

switch (x) {
  case Number ?n -> console.log(n)
  case String ?s -> console.log(s)
  case Point {?x, ?y} -> console.log(x, y)
  case OtherPattern ?binding -> console.log(binding)
}
@hax
hax / promise-trans.md
Last active May 19, 2020 04:00
Promise 相关译名考虑

译名方法:

  • 黑话型(生造新词)
  • 会意型(合成新词、古词新用)
  • 假借型(复用引申已有专用名词)
  • 白话型(直接用日常用语)

术语

  • Promise
  • Status/State: pending/fulfilled/rejected, settled
  • Fate: resolved/unresolved
// 根据 https://www.zhihu.com/question/391388615/answer/1193135808 中的代码重构,
// 以对应所 fork 的工业聚版本
// 和工业聚版本的主要区别:
// - OO 范式而不是 FP 范式(其实像知乎原版那样不用 class 写成 makeTree 函数也是可以的,这里
// 主要是响应工业聚的号召,更「class」一些。但因为是 JS 而不是 TS 写的,就没有再加额外的
// interface 或 abstract class 了。)
// - Tree 没有弄成 immutable 的。要弄成 immutable 也不是不行,但 OO 范式下处理这类数据结构,
// 通常不会特意弄成 immutable 的,而是在必要的时候提供 clone 方法。
// - 简易起见,没有对 root 节点做额外处理(实际上似乎也不应该做原本格式的数据必然只有一个