Changes between Version 68 and Version 69 of Clojure Client Tutorial


Ignore:
Timestamp:
Aug 23, 2011 12:26:15 PM (13 years ago)
Author:
kedwar10
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Clojure Client Tutorial

    v68 v69  
    3737* Lisp source code for AIMA [http://aima.cs.berkeley.edu/lisp/doc/overview.html].
    3838
    39 Some advice for clojure programming:
     39Some advice for Clojure programming:
    4040* Always strive for pure functions (as in no side effects).  This is critical if you want to avoid refactoring your code later on. 
    4141* For tracing functions, use [http://richhickey.github.com/clojure-contrib/trace-api.html].
    4242* :pre and :post conditions can be used to put constraints on functions.  Here is an example [http://blog.fogus.me/2009/12/21/clojures-pre-and-post].
    43 * Prefer working from the leiningen REPL, avoid compiling and running.
     43* Prefer working from the leiningen REPL; avoid compiling and running.
    4444* If you define a record in a namespace and wish to access it in another, you must explicitly import it with the ":import" keyword in your namespace declaration.
    45 * Giving names to your anonymous functions will dramatically increase the usefulness of clojure error reporting (an example: (fn identity [x] x).  Doing this will also allow the function to call itself.
     45* Giving names to your anonymous functions will dramatically increase the usefulness of Clojure error reporting (an example: (fn identity [x] x).  Doing this will also allow the function to call itself.
    4646* If you want to ensure tail-call optimization, use the "recur" function instead of using your function name.  "recur" also works with loops.
    4747* "Weird" error messages usually result from forgetting to specify the arguments during a function definition or from mismatched parenthesis.
     
    5555== Organization of Code ==
    5656
    57 Once you have lieningen setup, cd to the client directory and type "tree" into the shell.
     57Once you have lieningen set up, cd to the client directory and type "tree" into the shell.
    5858
    5959This organization is the default for the leiningen projects created with the "lein new" command.
    6060
    61 docs contains and html file that provides a sort of annotated presentation of the code.  Read this file first to familiarize yourself with the organization of the project. Once you have made your own changes to the client and wish to update the documentation, run "lein marg" in the top level directory.  This invokes the marginalia jar in the lib directory and generates a new html file.
    62 
    63 In the top level directory there is a file called "project.clj". This file tells leiningen how your project is organized.  For details, consult the tutorial, but know that if you wish to add additional clojure libraries to your project, you will need to specify them here, and run "lein deps" to download them.  Specific directions for leiningen are provided nearly universally for clojure project, so just look them up as needed.
     61"docs" contains and html file that provides a sort of annotated presentation of the code.  Read this file first to familiarize yourself with the organization of the project. Once you have made your own changes to the client and wish to update the documentation, run "lein marg" in the top level directory.  This invokes the marginalia jar in the "lib" directory and generates a new html file.
     62
     63In the top level directory there is a file called "project.clj". This file tells Leiningen how your project is organized.  For details, consult the tutorial, but know that if you wish to add additional Clojure libraries to your project, you will need to specify them here, and run "lein deps" to download them.  Specific directions for Leiningen are provided nearly universally for Clojure project, so just look them up as needed.
    6464
    6565{{{
     
    7777}}}
    7878
    79 src contains all of the .clj files in your project.  The organization of the directory must correspond to the namespaces of your project so edit with care (more on this later, but one thing to note is that if you have a namespace with dash in it, "client.my-ns", for example, the actual filename must be "my_ns.clj" and it must be located in the client directory).
    80 
    81 test contains files that leiningen will use for testing your functions via the "lein test" command.  This functionality is not critical in clojure since you can debug easily from the REPL, but if you want to batch test functions then you can look into this.
    82 
    83 When you create a new project with leiningen, it will automatically provide a core.clj file for you.  If your namespaces are organized into a tree, this is the root.  When you run the REPL in the top level directory via the "lein repl" command, the core namspace becomes available to you.  This is why having leiningen on your path is useful, as you won't always want to load the core namespace.  An important detail: namespaces must NOT be cyclic in clojure, so plan accordingly when designing your project.
     79"src" contains all of the .clj files in your project.  The organization of the directory must correspond to the namespaces of your project so edit with care (more on this later, but one thing to note is that if you have a namespace with dash in it, "client.my-ns", for example, the actual filename must be "my_ns.clj" and it must be located in the client directory).
     80
     81"test" contains files that Leiningen will use for testing your functions via the "lein test" command.  This functionality is not critical in Clojure since you can debug easily from the REPL, but if you want to batch test functions then you can look into this.
     82
     83When you create a new project with Leiningen, it will automatically provide a core.clj file for you.  If your namespaces are organized into a tree, this is the root.  When you run the REPL in the top level directory via the "lein repl" command, the "core" namspace (or whatever namespace in project.clj is specified as ":main") becomes available to you.  This is why having leiningen on your path is useful, as you won't always want to load the core namespace.  An important detail: namespaces must NOT be cyclic in clojure, so plan accordingly when designing your project.
    8484
    8585----
     
    8787== Client Usage Tutorial ==
    8888
     89=== Summary ===
     90The client has several ways of writing messages to the server, each of which has different behaviour with respect to synchronization and reply processing.  The user must specify the way these functions operate by passing them data structures and functions which will be called on the data returned from the server.  This is analogous to the "reduce" or "foldl" functions found in Clojure and Scheme respectively.  If you are unfamiliar with using functions as arguments, then it may be worthwhile to experiment with in the REPL with either "map" or "reduce".  For example, 
     91
    8992=== Basic Functionality ===
    90 Now it is time to learn to use the client.  cd to the top level directory and typu "lein repl". If everything went smoothly you'll see something about no rlwrap (if you are working on one of the lab machines) and the prompt.
     93cd to the top level directory and type "lein repl". If everything went smoothly you'll see something about no rlwrap (if you are working on one of the lab machines) and the prompt.
    9194
    9295{{{