Fundamentals
Code
Diff
  • (ns power.core)
    
    (defn power [n e]
      (condp = e
        0 1
        (* n (power n (dec e)))))
  • 1
    function power_of(int $x, int $y) {
    
    2
      return $x ** $y;
    
    3
    }
    
    1+
    (ns power.core)
    
    44
    5
    $test->describe("Basic Power Function", function () {
    
    6
      global $test;
    
    7
      $test->it("should always return when the power is 0", function () {
    
    8
        global $test;
    
    9
        $test->assert_equals(power_of(1, 0), 1);
    
    3+
    (defn power [n e]
    
    4+
      (condp = e
    
    5+
        0 1
    
    6+
        (* n (power n (dec e)))))
    
Fundamentals
Code
Diff
  • (ns hello.core)
    
    (println "Hello Clojure")
  • 1
    #include <iostream>
    
    2
    #include <string>
    
    1+
    (ns hello.core)
    
    33
    4
    int helloCplusplus(){
    
    5
      std::string str = "Hello, C++!";
    
    6
      std::cout << str << '\n';
    
    7
      return 0;
    
    8
    }
    
    3+
    (println "Hello Clojure")
    
Fundamentals
Code
Diff
  • package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	fmt.Println("Hello Golang")
    }
  • 1
    #include <iostream>
    
    2
    #include <string>
    
    1+
    package main
    
    33
    4
    int helloCplusplus(){
    
    5
      std::string str = "Hello, C++!";
    
    6
      std::cout << str << '\n';
    
    7
      return 0;
    
    3+
    import (
    
    4+
    	"fmt"
    
    5+
    )
    
    6+
    7+
    func main() {
    
    8+
    	fmt.Println("Hello Golang")
    
    88
    }
    
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 +)
  • 1
    add = -> a, b { a + b }
    
    2
    square = -> x { x * x }
    
    1+
    (ns basics.core)
    
    33
    4
    # sigh, if only Ruby supported pipes!
    
    5
    result = square.(add.(add.(add.(1,2), 3), 4))
    
    3+
    (defn add [a b] (+ a b))
    
    4+
    (defn square [x] (* x x))
    
    5+
     
    
    6+
    (def result (-> (add 1 2) (add 3) (add 4) (square)))
    
    66
    7
    puts result
    
    8+
    (println result)
    
    9+
    10+
    ;; It is better (without def), but it is more difficult to test:
    
    11+
    ; (let [result (-> (add 1 2)
    
    12+
    ;                  (add 3)
    
    13+
    ;                  (add 4)
    
    14+
    ;                  (square))]
    
    15+
    ;  (print result))
    
    16+
    17+
    ;; Shorter:
    
    18+
    ; (def add #(+ %1 %2))
    
    19+
    ; (def square #(* %1 %1))
    
    20+
    21+
    ;; Or maybe just:
    
    22+
    ; (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