Skip to content

Instantly share code, notes, and snippets.

View search.f90
!!
!! 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
View rust_snippet_runner.sh
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.
View python_release.sh
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
View json2table.py
#!/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)
View rs_ws_ssl_server_client.rs
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 / parallel_map.rs
Last active September 22, 2018 21:01
Multithreaded version of map in Rust - see tests for usage - safe
View parallel_map.rs
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.
@mverleg
mverleg / voronoi.py
Created September 12, 2018 20:05
Voronoiify an image
View voronoi.py
"""
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
View rust_modulo_from_rem.rs
/// 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
View playground.rs
// 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
View identifier.py
"""
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 = ''