Skip to content

Instantly share code, notes, and snippets.

View keyvanakbary's full-sized avatar

Keyvan Akbary keyvanakbary

View GitHub Profile
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 / 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
@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)

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))
@keyvanakbary
keyvanakbary / StreamHttpClient.php
Created March 6, 2015 07:39
Simple HTTP client for GET and POST methods
<?php
class StreamHttpClient implements HttpClient
{
public function request($url, $method, array $parameters = [])
{
$content = http_build_query($parameters);
$options = ['method' => $method];
@keyvanakbary
keyvanakbary / pi.clj
Last active August 29, 2015 14:14
Nilakantha's series for calculating π
(defn nilakantha [iterations]
(loop [sum 3.0M
counter 0
sign +]
(if (>= counter (* 2 iterations))
sum
(recur
(sign sum
(/ 4.0M
(* (+ counter 2)