Fundamentals
(ns power.core) (defn power [n e] (condp = e 0 1 (* n (power n (dec e)))))
function power_of(int $x, int $y) {return $x ** $y;}- (ns power.core)
$test->describe("Basic Power Function", function () {global $test;$test->it("should always return when the power is 0", function () {global $test;$test->assert_equals(power_of(1, 0), 1);$test->assert_equals(power_of(2, 0), 1);$test->assert_equals(power_of(6, 0), 1);$test->assert_equals(power_of(100, 0), 1);$test->assert_equals(power_of(1000, 0), 1);$test->assert_equals(power_of(99999, 0), 1);$test->assert_equals(power_of(666, 0), 1);$test->assert_equals(power_of(33, 0), 1);$test->assert_equals(power_of(123456789, 0), 1);$test->assert_equals(power_of(911, 0), 1);});$test->it("should work for other powers", function () {global $test;$test->assert_equals(power_of(1, 1), 1);$test->assert_equals(power_of(4, 2), 16);$test->assert_equals(power_of(2, 4), 16);$test->assert_equals(power_of(5, 3), 125);$test->assert_equals(power_of(2, 9), 512);$test->assert_equals(power_of(7, 3), 343);$test->assert_equals(power_of(6, 3), 216);$test->assert_equals(power_of(3, 6), 729);$test->assert_equals(power_of(9, 3), 729);$test->assert_equals(power_of(10, 10), 10000000000);$test->assert_equals(power_of(2, 53), 9007199254740992);});});- (defn power [n e]
- (condp = e
- 0 1
- (* n (power n (dec e)))))
;; TODO: TDD development using clojure.test framework
- ;; TODO: TDD development using clojure.test framework
Fundamentals
Fundamentals
(ns basics.core) (defn add [a b] (+ a b)) (defn square [x] (* x x)) (def result (-> (add 1 2) (add 3) (add 4) (square))) (println result) ;; It is better (without def), but it is more difficult to test: ; (let [result (-> (add 1 2) ; (add 3) ; (add 4) ; (square))] ; (print result)) ;; Shorter: ; (def add #(+ %1 %2)) ; (def square #(* %1 %1)) ;; Or maybe just: ; (def add +)
add = -> a, b { a + b }square = -> x { x * x }- (ns basics.core)
# sigh, if only Ruby supported pipes!result = square.(add.(add.(add.(1,2), 3), 4))- (defn add [a b] (+ a b))
- (defn square [x] (* x x))
- (def result (-> (add 1 2) (add 3) (add 4) (square)))
puts result- (println result)
- ;; It is better (without def), but it is more difficult to test:
- ; (let [result (-> (add 1 2)
- ; (add 3)
- ; (add 4)
- ; (square))]
- ; (print result))
- ;; Shorter:
- ; (def add #(+ %1 %2))
- ; (def square #(* %1 %1))
- ;; Or maybe just:
- ; (def add +)
(ns basics.core-test (:require [clojure.test :refer :all] [basics.core :refer [result]])) (deftest result-100? (testing "Result equals 100" (is result 100)))
Test.assert_equals(result, 100)- (ns basics.core-test
- (:require [clojure.test :refer :all]
- [basics.core :refer [result]]))
- (deftest result-100?
- (testing "Result equals 100"
- (is result 100)))
const fn = (f) => (...args) => args.reduce((f, arg) => f(arg), f);
// ==================================
const sum = fn(a => b => c => (a + b + c));
console.log(sum(1, 2, 3)); // 6
console.log(sum(1, 2)(3)); // 6
console.log(sum(1)(2)(3)); // 6
const doubleInc = sum(1)(1);
console.log(doubleInc(4)); // 6