Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴

Ryan Seys ryanseys

🌴
View GitHub Profile
@ryanseys
ryanseys / Foo.java
Created Oct 10, 2015
Builder pattern without a Builder class
View Foo.java
public class Foo {
private String name;
private String town;
private int age;
public Foo setName(String name) {
this.name = name;
return this;
}
@ryanseys
ryanseys / kill-port.sh
Created May 24, 2015
Kill processes using a particular port
View kill-port.sh
lsof -P | grep ':8080' | awk '{print $2}' | xargs kill -9
@ryanseys
ryanseys / gcloud-socket.js
Last active Aug 29, 2015
Testing socket stuff
View gcloud-socket.js
// before running, set pool: { maxSockets: 2 } in request defaults.
var projectId = process.env.PROJECT_ID;
var keyFile = process.env.GCLOUD_KEYFILE;
var fs = require('fs');
var gcloud = require('gcloud')({
projectId: projectId,
credentials: require(keyFile)
});
View utf8.js
var range = Array.apply(null, Array(1000)).map(function (_, i) {return i;});
var str = '';
for (i in range) {
str += String.fromCharCode(i);
}
new Buffer(str).toString('utf8') == str
// returns true
@ryanseys
ryanseys / dns.js
Last active Aug 29, 2015
Cloud DNS
View dns.js
var config = {};
var dns = gcloud.dns(config);
// - - - Zones - - - //
var query = {
maxResults: 2,
pageToken: 'blah'
};
dns.getZones(query, function(err, zones, nextQuery, apiResponse) {});
View random-nums.js
function getUniformRandomNumber() {
return Math.random();
}
// Rate is the rate parameter of the exponential
// distribution. Typically denoted by lambda.
function getExponentialRandomNumber(rate) {
return Math.log(1 - Math.random()) / (−rate);
}
View accept-reject.js
// Acceptance rejection technique
function AcceptReject() {}
AcceptReject.prototype.inverseG = function(x) {
return -1 * Math.log((1 - x) / 2.85);
};
AcceptReject.prototype.f = function(x) {
return 20 * x * Math.pow(1 - x, 3);
};
@ryanseys
ryanseys / poisson-packet.js
Created Mar 17, 2015
First N Packet Poisson Arrival Times
View poisson-packet.js
function firstNPacketPoissonArrivals(n, rate, t) {
n--;
t = t || 0;
var u, x;
var lambda = rate;
u = Math.random(); // random value between [0, 1]
x = (-1/lambda)*Math.log(1-u);
t = t + x;
console.log('Arrival of packet at time: ' + t);
if (n > 0) {
@ryanseys
ryanseys / mailgun.php
Created Mar 15, 2015
Sending a message with Mailgun API using PHP (no dependencies needed)
View mailgun.php
<?php
function send_mailgun($email, $body) {
$domain = "YOUR_DOMAIN.COM";
$config = array();
$config['api_key'] = "YOUR_KEY_HERE";
$config['api_url'] = "https://api.mailgun.net/v2/" . $domain . "/messages";
$message = array();
$message['from'] = "Mailgun <YOU@YOUR_ADDRESS_HERE.com>";
@ryanseys
ryanseys / reverse-decimal.js
Created Mar 15, 2015
Reverse the numbers after decimal in list of numbers
View reverse-decimal.js
function reverseDecimal(num) {
var arr = num.toString().split('.');
arr[1] = arr[1].split('').reverse().join('');
return parseFloat(arr.join('.'));
}
var numbers = [0.24, 0.35, 0.76];
console.log(numbers.map(reverseDecimal));