Skip to content

Instantly share code, notes, and snippets.

View lovasoa's full-sized avatar
🎯
Focusing

Ophir LOJKINE lovasoa

🎯
Focusing
View GitHub Profile
@lovasoa
lovasoa / async_balancing.js
Created June 4, 2019 13:09
Balance load between asynchronous functions
/**
* Balances work between several asynchronous functions.
* Given an array of async functions that make the same computation,
* returns a single function that has the computation made by
* the first idle function.
*
* @template T,U
* @param {((...args:T) => Promise<U>)[]} functions
* @param {boolean?} fifo Whether to process the arguments in fifo or lifo
* @returns {(...args:T) => Promise<U>}
@lovasoa
lovasoa / max_canvas_size.js
Created May 27, 2019 17:07
Compute the maximum available canvas width and height in a browser
async function maxCanvasSize(ratio) {
let cvs = document.createElement("canvas");
let xmin = 1, xmax = 1 << 18;
while (xmax - xmin > 1) {
let x = ((xmin + xmax) / 2) | 0;
let y = (x / ratio) | 0;
if (await canvasTest(cvs, x, y)) xmin = x;
else xmax = x;
}
return [xmin, xmin / ratio | 0];
@lovasoa
lovasoa / my_aes_decrypt.py
Created May 22, 2019 11:32
custom aes decrypt in pure python3 (does not include the key schedule algorithm)
#!/usr/bin/env python3
# coding: utf-8
from functools import reduce
from operator import xor
import struct
aes_inverse_s_box = bytes.fromhex(
'52096ad53036a538bf40a39e81f3d7fb7ce339829b2fff87348e4344c4dee9cb547b9432a6c2233dee4c950b42fac34e082ea16628d924b276'
'5ba2496d8bd12572f8f66486689816d4a45ccc5d65b6926c704850fdedb9da5e154657a78d9d8490d8ab008cbcd30af7e45805b8b34506d02c'
'1e8fca3f0f02c1afbd0301138a6b3a9111414f67dcea97f2cfcef0b4e67396ac7422e7ad3585e2f937e81c75df6e47f11a711d29c5896fb762'
@lovasoa
lovasoa / aes.py
Last active March 1, 2023 10:08 — forked from bonsaiviking/aes.py
A simple/simplistic implementation of AES in pure Python 3
# My AES implementation
# By Daniel Miller
# Ported to python 3 by @lovasoa
def xor(s1, s2):
return bytes(a ^ b for a, b in zip(s1, s2))
class AES(object):
@lovasoa
lovasoa / flatten.js
Last active May 20, 2019 12:37
mutable array flatten. An array flattening implementation that does not require more memory than the original deep array.
/**
* Multi-level array flattening. Takes an array of arbitrarily deep nested arrays, and returns an array of values.
* It mutates the original array instead of creating a new one.
**/
function flatten(arr) {
for (let i = 0, inc=1; i < arr.length; i += inc) {
if (Array.isArray(arr[i])) {
const flat = flatten(arr[i]);
arr.splice(i, 1, ...flat);
inc = flat.length;
@lovasoa
lovasoa / fetchChunks.js
Created May 16, 2019 15:48
fetch chunks: use the fetch and WHATWG Stream APIs to return an async generator of typed arrays given an URL
async function* fetchChunks(...args) {
let response = await fetch(...args);
let reader = response.body.getReader(), state = {done:false};
while (!state.done) {
state = await reader.read();
yield state.value;
}
}
@lovasoa
lovasoa / snake_cube_julia.ipynb
Last active May 13, 2019 10:08
Résolution du diabolicube en julia
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lovasoa
lovasoa / test_PEP563_annotations.py
Created May 6, 2019 07:31
typing.get_type_hints fails when used with a class defined in a documentation test
#!/usr/bin/env python3 -m doctest
from __future__ import annotations
import typing
def f(clazz):
"""
>>> class MyClass:
... pass
>>> class MyOtherClass:
@lovasoa
lovasoa / indexable_iterator.rs
Created April 16, 2019 07:25
An iterator that stores the values it yields, in order to be able to access them by index.
/// An iterator where you can look back on items that were already consumed
struct PartiallyConsumedIterator<T, I: Iterator<Item=T>> {
/// Elements that were already consumed
previous: Vec<T>,
/// Elements that still have to be consumed
remaining: I,
}
impl<T, I: Iterator<Item=T>> From<I> for PartiallyConsumedIterator<T, I> {
fn from(iter: I) -> Self {