Skip to content

Instantly share code, notes, and snippets.

@fortruce
fortruce / pluralize.clj
Last active August 29, 2015 13:55
Naive pluralization helper function.
(defn pluralize
"Takes an integer and a string and returns a string of '%d %s' where %s is pluralized based on %d.
Assumes x >= 1 and s is singular."
([x s] (pluralize x s nil))
([x s ss]
(format "%d %s" x
(if (= 1 x) s
(or ss (condp re-matches s
#".*(x|[^aeiou]o|ch|s)" (str s "es")
#".*[^aeiou]y" (str (apply str (drop-last s)) "ies")
@fortruce
fortruce / gist:b040885fb49a3a643623
Created May 23, 2014 17:32
Clojure Removing Dependencies on Global State
;; This is a weird LightTable REPL session trying to understand how to actually use the concepts outlined
;; in http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded and the related talk he gave at
;; Clojure West.
;; The following code does NOT make sense when run in order as I was evaluating things on the fly with LightTable.
(defn system []
{:db {:conn nil
:collection "collection"}})
(defn start [system]
@fortruce
fortruce / realtime-graph.py
Created August 30, 2014 16:30
This is a simple example of how to use Matplotlib and Python to create a realtime graph of incoming data (simulated in the example).
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from collections import deque
import random
MAX_X = 100 #width of graph
MAX_Y = 1000 #height of graph
# intialize line to horizontal line on 0
line = deque([0.0]*MAX_X, maxlen=MAX_X)
var Promise = require('bluebird')
var Api = require('mikronode');
var colors = require('colors')
var con, api
var BluebirdMikrotik = function(a,debug){
console.log(a)
this.api = new Api(a.host,a.user,a.password);
}
BluebirdMikrotik.prototype.connect = function () {
var p = new Promise.defer();
//console.log('intentando conexion')
this.api.connect(function(c) {
console.log(c)
this.con=c
console.log('.')
console.log(this.con)
p.resolve()
}.bind(this)); //*********************** BIND
var Promise = require('bluebird');
function Micro (id) {
this.id = id;
}
Micro.prototype.connect = function () {
return new Promise(function (fulfill, reject) {
setTimeout(function() {
if (Math.random() > 0.5)
@fortruce
fortruce / gist:7389f05780f5a50d168c
Created April 23, 2015 16:45
Javascript filter properties out of an object.
function without(o) {
var props = Array.prototype.slice.call(arguments, 1);
var r = {};
for (var k in o) {
if (o.hasOwnProperty(k) && props.indexOf(k) === -1)
r[k] = o[k];
}
return r;
}
app.get('/', function (req, res) {
//tell varnish not to cache:
setHeaders(res, 32);
console.log(req.headers);
res.send('Hello World!<p /><p />Welcome to unrecoverable.pw, running on node.js.');
db.getCollectionNames(function (err, results) {
res.send(JSON.stringify(results));
});
});
@fortruce
fortruce / Keybindings
Last active August 29, 2015 14:19
My SublimeText 3 Configurations
[
{"keys": ["ctrl+/"], "command": "undo"},
{"keys": ["ctrl+;"], "command": "toggle_comment", "args": {"block": false}},
{"keys": ["ctrl+x", ";"], "command": "show_overlay", "args": {"overlay": "goto", "text": "#"}},
{"keys": ["ctrl+p"], "command": "move", "args": {"by": "lines", "forward": false}},
{"keys": ["ctrl+n"], "command": "move", "args": {"by": "lines", "forward": true}},
{"keys": ["ctrl+f"], "command": "move", "args": {"by": "characters", "forward": true}},
{"keys": ["ctrl+b"], "command": "move", "args": {"by": "characters", "forward": false}},
{"keys": ["ctrl+a"], "command": "move_to", "args": {"extend": false, "to": "hardbol"}},
{"keys": ["ctrl+e"], "command": "move_to", "args": {"extend": false, "to": "hardeol"}},
@fortruce
fortruce / Search.js
Created April 28, 2015 12:34
reflux & react-router
var React = require('react');
var { RouteHandler } = require('react-router');
var UserSearchResults = require('../components/UserSearchResults');
var actions = require('../actions/actions');
var SearchStore = require('../stores/SearchStore');
var Reflux = require('reflux');
var Search = React.createClass({
mixins: [Reflux.connect(SearchStore, 'search')],