Skip to content

Instantly share code, notes, and snippets.

View keshavsaharia's full-sized avatar

Keshav Saharia keshavsaharia

View GitHub Profile
@keshavsaharia
keshavsaharia / Mathematica - StringTakeUntil
Created June 30, 2013 05:46
A missing function in Mathematica that is essential for many string parsing applications. Inputs a string and a regular expression delimiter, and takes from the string until it matches the delimiter (or takes the entire string if the delimiter is not found).
StringTakeUntil[str_, delim_] :=
If[StringMatchQ[str, delim ~~ ___] || StringLength[str] == 0, "",
StringTake[str, 1] <> StringTakeUntil[StringDrop[str, 1], delim]];
@keshavsaharia
keshavsaharia / Mathematica - ASCII
Created June 30, 2013 05:44
A Mathematica function for generating beautiful ASCII art out of any image.
(*ASCIIrange = { " ","` ",". "," .","..",".:",":.","::",":#","##","#@","x@","o@","@@","@X","XX"};*)
(*ASCIIrange = { " "," ",".","..",".:","::",":i","io","ex","pq","Xq","AG","ZY","VW"};*)
(* Produces the best results by randomizing letters using a held random choice call *)
ASCIIrange = {" ", Hold[RandomChoice[{" ", " `"}]], ". ", "..",
Hold[RandomChoice[{".:", ":."}]],
Hold[RandomChoice[{"::", ";:", ":;", ";;"}]],
Hold[RandomChoice[{":x", "x:"}]],
Hold[RandomChoice[{"io", "ae", "oc"}]],
Hold[RandomChoice[{"yj", "tp", "qw"}]],
@keshavsaharia
keshavsaharia / Arduino - nextInt
Created June 30, 2013 05:39
nextInt() for Arduino serial communication
// Main method to call to get an integer.
// It will block until an integer followed by a non-digit delimiter is read from the serial input stream.
int getInt() {
return getInt(0);
}
// Recursive helper method.
// @param sum - an accumulator that "builds" the next number, character by character.
int getInt(int sum) {
// Read a single character from the serial input stream.