Skip to content

Instantly share code, notes, and snippets.

@mverleg
mverleg / parallel_map.rs
Last active March 19, 2024 14:29
Multithreaded version of map in Rust - see tests for usage - safe
extern crate num_cpus;
extern crate scoped_pool;
use scoped_pool::Pool;
use std::fmt::Debug;
use std::sync::mpsc::sync_channel;
use std::vec::Vec;
/// Transform a collection to another collection using a closure,
/// with execution happening in parallel on a new thread pool with one thread per cpu.
!!
!! f2py -c -m search search.f90
!!
subroutine find_first(needle, haystack, haystack_length, index)
!!
!! Find the first index of `needle` in `haystack`.
!!
implicit none
@mverleg
mverleg / rust_snippet_runner.sh
Created October 26, 2019 21:34
Bash wrapper for Runner, a cool tool to run Rust snippets, adding stdin, caching and standard flags https://github.com/stevedonovan/runner
function rsr() {(
##
## Use 'runner' on a string instead of a file.
##
## If code is on stdin, all arguments are passed on. Otherwise,
## arg1: the string to run
## arg2+: passed on to the executable
##
## Examples:
## rsr 'println!("{}", "hi");' my_arg
@mverleg
mverleg / python_release.sh
Created October 26, 2019 13:05
Bash script to create a Python release. Uploads to the test server first, signs uploads, verifies install after upload, and some other checks.
function python_release() {
##
## Upload a new release for a python package
##
## Requirements:
## - ~/.pypirc exists and contains both testpypi and pypi
## - Active virtual environment
## - setup.py exists
##
## Besides the above, will verify that:
@mverleg
mverleg / json2table.py
Last active October 13, 2019 20:32
Convert json list of dicts to a table
#!/usr/bin/env python3
"""
Render json as a text table.
Input should be a list of dictionaries:
[{"name": "first", "a": 1, "b": null, "x": true}, {"b": 2, "name": "second", "x": false}, {"a": 123, "name": "third", "x": null}]
Output will be a table:
@mverleg
mverleg / rs_ws_ssl_server_client.rs
Created December 2, 2018 23:03
Simple rs-ws websocket server and client that don't quite work (for bug report)
extern crate env_logger;
#[macro_use]
extern crate log;
extern crate openssl;
extern crate proc_macro;
extern crate url;
extern crate ws;
use std::collections::HashMap;
use std::fs::File;
@mverleg
mverleg / voronoi.py
Created September 12, 2018 20:05
Voronoiify an image
"""
This divides the images in small patches by means of minimal distance to random points.
It then computes the average color of each patch, and gives the whole patch that color.
This code is NOT fast, O(N^2) for N number of pixels (~1min). It's pretty short and easy though.
"""
from random import randint, seed
from time import time
from matplotlib.pyplot import subplots, show
@mverleg
mverleg / rust_modulo_from_rem.rs
Last active July 31, 2018 19:56
Implement modulo operation for types that have remainder implemented (and add and clone), with specialization for copy types
/// Define a modulo operation, in the mathematical sense.
/// This differs from Rem because the result is always non-negative.
pub trait Modulo<T> {
type Output;
#[inline]
fn modulo(self, other: T) -> Self::Output;
}
@mverleg
mverleg / playground.rs
Created April 10, 2018 20:58 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Unfortunately no polymorphism
struct Alpha {}
trait Beta {
fn p(&self, z: &Beta) -> bool;
}
trait Gamma: Beta {}
@mverleg
mverleg / identifier.py
Created February 26, 2018 22:07
Encode/decode an incrementing integers as an alphanumeric identifier
"""
Encode/decode an incrementing integers as an alphanumeric identifier.
Uses digits and case-insensitive letters (produces caps, accepts both), minimum length 3.
Does not cryptographically hide the original id.
"""
def to_code(nr):
assert nr >= 0
nr *= 7
code = ''