Skip to content

Instantly share code, notes, and snippets.

View mdallastella's full-sized avatar

Marco Dalla Stella mdallastella

View GitHub Profile
@mdallastella
mdallastella / graph.clj
Created April 26, 2015 22:15
Clojire stateless graph implementation
(ns graph)
(defrecord Node [properties])
(defn new-node
[properties] (Node. properties))
(defrecord Edge [node properties weight])
(defn new-edge
([node] (Edge. node {} 0))
([node properties] (Edge. node properties 0))
@mdallastella
mdallastella / gist:b2819acc8ab388a10136
Last active August 29, 2015 14:18
Function returning the result the rolling of a dice with n sides.
(defn roll-dice [sides]
(let [dice (map inc (take sides (range)))]
(rand-nth dice)))
(roll-dice 6) ;; => random number between 1 and 6
;; This is much better:
(defn roll-dice [sides]
(inc (rand-int sides)))
#!/usr/bin/env python
def to_base_ten(str_num):
i = 0
base10 = 0
for num in reversed(str_num):
base10 += int(num) * pow(3, i)
i += 1
return base10
@mdallastella
mdallastella / gist:59f3dd14047ce4c7cf2a
Created July 28, 2014 12:39
Bootstrap 3 Inline Form with custom width
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Forms test</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
'use strict';
// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
if (arguments.length<=1) {
return function() {
return that.apply(owner, arguments);
@mdallastella
mdallastella / gist:3162955
Created July 23, 2012 10:15
Simple js switch with no switch statement.
var sw = new Object();
sw.square = function (x) { return x * x; }
sw.cube = function (x) { return x * (x * x); }
var x = 2
var name = 'square';
sw[name](x) // => 4