Skip to content

Instantly share code, notes, and snippets.

View Rudxain's full-sized avatar

Ricardo Fernández Serrata Rudxain

View GitHub Profile
@Rudxain
Rudxain / more logic.js
Last active November 5, 2022 14:06
Some "missing" JS logical operators defined as functions
'use strict'
const
/**Python's `bool` ported to JS (doesn't work quite the same) */
bool = x => x == true,
logicXOR = (a, b) => !a != !b ? a : b,
logicXNOR = (a, b) => !a == !b ? a : b,
logicXORalt = (a, b) => !a != !b ? a || b : a && b,
logicXNORalt = (a, b) => !a == !b ? a || b : a && b,
@Rudxain
Rudxain / dominance.js
Last active August 26, 2022 07:51
How to assert dominance in Javascript
'use strict'
const assert = function(bool, msg) {if (!bool) throw new Error(msg)}
let dominance = true
assert(dominance, 'dominance not asserted')
//I'm so based that I purposefully ignored `console.assert`
console.log('dominance successfully asserted lol')
dominance = false //dominance doesn't last forever
@Rudxain
Rudxain / CTZ algos.js
Last active February 25, 2023 03:37
Algorithms to count binary trailing zeros in the mathematical value of floats
'use strict'
const {trunc} = Math
/** mantissa bit-length of IEEE 754 "binary64" double-float */
const M_LEN = 52n
/**
Bitcast. Keep in-memory bits the same, but change the type.
@param {number} f
*/
@Rudxain
Rudxain / freq-analysis.ts
Last active June 24, 2024 23:10
Counts how many instances of each value are present in an iterable, implemented in Rust and TypeScript
export const count_values = <T,>(it: Iterable<T>) => {
const counts: Map<T, bigint> = new Map
for (const x of it)
counts.set(x, (counts.get(x) ?? 0n) + 1n)
return counts
}
@Rudxain
Rudxain / minimum bit length.js
Last active April 11, 2024 17:12
One of the many ways to measure entropy
'use strict'
/**
Calculates the minimum int-num of payload bits needed to encode the given data.
It does so with a simple algorithm:
- `boolean`: `1` (duh).
- `bigint`: bitcount - Most Significant One + sign-bit (only if negative), `sizeof 0n == 0`.
- `number`: Same as `bigint` (MSO is the implicit-bit) + bits after radix-point (not including the point itself).
- `string`: Number of code-points multiplied by the bits-per-char. BPC is calculated from min-charset-size.
@Rudxain
Rudxain / Generic Hamming Dist.js
Last active December 22, 2022 22:38
Calculate Hamming Distance of any JS types, even different types. Supports mismatched lengths
'use strict'
/**
@param {number | bigint | Iterable} a
@param {number | bigint | Iterable} b
@license Unlicense
*/
const Hamming_dist = (a, b) => {
const get_chars = x => {
const t = typeof x
return 'number' == t || 'bigint' == t
@Rudxain
Rudxain / get-in.js
Last active December 8, 2022 15:15
Access any value within a tree, at any depth. Ported from Clojure
'use strict'
/**
@param {unknown} struct
@param {Iterable<unknown>} keys
*/
const get_in = (struct, keys) => {
for (const k of keys)
struct =
struct instanceof Map ||
@Rudxain
Rudxain / esoteric indentation.txt
Last active December 8, 2022 14:04
Remember Fibonacci indent? What if we use other math sequences in the OEIS? (I'll probably convert this into a repo))
// Prime
foo {// 0 isn't prime, but we need it
bar {// 1 gets totally ignored lol
baz {
quz {
quux {
corge {
grault {
garply {
// waldoo
@Rudxain
Rudxain / Because the essence of programming is repeating.py
Last active December 18, 2022 06:01
Because the essence of programming is repeating.
Because_the_essence_of_programming_is_repeating = "Because the essence of programming is repeating".split()
while True:
for word in Because_the_essence_of_programming_is_repeating:
for _ in range(len(Because_the_essence_of_programming_is_repeating)):
print(word)
# LICENSE: https://unlicense.org
@Rudxain
Rudxain / Real-num CTZ.md
Last active December 18, 2022 04:01
Attempt to generalize Count-Trailing-Zeros to non-integers, in a mathematical way, no IEEE754

define CTZ(a * b^c, b) = c ; where GCD(a, b^c) = 1

following is in binary:

0.0101 -> -10

101 * 10^-100 -> -100 (contradiction)