Skip to content

Instantly share code, notes, and snippets.

View csauve's full-sized avatar
⌨️
doot doot

Connor Sauve csauve

⌨️
doot doot
View GitHub Profile
@csauve
csauve / gist:43bf117a31dca9808228
Created December 8, 2014 22:10
Simple map-reduce in Groovy
def mapReduce = { collection, mapper, reducer ->
collection.groupBy(mapper)
.collectEntries { k, v ->
[k, reducer(v)]
}
}
mapReduce([1, 6, 3, 7, 3, 9, 1, 2, 10, 9, 5, 8, 4],
{ it % 2 ? "odd" : "even" },
{ it.size })
@csauve
csauve / unicode-hide.js
Created November 6, 2015 08:54
Given some ASCII input text, encodes it in the free codepoint bits of 3-byte unicode characters. Result is fewer characters but more bytes.
var TextEncoder = require("text-encoding").TextEncoder;
var TextDecoder = require("text-encoding").TextDecoder;
var jsSource = "";
process.stdin.resume();
process.stdin.on("data", function(buf) {
jsSource += buf.toString();
});
process.stdin.on("end", function() {
@csauve
csauve / jordan-curves.html
Created September 12, 2017 07:39
Visualize inscribed lines in jordan curves
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jordan Curve Inscribed Lines</title>
<style>
body { font-family: monospace; }
.canvas-section { display: inline-block; }
canvas { border: 1px solid black; }

Functional JS & Ramda


What is functional JS?

JavaScript is a multi-paradigm language, supporting imperative, object-oriented, and functional styles. This presentation is intended to add some tools to your toolbelt. With familiarity of its patterns, functional JS can be very expressive. These patterns often follow from JS best practices:

  • Don't extend Array.prototype or Object.prototype with nonstandard functionality; use utility functions
  • Avoid shared or global state
  • Prefer pure functions without side-effects
#!/bin/sh
set -ex
mkdir -p /opt/jdk
tar zxvf jdk.tar.gz -C /opt/jdk --strip-components=1
rm jdk.tar.gz
rm /opt/jdk/lib/src.zip
/opt/jdk/bin/jlink \
--module-path /opt/jdk/jmods \
@csauve
csauve / git-lines.sh
Created June 21, 2018 22:18
List line contributions to all git repos in CWD
for d in ./*/ ; do (cd "$d" && git log --author="<name here>" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 + $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -); done
// error[E0502]: cannot borrow `window` as mutable because it is also borrowed as immutable
// --> src/main.rs:14:9
// |
// 14 | window.poll(|event| {
// | ^ ---- ------- immutable borrow occurs here
// | | |
// | _________| immutable borrow used by call, in later iteration of loop
// | |
// 15 | | match event {
// 16 | | WindowEvent::Close => {
@csauve
csauve / graph-game.js
Last active March 10, 2019 07:19
Plots win rates for optimal play of a randomly generated graph-based game
/* In this game, alice and bob take turns removing any vertex of nonzero even
* degree from an undirected graph, which also removes all connected edges.
* The winner is the last player able to do so.
*/
//compactify vertex indices to improve memoization cache hit rates
function simplify(edges) {
const mappings = edges
.flat()
.reduce((uniq, v) => uniq.includes(v) ? uniq : [...uniq, v], [])
@csauve
csauve / trimbytes.groovy
Created October 1, 2019 22:52
Safely trims a String to a max length of bytes in a desired encoding without cutting multi-byte characters in half.
static String trimBytes(String input, int lengthBytes, Charset charset) {
if (input == null) return null
if (input.getBytes(charset).length <= lengthBytes) return input
def bytes = charset.encode(input)
bytes.limit(Math.min(lengthBytes, bytes.limit()))
def chars = charset.newDecoder()
.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.IGNORE)
.decode(bytes)
const R = require("ramda");
//state operations
const updStack = R.curry((prop, updater, state) => R.over(R.lensProp(prop), updater, state));
const updCall = (stackOp) => updStack("callStack", stackOp);
const updData = (stackOp) => updStack("dataStack", stackOp);
//stack operations
const pop = R.curry((num, stack) => {
if (stack.length < num) throw new Error("Stack underflow!");