Skip to content

Instantly share code, notes, and snippets.

View fidel-karsto's full-sized avatar
🚀
¯\_(ツ)_/

Karsten Rieger fidel-karsto

🚀
¯\_(ツ)_/
View GitHub Profile
@fidel-karsto
fidel-karsto / Field of View Pseudocode
Created June 21, 2013 21:27
Pseudocode for field of view determination. Quite slick - real life example: http://noelfb.tumblr.com/post/53528361309
// from http://roguebasin.roguelikedevelopment.org/index.php?title=Eligloscode
void FOV()
{
float x,y;
int i;
CLEAR_MAP_TO_NOT_VISIBLE();//Initially set all tiles to not visible.
for(i=0;i<360;i++)
{
x=cos((float)i*0.01745f);
@fidel-karsto
fidel-karsto / CachingFunction.js
Last active December 19, 2015 15:09
cache a function and its return value
var memo = function (fn) {
var cache = {},
toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
return function () {
var key = [].join.call(arguments, '§') + '§';
if (!(key in cache)) {
cache[key] = fn.apply(this, arguments);
}
@fidel-karsto
fidel-karsto / curry.js
Last active December 19, 2015 15:19
curry / schönfinkel example
// http://blog.mgechev.com/2013/01/21/functional-programming-with-javascript/
/* By Stoyan Stafanov */
function schonfinkelize(fn) {
var slice = Array.prototype.slice,
stored_args = slice.call(arguments, 1);
return function () {
var new_args = slice.call(arguments),
args = stored_args.concat(new_args);
return fn.apply(null, args);
};
@fidel-karsto
fidel-karsto / loops.js
Last active December 19, 2015 15:58
performance comparison of different loop technics
var size = 10e6, // array size - the larger the better
// http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
},
// modify function used in loop peration
modifyFn = function(val) {
return val * 2;
},
// caching initialisation result for repeated use
@fidel-karsto
fidel-karsto / namespaceCreator.js
Last active December 20, 2015 11:59
Convenient function to create namespaces by passing in a string and a scope object.
var namespace = function namespace (packagesStr, context) {
var myContext = context || window,
packages = (packagesStr && typeof packagesStr === "string") ? packagesStr.split(".") : [],
create = function create (pkg, folders) {
if (pkg && typeof this[pkg] === "undefined") {
this[pkg] = {};
}
if (folders && folders.length > 0) {
return create.call(this[pkg], folders.shift(), folders);
}
@fidel-karsto
fidel-karsto / calculusCollection.js
Last active December 20, 2015 17:29
some calculations that might come in handy some time.
/*
Problem: how many collectible packets need to be bought to achieve a complete set,
when you don't know which one is in the packet.
i.e. There are 16 unique trading cards to complete a set. Each is sold in a sealed enclosing.
How many of them would it take to collect all 16?
answer: about 54!
https://plus.google.com/u/0/104223386721240823159/posts/goRYigJ4RGB
*/
var countCollectibles = function (collectibleCount) {
var eulerConstant = 0.5772156649;
@fidel-karsto
fidel-karsto / aop.js
Created August 22, 2013 11:53
Some basic AOP style functions.
var before = function(fn, pre) {
return function() {
pre.apply(this, arguments);
return fn.apply(this, arguments);
};
},
after = function(fn, aft) {
return function() {
var result = fn.apply(this, arguments);
aft.apply(this, result);
Array.prototype.shuffle = Array.prototype.shuffle || (function () {
function shuffle(array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
@fidel-karsto
fidel-karsto / grid.less
Created April 2, 2015 20:24
Example of a dynamic responsive grid system.
// DyRGS - dynamic responsive grid system
// @author: karsten.rieger@tritium-design.de
@gutter: 1.5625%; // ~= 10px gutter @ 320px width
@mobile-column-count: 4;
@tablet-column-count: 8;
@desktop-column-count: 12;
@mobile-selector: m;
@tablet-selector: t;
@desktop-selector: d;
// UMD dance - https://github.com/umdjs/umd
!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';