Skip to content

Instantly share code, notes, and snippets.

### Keybase proof
I hereby claim:
* I am mearns on github.
* I am mearns (https://keybase.io/mearns) on keybase.
* I have a public key whose fingerprint is E65B 5B00 0E4F 8FFC 8529 3C5B 3530 CADE 3AA7 0848
To claim this, I am signing this object:
@mearns
mearns / prepare-chef-envs.sh
Last active February 1, 2017 17:16
Prepare the same modification to a set of chef environment files, and create branches for each env.
#!/bin/bash
#
# A script to prepare the same set of modifications to a specified set of chef
# environment files, each in a separate git branch. This is typically used for
# creating merge requests for deploying a ticket (e.g., a new cookbook version)
# to each of the specified environments.
#
# Requires <https://github.com/trentm/json>
#
function openFileDisposer(filePath) {
const openedFilePromise = openFileAsPromised(filePath);
const fileDisposer = openedFilePromise.disposer((openedFile) => {
openedFile.close();
});
return fileDisposer;
}
const promiseToOpenWriteAndCloseFile = Promise.using(openFileDisposer('/tmp/foo'), (openedFile) => {
int inc(int x) {
int m = 1;
while (m) {
x ^= m;
m = (m & ~x) << 1;
}
return x;
}
int dec(int x) {
@mearns
mearns / chain-over-nest-local.js
Last active October 16, 2017 17:58
Toying around with some ideas of chaining instead of nesting, provoked by the presentation I'm currently writing about Promises.
const foo = (() => {
return bar() + baz() // = 80, I think.
})
.local('bar', () => 3*baz())
.local('baz', () => 2*trot)
.local('trot', 10)
@mearns
mearns / map.java
Last active December 6, 2017 17:39
normalization in design - non-normalized mapping
Map<String, Thing> createLookupMap (Collection<Thing> thingsToMap) {
Map<String, Collection<Thing>> lookup = new HashMap<>();
for (Thing thing : thingsToMap) {
String propValue = thing.getSomeProperty();
lookup.put(propValue, thing);
}
return lookup;
}
@mearns
mearns / follow-up.java
Last active January 22, 2018 03:24
Follow Up Pattern
map.put(key, value)
.ifHadKey(oldValue -> System.out.println("Replaced " + oldValue + " at key " + key))
.ifDidNotHaveKey(() -> System.out.println("New key added: " + key));
@mearns
mearns / Assuming.java
Created February 2, 2018 18:54
Java Assumptions Pattern
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
@mearns
mearns / README.md
Created April 16, 2018 15:25
A trivial example of a circular dependency in python

If you run python a.py from this directory, you get an error such as:

Traceback (most recent call last):
  File "a.py", line 1, in <module>
    import b
  File "./b.py", line 1, in <module>
    import a
  File "./a.py", line 3, in <module>
 b.bar()
@mearns
mearns / recipe.rb
Created August 1, 2018 16:45
Example use of a helper library "myapp" in a chef recipe.
template myapp.config_file_path do # Access helper library methods inside a recipe.
source 'config.yml.erb'
owner 'root'
group 'root'
mode '644'
variables(
log_level: myapp.logging_level # Access helper library methods inside a resource
log_file_path: myapp.log_file_path
)
end