Ad
Fundamentals
Code
Diff
  • (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)))))
Fundamentals
Code
Diff
  • (ns hello.core)
    
    (println "Hello Clojure")
    • #include <iostream>
    • #include <string>
    • (ns hello.core)
    • int helloCplusplus(){
    • std::string str = "Hello, C++!";
    • std::cout << str << '\n';
    • return 0;
    • }
    • (println "Hello Clojure")
Fundamentals
Code
Diff
  • package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	fmt.Println("Hello Golang")
    }
    • #include <iostream>
    • #include <string>
    • package main
    • int helloCplusplus(){
    • std::string str = "Hello, C++!";
    • std::cout << str << '\n';
    • return 0;
    • import (
    • "fmt"
    • )
    • func main() {
    • fmt.Println("Hello Golang")
    • }
Code
Diff
  • (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 +)
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