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 / piper.rs
Last active September 12, 2019 07:49
rust : convert a writer function to an io::Read
extern crate pipe;
/**
Some libraries (such as handlebars or serde) offer functions that can generate data when given
an object that implements io::Write.
Some other libraries (such as Rocket) can consume data only from objects implementing io::Read.
Here is an example `piper` function that can be used to make these two kinds of libraries together.
/*
A WBO (https://wbo.openode.io) hello world script
This draws an orange square
Be careful not to commit more than a few updates per second to the server when scripting, or it will ban you.
Enter the following in your browser's javascript console:
*/
@lovasoa
lovasoa / generic-factorial.rs
Created April 11, 2019 09:40
generic factorial function in rust
use num::{One, BigUint}; // 0.2.0
use std::ops::{MulAssign, AddAssign};
fn fac<T>(n: T) -> T
where T: One + AddAssign + Ord,
for <'a> T : MulAssign<&'a T> {
let mut r = T::one();
let mut i = T::one();
while i <= n {
@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 {
@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:
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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.
@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 / 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;