Skip to content

Instantly share code, notes, and snippets.

View ducin's full-sized avatar
🛠️
creatin' some stuff

Tomasz Ducin ducin

🛠️
creatin' some stuff
View GitHub Profile
@ducin
ducin / bbmodal-immediate.js
Created August 14, 2013 20:27
backbone + bootstrap/modal - example (immediate modal display)
var ModalView = Backbone.View.extend({
tagName: 'p',
template: 'this is modal content',
render: function() {
this.$el.html(this.template);
return this;
}
});
$(document).ready(function() {
@ducin
ducin / bbmodal.js
Created August 14, 2013 20:11
backbone + bootstrap/modal example
var MainView = Backbone.View.extend({
el: 'body',
events: {
'click #open': 'openModal'
},
template: '<a id="open" class="btn">open modal</a>',
openModal: function() {
var view = new ModalView();
var modal = new Backbone.BootstrapModal({
content: view,
@ducin
ducin / iterator.js
Created August 6, 2013 10:12
stateful functions - generating unique id for objects and iterating collections
(function () {
function iterator(collection) {
var index = 0;
var length = collection.length;
function next() {
var item = collection[index++];
return item;
}
@ducin
ducin / fib.js
Created August 6, 2013 09:07
caching function results (single argument function)
function fib(x) {
if (x < 2)
return 1;
return fib(x-1) + fib(x-2);
}
@ducin
ducin / curry.js
Created August 6, 2013 07:28
currying is a mechanism to pre-fill arguments of a later function call
if (!Function.prototype.curry) {
(function () {
var slice = Array.prototype.slice;
Function.prototype.curry = function () {
var target = this;
var args = slice.call(arguments);
return function () {
var allArgs = args;
@ducin
ducin / bind.js
Last active December 20, 2015 10:30
binding "this" object to a function
// binding "this" object only (function arguments are ignored)
if (!Function.prototype.bind) {
Function.prototype.bind = function bind(thisObject) {
var fun = this;
return function() {
fun.apply(thisObject, arguments);
};
};
}
@ducin
ducin / bind.js
Created July 30, 2013 18:47
binding both "this" object and optionally arguments
// binding both "this" object and optionally arguments
if (!Function.prototype.bind) {
Function.prototype.bind = function bind(thisObject) {
var fun = this, boundArgs = Array.prototype.slice.call(arguments, 1);
return function() {
var allArgs = boundArgs;
if (arguments.length) {
allArgs = allArgs.concat(Array.prototype.slice.call(arguments));
}
@ducin
ducin / sum_dict_switch.py
Created July 10, 2013 21:12
summing up python dictionary values: add all values, where type='+' should be added, type='-' should be subtracted and other type should be ignored
# define entries
entries = [{'type': '+', 'value': 9},\
{'type': '+', 'value': 12.3},\
{'type': '-', 'value': 4},\
{'type': '+', 'value': 0.89},\
{'type': '-', 'value': 2.65},\
{'type': '-', 'value': 14.3},\
{'type': '+', 'value': 6.8},\
{'type': 'unknown', 'value': 200}]
@ducin
ducin / time_clock.py
Created July 10, 2013 21:28
benchmarking/timing python instructions
import time
start = time.clock()
for x in range(1,10000):
y = x**3
elapsed = time.clock() - start
print elapsed
@ducin
ducin / ich-template-loader.js
Created April 7, 2013 15:53
ICanHaz templates loader object - load all your templates after the website has been initially rendered. See http://symfony-world.blogspot.com/2013/04/managing-lots-of-icanhaz-templates.html for details on concatenating template files.
var TemplateLoader = {
path: 'templates/', // template files are stored in this directory
templates: ['file1', 'file2', 'file3'], // put all your template files here
fetchTemplate: function(path) {
$.ajax({
type: 'GET',
dataType: 'text',
async: false,
url: path
}).done(function(response) {