Skip to content

Instantly share code, notes, and snippets.

View PatrickJS's full-sized avatar

PatrickJS PatrickJS

View GitHub Profile
@PatrickJS
PatrickJS / FizzBuzz.rb
Last active December 17, 2015 17:48
FizzBuzz
1.upto(100){|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
=begin
1.upto(100) do |num|
puts"FizzBuzz#{num}"[ num%3<1 ? 0 : num%5<1 ? 4 : 8, num%15<1 ? 8 : 4 ]
end
=end
@PatrickJS
PatrickJS / calculator.rb
Created May 25, 2013 15:58
Simple calculator make in one ruby function
def calculator(expression, *args)
{
add: -> (a,b) { a + b },
subtract: -> (a,b) { a - b },
divide: -> (a,b) { a / b },
multiply: -> (a,b) { a * b},
power: -> (a,b) { a ** b }
}[expression].call(*args)
end
var fibonacci = function(n) {
return n < 2 ? n : arguments.callee(n - 1) + arguments.callee(n - 2);
};
@PatrickJS
PatrickJS / RegExp
Last active December 18, 2015 09:09
Regular Expression
===========================
| Match any url |
| -------------- |
| https://www.google.com/ |
| http://gdi2290.com/ |
| ftp://s3.amazom.com/ |
| -------------- |
| /(ht|f)tps?://.+/g |
===========================
@PatrickJS
PatrickJS / identity_monad.js
Last active December 18, 2015 20:59
Identity Monad - Take a value and give the value back with bind.
function MONAD() {
return function unit(value) {
var monad = Object.create(null);
monad.bind = function(func) {
return func(value);
};
return monad;
};
}
@PatrickJS
PatrickJS / ajax_monad.js
Last active February 12, 2018 23:13
Ajax Monad
function Monad() {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function(func, args) {
return func.apply(undefined, [value].concat(Array.prototype.slice.apply(args || [])));
}
return monad;
}
unit.lift = function(name, func) {
@PatrickJS
PatrickJS / maybe_monad.js
Last active January 27, 2016 03:01
Maybe Monad
function MONAD(modifier) {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function(func, args) {
return func.apply(undefined, [value].concat(Array.prototype.slice.apply(args || [])));
};
if (typeof modifier === 'function') {
modifier(monad, value);
}
// vow.js
// Douglas Crockford
// 2013-01-28
// Public Domain
/*global setImmediate */
var VOW = (function () {
@PatrickJS
PatrickJS / bubblesort.js
Created July 11, 2013 17:13
Bubble Sort in javascript
var bubbleSort = function(array) {
if (array.constructor !== Array) throw 'not an Array';
do {
var somethingWasSwapped = false;
// go through and...
for(var i = 0, ii = array.length; i < ii; i++) {
// if necessary
if (array[i] > array[i + 1]) {
// swap the two indices
somethingWasSwapped = true;
@PatrickJS
PatrickJS / iou.js
Created July 21, 2013 17:25
simple version of promises without then, when, chaining
var fs = require('fs');
module.export.fileObj = function(file, encode) {
var status = 'pending',
doneStack = [],
failStack = [],
done, fail, fullFail, fullData,
rect = {};
fs.fileRead(file, encode, function(data, err) {
if (!err) {