Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / 100prisoners.js
Last active Aug 8, 2022
[javascript]simulate 100 prisoners problem strategies
View 100prisoners.js
// Simulation of 100 prisoers problem strategies
// - https://en.wikipedia.org/wiki/100_prisoners_problem
// array utils
const range = n => [...Array(n).keys()];
const shuffle = a => {
for (let i = a.length - 1; i > 0; i--) { // Fisher-Yates
const j = Math.random() * i >>> 0;
[a[i], a[j]] = [a[j], a[i]];
}
@bellbind
bellbind / build.zig
Last active Aug 8, 2022
[zig][WebAssembly][c11] FFT
View build.zig
// build all: $ zig build
// clean up: $ zig build dist-clean
const builtin = @import("builtin"); // list-up the content with `zig builtin` command
const Builder = @import("std").build.Builder;
//NOTE: this build code is for zig-0.4.0, maybe incompatible with zig >= 0.5
pub fn build(b: *Builder) void {
{ //[case: WebAssembly wasm] build fft.wasm
const wasmStep = b.step("fft.wasm", "build fft.wasm");
const wasm = b.addExecutable("fft.wasm", "fft.zig");
@bellbind
bellbind / pem.js
Last active Aug 6, 2022
[nodejs]Example of RSA usages with node-forge
View pem.js
// RSA with node-forge
"use strict";
// npm install node-forge
const forge = require("node-forge");
new Promise((f, r) => forge.pki.rsa.generateKeyPair(
2048, (err, pair) => err ? r(err) : f(pair)))
.then(keypair => {
const priv = keypair.privateKey;
@bellbind
bellbind / index.html
Last active Aug 2, 2022
[threejs] simplified gltf2 loader example
View index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://unpkg.com/three"></script>
<script
src="https://unpkg.com/three/examples/js/loaders/GLTF2Loader.js"
></script>
<script src="script.js" defer="defer"></script>
</head>
@bellbind
bellbind / index.html
Created Jul 29, 2022
[JavaScript][HTML] run es modules in iframe context
View index.html
<!doctype html>
<html>
<head>
<script type="module">
document.querySelector("#run").addEventListener("click", async ev => {
const modUrl = new URL("./module.js", location.href).href;
//console.log(modUrl);
const iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.append(iframe);
@bellbind
bellbind / index.html
Last active Jul 23, 2022
[WebGPU] Game of Life as Compute+Render example for Chrome-103
View index.html
<!doctype html>
<html>
<head>
<!-- IMPORTANT: The current Chrome requires some origin-trial token in <meta>.
To register origins at the last "WebGPU REGISTER" in https://developer.chrome.com/origintrials/
This token is for a Web Origin "https://gist.githack.com" (maybe expired at Mar 31, 2022)
It can register localhost origin as "http://localhost:8000"
-->
<meta http-equiv="origin-trial"
content="Akv07qcAop5MFaZYxJtHHjUuM8eV3GpbHkTeuhZo/4wsNjYnQ7GSGJyo7hRVZvpvyjYwilbJ8KbFVchI4O1DpA0AAABQeyJvcmlnaW4iOiJodHRwczovL2dpc3QuZ2l0aGFjay5jb206NDQzIiwiZmVhdHVyZSI6IldlYkdQVSIsImV4cGlyeSI6MTY1MjgzMTk5OX0=" />
@bellbind
bellbind / index.html
Last active Jul 14, 2022
[chrome][android] BarcodeDetector example
View index.html
<!doctype html>
<html>
<head>
<script type="module">
// WICG Shape Detection API
// - https://wicg.github.io/shape-detection-api/
try {
const start = document.getElementById("start");
const video = document.getElementById("video");
const result = document.getElementById("result");
@bellbind
bellbind / cpuinfo.c
Created Oct 29, 2015
[c][intel][clang] get cpu info from CPUID intrinsic in clang/gcc
View cpuinfo.c
// intel CPUID opcode
// see: https://en.wikipedia.org/wiki/CPUID
// clang -Wall -Wextra -std=c11 cpuinfo.c -o cpuinfo
#include <stdint.h>
#include <stdio.h>
#include <cpuid.h> //macro __cpuid(eaxin, eaxout, ebx, ecx, edx)
int main() {
{
@bellbind
bellbind / fizzbuzz.c
Created Mar 5, 2015
[c]FizzBuzz without modulus and branching (no-warning with -Wall -Wextra)
View fizzbuzz.c
#include <stdio.h>
int main() {
int n; char* t[] = {"%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n"};
for (n = 1; n <= 100; n++) printf(t[1/(1+n-n/3*3)+1/(1+n-n/5*5)*2], n);
}
@bellbind
bellbind / main.js
Last active Jul 8, 2022
[electron] Tray launcher example
View main.js
"use strict";
// [run the app]
// $ npm install electron
// $ ./node_modules/.bin/electron .
const {app, nativeImage, Tray, Menu, BrowserWindow} = require("electron");
let top = {}; // prevent gc to keep windows