Changes between Version 70 and Version 71 of Clojure Client Tutorial


Ignore:
Timestamp:
Aug 23, 2011 2:10:57 PM (13 years ago)
Author:
kedwar10
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Clojure Client Tutorial

    v70 v71  
    2525* Clojure Docs [http://clojuredocs.org/].
    2626* Statistical Computing [http://incanter.org/]
    27 * Marginalia (literate computing) [http://fogus.me/fun/marginalia/]
     27* Literate Programming [http://fogus.me/fun/marginalia/]
    2828* Pattern Matching and Predicate Dispatch [https://github.com/swannodette/match]
    2929* Logic Programming [https://github.com/clojure/core.logic]
     
    9393user=> (map inc [1 2 3 4 5])
    9494(2 3 4 5 6)
    95 user=> (map (fn [e] (* e 2)) [1 2 3 4 5]) ; an anonymous function
     95user=> (map (fn [e] (* e 2)) [1 2 3 4 5]) ; an anonymous function that doubles its argument
    9696(2 4 6 8 10)
    9797user=> (map #(* % 2) [1 2 3 4 5]) ; same, but with alternative syntax       
    9898(2 4 6 8 10)
    99 user=> ((juxt inc dec) 1)         
    100 [2 0]
    101 }}}
    102 
    103 The client behaviour more closely corresponds to the "reduce" function.
    104 
    105 {{{
    10699user=> (reduce + [1 2 3 4 5])
    10710015
    108 }}}
     101user=> (reduce #(cons %2 %1) (list) [1 2 3 4 5]) ; reverse a list
     102(5 4 3 2 1)
     103}}}
     104
     105Another useful concept is that of "closures".  Generally these appear in the context of a "let over lambda".  For example,
     106
     107{{{
     108user=> (let [a (atom 0)]
     109         (defn counter []
     110           (swap! a inc)))
     111#'user/counter
     112user=> (counter)
     1131
     114user=> (counter)
     1152
     116user=> (counter)
     1173
     118}}}
     119
     120In this case, counter "closes" over the a lexical variable "a" and prevents it from going out of scope.
    109121
    110122=== Basic Functionality ===