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 / 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'),
DROP FUNCTION IF EXISTS lowerword;
DELIMITER |
CREATE FUNCTION lowerword( str VARCHAR(128), word VARCHAR(5) )
RETURNS VARCHAR(128)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE loc INT;
SET loc = LOCATE(CONCAT(word,' '), str, 2);
public class Solution {
public void traverse(TreeNode root) {
Queue<TreeNode> nodes = new ArrayList();
nodes.add(root);
while (!nodes.isEmpty()) {
TreeNode node = nodes.remove();
System.out.println(node.val);
if (node.left != null) queue.add(node.left);
const cons = (x, y) => (m) => m(x, y);
const car = (z) => z((p, q) => p);
const cdr = (z) => z((p, q) => q);
const pair = cons(1, 2);
console.log(car(pair), cdr(pair));//1 2
const list = cons(1, cons(2, cons(3, cons(4, null))));
const each = (l, fn) => cdr(l) ? (() => {fn(car(l)); each(cdr(l), fn)})() : fn(car(l));
each(list, console.log);//1 2 3 4
@keyvanakbary
keyvanakbary / object.scm
Created March 13, 2016 20:36
Modelling objects
(define (create-user name)
(define say-hello
(print "Hello " name))
(define (change-name new-name)
(set! name new-name)
name)
(define (dispatch method)
(cond
((eq? method 'say-hello) say-hello)
((eq? method 'change-name) change-name)
@keyvanakbary
keyvanakbary / stateful-accumulator.clj
Created March 13, 2016 20:57
Stateful Accumulator
(defn make-acumulator [value]
(def acc (atom value))
(fn [add]
(swap! acc #(+ % add))
@acc))
(def a (make-acumulator 10))
(a 10)
;20

Quick install PHP 7.0:

1. Install depends PHP 7.0
$ brew install autoconf automake gmp homebrew/versions/bison27 gd freetype t1lib gettext zlib mcrypt
2. Configure PHP 7.0
$ git clone --depth=1 https://github.com/php/php-src.git

$ cd php-src

@keyvanakbary
keyvanakbary / let-syntactic-sugar.clj
Created January 12, 2016 14:41
Let is just a syntax sugar over lambdas
; Let lambda syntactic sugar
(let [var1 exp1
var2 exp2]
(do-something var1 var2))
((fn [var1 var2]
(do-something var1 var2))
(exp1 exp2))
; LINEAR RECURSION
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
(factorial 3)
;(* 3 (factorial 2))
;(* 3 (* 2 (factorial 1)))
;(* 3 (* 2 1))