Instantly share code, notes, and snippets.

View array_to_string.h
#include <string>
// Convert array to string
template<class T> std::string array_to_string(T array[], size_t size, std::string join="", std::string end="", std::string start="") {
std::string output = start;
if(size > 0) output += std::to_string(array[0]);
for(T i = 1; i < size; i++) output += join + std::to_string(array[i]);
return output + end;
}
View optimise.bash
#!/bin/bash
shopt -s globstar
for i in **/*.jpg; do
echo "$i"
convert "$i" -auto-orient pnm:- | ./cjpeg.exe -quality 90 > "${i%%.jpg}_optimised.jpg"
mv "${i%%.jpg}_optimised.jpg" "$i"
done
View lisp.md

Lisp

Writing your own Lisp-ette is a brilliant evening or weekend project, regardless of the language. It's some of the simplest non-toy parsing you can attempt, a bit of light data structure work, and understanding eval/apply is 80% of the work in implementing it. I would highly recommend anyone to have a go, and try not to follow existing code too closely: figure out the problems in your language of choice. The post identifies some of it's own weaknesses (memory handling, errors), which are quite C specific. Or at least easier to handle in other languages, where you can punt those issues to the host language runtime. But it will be a fun extension to fix them (a job for the second evening / weekend of coding ;) ) But, imho, the beauty of writing a Lisp is that there are a bunch of things you can do from there, some more difficult, but several are achievable step-by-step in a day or a few days each. I'd first add a few special forms more than the OP (quote, if, progn, math operations), then my suggestions:

View feed.xml
---
layout: xml
---
<rss version="2.0">
<channel>
<title>{{ site.title }}</title>
<link>{{ site.link }}</link>
<description>{{ site.description }}</description>
<copyright></copyright>
View ACMCitSeq.xsl
<?xml version="1.0" encoding="utf-8"?>
<!--
Stylesheet for Microsoft Word 2007 Bibliography formatting.
Author(s): Yves Dhondt (yves.dhondt@gmail.com) and Matt Consto (matt@consto.uk)
Copyright: Copyright (c) 2008 Yves Dhondt
Permission is hereby granted, free of charge, to any person obtaining a
View cheaters.py
from random import random
people = 1
limit = 500000
while True:
total = 0
for iteration in range(limit):
# Mutate
View StackSort.java
import java.util.Stack;
public class StackSort {
/**
* Sort a stack, using a stack, and only* a stack. It's not at all efficient.
* @param asc The stack we want to sort into ascending order.
* @throws NullPointerException If any element in the stack is NULL.
*/
public static <T extends Comparable<? super T>> void sort(Stack<T> asc) {
Stack<T> desc = new Stack<>();
View maze.py
from random import shuffle, randrange
def make_maze(w = 20, h = 8):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for _ in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
def walk(x, y):
vis[y][x] = 1
View format.js
Date.prototype.format = function(string) {
// Helper function
var pad = function(width, string, padding) {
return (width <= (string + "").length) ? (string + "") : pad(width, (padding + "") + (string + ""), (padding + ""))
}
// Build data lazily, only evaluate when needed.
var date = this; // For scoping reasons.
var leap = ((date.getFullYear() % 4 == 0) && (date.getFullYear() % 100 != 0)) || (date.getFullYear() % 400 == 0);
var replacements = {
View grid.py
#!/bin/python
import subprocess
color = "white"
thickness = 1
spacing = 200
file = "input.jpg"
def rreplace(string, old, new, max=-1):