Skip to content

Instantly share code, notes, and snippets.

@noahlt
noahlt / fizzbuzz.hs
Created December 18, 2013 02:21
FizzBuzz in Haskell — I'm sure this has been done before, but this one is mine.
mapM_ putStrLn [ if x `mod` 15 == 0 then "fizzbuzz" else if x `mod` 3 == 0 then "fizz" else if x `mod` 5 == 0 then "buzz" else show x | x <- [1..100]]
/*
* TOA = TON + NPI
* See TS 24.008 section 10.5.4.7 for details.
* These are the only really useful TOA values
*/
public static final int TOA_International = 0x91;
public static final int TOA_Unknown = 0x81;
@noahlt
noahlt / .flowconfig
Last active August 29, 2015 14:19
flow check: declare module
[ignore]
[include]
[libs]
./lib.js
[options]
@noahlt
noahlt / lintjs.sh
Created August 3, 2015 18:49
semiautomatic semicolon insertion
#!/usr/bin/env bash
# lintjs.sh
REPO_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
JS_FILES=$(find . \
-not -path '*node_modules*' \
-not -path '*module-cache*' \
-not -path '*/lib/*' \
-not -name '*.min.js' \
@noahlt
noahlt / react_dont_update_children.html
Created October 17, 2015 00:52
Hack to block re-rendering child when parent was updated
<!DOCTYPE html>
<html>
<head>
<title>Hack to block re-rendering child when parent was updated</title>
<script src="https://fb.me/react-0.13.0.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.0.js"></script>
</head>
<body>
</body>
@noahlt
noahlt / pick.py
Created November 17, 2015 18:12
Pick keys/values from dict
def pick(d, filter_tree):
'''Returns a new dictionary by picking keys/values from d per filter_tree.
Works recursively: values in filter_tree can be either True or dictionaries
which are their own filters.
>>> d = {'a': 1, 'b': 2, 'c': {'foo': -1, 'bar': -2}, 'd': {'r': 'red', 'b': 'blue'}}
>>> pick(d, {'a': True, 'b': True})
{'a': 1, 'b': 2}
>>> pick(d, {'a': True, 'b': True, 'd': True})
{'a': 1, 'b': 2, 'd': {'r': 'red', 'b': 'blue'}}
@noahlt
noahlt / SafeReactComponent.js
Created March 9, 2016 18:21
Catch errors in render() with SafeReactComponent
var React = require('react');
var _ = require('underscore');
// Catches errors in render() and returns null so that we don't
// break the entire React app.
var SafeReactComponent = function(classObj) {
var originalRender = classObj.render;
var displayName = classObj.displayName || 'unknown component';
return React.createClass(_.extend(classObj, {
render: function() {

A bit easier to read:

var installed = WindowController.getInstalledPlugins();
return {
  installedPlugins: _.intersection(conf.SUPPORTED_EDITORS, installed),
  uninstalledPlugins: _.difference(conf.SUPPORTED_EDITORS, installed),
}
@noahlt
noahlt / remote_env_vars.sh
Created May 17, 2016 23:34
Using environment variables from remote servers
#!/usr/bin/env bash
# A couple of handy bash functions that I have in my .bashrc:
# get_env uses ssh to copy an environment variable from a remote
# server to your current machine. Example usage:
#
# get_env SOME_VAR_NAME example.com
get_env () {
VARNAME=$1
@noahlt
noahlt / prctx.go
Created August 24, 2016 23:19
Print context of string around a point
// Print context of string around a point
func prctx(buf string, point int) {
if point < 0 || point >= len(buf) {
log.Printf("context for point: out of bounds (%d/%d)", point, len(buf))
return
}
min := func(a int, b int) int {
if a < b {