Skip to content

Instantly share code, notes, and snippets.

Connor Sauve csauve

Block or report user

Report or block csauve

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@csauve
csauve / trimbytes.groovy
Created Oct 1, 2019
Safely trims a String to a max length of bytes in a desired encoding without cutting multi-byte characters in half.
View trimbytes.groovy
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)
@csauve
csauve / graph-game.js
Last active Mar 10, 2019
Plots win rates for optimal play of a randomly generated graph-based game
View graph-game.js
/* 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], [])
View borrow-error.rs
// 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 / git-lines.sh
Created Jun 21, 2018
List line contributions to all git repos in CWD
View git-lines.sh
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
View build-minimal-jre.sh
#!/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 \
View lunch-n-ramda.md

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
@csauve
csauve / jordan-curves.html
Created Sep 12, 2017
Visualize inscribed lines in jordan curves
View jordan-curves.html
<!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; }
View stack-machine.js
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!");
@csauve
csauve / unicode-hide.js
Created Nov 6, 2015
Given some ASCII input text, encodes it in the free codepoint bits of 3-byte unicode characters. Result is fewer characters but more bytes.
View unicode-hide.js
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 / gist:43bf117a31dca9808228
Created Dec 8, 2014
Simple map-reduce in Groovy
View gist:43bf117a31dca9808228
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 })
You can’t perform that action at this time.