Skip to content

Instantly share code, notes, and snippets.

View keyvanakbary's full-sized avatar

Keyvan Akbary keyvanakbary

View GitHub Profile
@keyvanakbary
keyvanakbary / httpurl.coffee
Last active February 4, 2021 10:16
Http URL regex based on RFC 1738
class HttpUrl
ALPHA = "[a-zA-Z]"
DIGIT = "[0-9]"
DIGITS = "#{DIGIT}+"
SAFE = "[-$_.+]"
EXTRA = "[!*'(),]"
ALPHADIGIT = "[a-zA-Z0-9]"
TOPLABEL = "#{ALPHA}(#{ALPHADIGIT}|-)*#{ALPHADIGIT}|#{ALPHA}"
@keyvanakbary
keyvanakbary / currying.js
Last active August 29, 2015 14:02
Delicious currying
//Module with boilerplate/duplicated code
var Module = function (elements) {
var
onLink1Click = function (callback) {
elements.link1.on("click", function (e) {
callback();
e.preventDefault();
});
},
onLink2Click = function (callback) {
@keyvanakbary
keyvanakbary / currying.clj
Last active August 29, 2015 14:02
Delicious currying
; Tons of duplicated code :'(
(defn insert-l [a s l]
(cond
(empty? l) `()
(= (first l) s) (cons a l)
:else (cons (first l) (insert-l a s (rest l)))))
(defn insert-r [a s l]
(cond
(empty? l) `()
@keyvanakbary
keyvanakbary / cps.clj
Last active August 29, 2015 14:02
Continuation-passing style
; calculate the sum of odd numbers, the product of even ones and a list of the odds
; example extracted from the book "The Little Schemer"
; more info here http://stackoverflow.com/questions/10692449/the-little-schemer-evens-onlyco
(defn evens-only*&co [l col]
(cond
(empty? l) (col `() 1 0)
(number? (first l))
(cond
(even? (first l)) (evens-only*&co
(rest l)
; pyramid of death
(defn baz [n1 n2]
(cond (> n1 n2) (+ n1 n2)
(< n1 n2) (cond (= n1 0) 1
(= n2 0) 2
:else 3)
:else 0))
; internal extraction
(defn baz [n1 n2]
@keyvanakbary
keyvanakbary / jquery-draft.js
Last active June 26, 2018 07:09
Jquery plugin for temp drafts in forms
/*global $,localStorage*/
$.fn.draft = function () {
'use strict';
if (!localStorage) {
return;
}
var form = $(this),
fields = form.find('input,select,textarea'),
@keyvanakbary
keyvanakbary / stack.clj
Last active August 29, 2015 14:05
Stack
(defrecord Node [head tail])
(defn create [value]
(Node. value nil))
(defn push [stack value]
(Node. value stack))
(defn peek [stack]
(:head stack))
@keyvanakbary
keyvanakbary / binary-tree.clj
Created September 3, 2014 14:43
Immutable and persistent Binary Tree
(defrecord Node [value left right])
(defn create [value]
(Node. value nil nil))
(defn insert [{:keys [left right value] :as tree} v]
(if tree
(cond
(< v value) (Node. value (insert left v) right)
(> v value) (Node. value left (insert right v))
@keyvanakbary
keyvanakbary / cps.coffee
Last active August 29, 2015 14:06
Continuation-passing style
#Direct style
factorial = (n) ->
return 1 if n is 0
n * factorial(n-1)
console.log factorial(3)
#6
#Continuation-passing style
eq_ = (n1, n2, fn) ->
@keyvanakbary
keyvanakbary / prototype-vs-object-create.js
Created October 16, 2014 17:03
Prototype vs Object.create
//CLASSIC PROTOTYPE
var Rectangle = function (width, height) {
this.width = width;
this.height = height;
}
Rectangle.prototype.area = function() {
return this.width * this.height;
}