Skip to content

Instantly share code, notes, and snippets.

View keyvanakbary's full-sized avatar

Keyvan Akbary keyvanakbary

View GitHub Profile
@keyvanakbary
keyvanakbary / intersect.clj
Created January 22, 2015 14:09
Intersection for sets
(defn intersect [s1 s2]
(loop [intersection #{}
s s1]
(cond
(empty? s) intersection
(contains? s2 (first s)) (recur (conj intersection (first s)) (rest s))
:else (recur intersection (rest s)))))
(reduce intersect [#{\a \b} #{\a \b} #{\a}])
;;=> #{\a}
(defn factorial [n]
(if (pos? n) (* n (factorial (dec n))) 1))
(defn calculate-e [x precision]
(if (pos? precision)
(+ (/ (Math/pow x precision) (factorial precision))
(calculate-e x (dec precision)))
1))
(dotimes [n (Integer/parseInt (read-line))]
(defn bubblesort [lns]
(let [swap
(fn swap [lns]
(if (or (= 1 (count lns)) (empty? lns))
lns
(cons (min (first lns) (second lns))
(swap (cons
(max (first lns) (second lns))
(drop 2 lns))))))]
(loop [sns '()
(defn insertionsort [lns]
(let
[insert
(fn insert [n lns]
(if (or (empty? lns) (< n (first lns)))
(cons n lns)
(cons (first lns)
(insert n (rest lns)))))]
(loop [sns '()
lns lns]
(ns listy.list)
(defn create []
{:items (sorted-map) :current nil :index 0})
(defn add [l]
(let [idx (inc (:index l))]
(-> l
(assoc-in [:items idx] {:title "" :items (sorted-map)})
(assoc :index idx)
@keyvanakbary
keyvanakbary / votes.php
Created October 28, 2014 23:24
Voter example
<?php
//Just a simple relationship
class Voter {
private $id;
private $votedTo;
private $votedAt;
public function votesTo(Party $party) {
if ($this->votedTo) {
@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;
}
@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 / 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 / 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))