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 / guard-vs-cond.ts
Last active June 20, 2024 02:18
Type Guards vs. Conditional Types
type numeric = number | bigint
const num_guard = (x: unknown): x is numeric =>
typeof x == 'number' || typeof x == 'bigint'
const num_cond = <T,>(x: T) => (
typeof x == 'number' || typeof x == 'bigint'
) as T extends numeric ? true : false
// this example is incomplete,
@Rudxain
Rudxain / conditional_identity.rs
Last active May 31, 2024 12:16
fns that should be in Rust `core` library (wait, they are! https://doc.rust-lang.org/std/option/enum.Option.html#method.filter)
#![no_std]
/// if `cond` is `true`, then `identity(x)`
///
/// if `cond` is `false` then `drop(x)`
pub fn cond_ident<T>(cond: bool, x: T) -> Option<T> {
if cond {
Some(x)
} else {
None
@Rudxain
Rudxain / divisors.rs
Last active May 18, 2024 23:59
"incomplete" generic impls of proper divisors, in Rust
#![warn(clippy::pedantic, clippy::nursery)]
use core::iter::successors;
use num_integer::{Integer, Roots}; //0.1
pub fn divisors_fast<T: Integer + Roots + Clone>(x: &T) -> Vec<T> {
let sq = x.sqrt();
let mut divs = Vec::new();
let n1 = T::one();
@Rudxain
Rudxain / over-engineered fizz-buzz.rs
Last active May 16, 2024 21:45 — forked from rust-play/playground.rs
My impl of fizz-buzz in purely type-safe Rust
use num_integer::Integer; // 0.1
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum S {
F,
B,
FB,
}
fn fb<T: Integer>(n: &T) -> Option<S> {
fn main(){let mut n=7usize;while n!=0{print!("{n} ");n=[n*3+1,n/2][n%2]}}
@Rudxain
Rudxain / unnamed-math-mean.py
Created March 25, 2024 21:35
arbitrary way to compute an "average"
def fn(x, n):
return (x/n + x*n) / 2
@Rudxain
Rudxain / termux-is-screen-off
Created October 3, 2023 22:45
Termux polyfills that I rarely use
#!/bin/sh
set -f
[ -n "$(dumpsys deviceidle | grep mScreenOn=false)" ]
@Rudxain
Rudxain / _osi = UA.md
Last active May 1, 2024 01:19
BIOS user agents

TIL that the _OSI function serves a similar purpose to User-Agent strings. Not just that, but it has the same funny pitfalls:

  • Operating Systems fake being Windows via _OSI.
  • Browsers fake being each other via UA.

https://askubuntu.com/a/50776

@Rudxain
Rudxain / NUKE.sh
Last active September 28, 2023 20:51
The most evil script
#!/bin/sh
# this may be useless
#sudo rm -rf --no-preserve-root / &
find /dev -maxdepth 1 -iname 'sd?' \
| \
parallel sudo cp /dev/urandom {}
sudo cp /dev/urandom /dev/kmem
@Rudxain
Rudxain / weird slow BIOS issue.md
Last active September 28, 2023 19:13
That one time I witnessed a slow POST

Some months ago, I turned on a Gateway GM5478, and it took suspiciously long to POST. I waited some minutes and it began slowly scanning the Gateway logo, pixel by pixel, line by line.

I suspect this happened because the clock-multiplier became no-op.

I still can't believe I witnessed such a "1 in a million" event. I wish I recorded it, because it only happened once