This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Just a simple relationship | |
| class Voter { | |
| private $id; | |
| private $votedTo; | |
| private $votedAt; | |
| public function votesTo(Party $party) { | |
| if ($this->votedTo) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 '() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn nilakantha [iterations] | |
| (loop [sum 3.0M | |
| counter 0 | |
| sign +] | |
| (if (>= counter (* 2 iterations)) | |
| sum | |
| (recur | |
| (sign sum | |
| (/ 4.0M | |
| (* (+ counter 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class StreamHttpClient implements HttpClient | |
| { | |
| public function request($url, $method, array $parameters = []) | |
| { | |
| $content = http_build_query($parameters); | |
| $options = ['method' => $method]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Let lambda syntactic sugar | |
| (let [var1 exp1 | |
| var2 exp2] | |
| (do-something var1 var2)) | |
| ((fn [var1 var2] | |
| (do-something var1 var2)) | |
| (exp1 exp2)) |