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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / time-server.php
Last active December 25, 2015 08:49
PHP time server (example PHP server implementation). Run the server from command line, specifying port parameter and connect to it from outside (could be any platform that is able to open a socket or just a telnet connection: `telnet localhost <port>`). The server writes back current time in RFC 2822 and immediately closes the connection.
#!/usr/bin/php -q
<?php
set_time_limit(0);
ob_implicit_flush();
if ($argc != 2)
die("Simple Time Server (C) 2013 Tomasz Ducin\n" .
"Invalid parameters passed\n" .
"Run:\n\t./time-server.php <port>\n" .
@ducin
ducin / class.js
Last active December 26, 2015 08:59
JavaScript class implementation (with example PersonClass declaration)
var Class = function(base){
if (typeof base.construct !== "function")
throw new TypeError("class definition has to define 'construct' function");
return function() {
// copy all methods
for (var prop in base) {
if (base.hasOwnProperty(prop) && base[prop] !== base.construct) {
this[prop] = base[prop];
}
}
@ducin
ducin / fork.py
Created November 3, 2013 12:38
scanning parent/child process IDs using os.fork
import os
parent_pid = os.getpid()
print "[parent] starts PID: %d" % (parent_pid, )
forked_pid = os.fork()
if forked_pid == 0:
print "[child] child process can't use os.fork() PID, since it's %d" % (forked_pid, )
print "[child] but it can reevaluate os.getpid() to get it's own PID: %d" % (os.getpid(), )
else: