Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clojure.  Follow instructions on lynda.com  Can install all this in other ways, but this is the easiest way to get a development environment working.

Similar presentations


Presentation on theme: "Clojure.  Follow instructions on lynda.com  Can install all this in other ways, but this is the easiest way to get a development environment working."— Presentation transcript:

1 Clojure

2  Follow instructions on lynda.com  Can install all this in other ways, but this is the easiest way to get a development environment working  Mac  Must have java 1.6 or higher  Must have homebrew (http://brew.sh)http://brew.sh ▪ Copy script and run in terminal  Install the leiningen system and clojure  brew install leiningen

3  Must have java  Go to leiningen web site  Download installer  installs

4  Go to terminal (Mac) or cmd (Windows)  Type “lein repl”  User ==>  Type “(println “Hello World”)  Should print to terminal  Type “exit”  This is the simplest and least effective way of running clojure

5  To start a new project on a Mac:  Go to terminal  Type “lein new hello”  “lein” is for the project manager leiningen  “new” means start a new project  “hello” is the name of the project  Will create a folder called hello leiningen is a project manager. We’ll use it to create Clojure projects

6  Note: this is very tricky. The instructions in the video did not work.  There are lots of plug-ins for emacs + clojure, most require some tweaking.  Easier to use intellij.

7  Download intellij  You can use the community version  Mac: open the dmg, drag to applications folder  Windows: download the installer and run

8  Run intellij  Can keep the plug-ins, accept defaults  Must install “cursive”  On Mac: note that lynda video is wrong!  Ignore the “new project” screen.  Preferences -> plugins, then choose Cursive  Requires a free license; go to web site  Will ask for this when you first load it

9  Run intellij  Can keep the plug-ins, accept defaults  Must install “cursive”  On Windows: note that lynda video is wrong!  On start up dialog box in bottom right, click “Configure” then choose “Plugins”.  Choose “Browse repositories” button on bottom  Search for Cursive.  Install, restart intellij ▪ Requires a free license; go to web site ▪ Will ask for this when you first load it ▪ Register on Cursive web site: https://cursive-ide.comhttps://cursive-ide.com ▪ Click on the “Get a license” button. ▪ Note that you must copy the entire license from opening comment to closing comment.

10  Must download files from lynda.com  There’s a link on the “up and running with Clojure” page

11  Import project  This is an option when you first start intellij ▪ Or choose File  New  Project from existing sources  Find the “ch02” project that we downloaded from lynda.com ▪ Select the “ch02” project folder  Select “leiningen” as the project model. Click next  Click next again  Now intellij asks for the version of java. Default is fine. If there are none listed, click “+”, jdk, then use the default.

12  In the navigator window on the left  Open the ch02 folder then the src folder then the ch02 folder  Double click “core.clj”  Now go to button in bottom left corner.  Hover, then choose “repl”  Window on right opens up  Right click on project name “ch02” in nav bar on left  Choose “create REPL for hello”  Click “ok” in dialog box. Wait for it to start.  Right click on project name again  Choose “Run REPL for Hello  Wait for it to start (can take a long time)  Right click in the center window (with “hello.core” at the top)  At the bottom of the dialog box choose “REPL” then “Load file in REPL”  Right click in the center window again  Choose REPL then “Switch REPL NS to current file” NOTE!! You might have to be on the internet to resolve dependencies! Only do this once

13  In the box at the bottom right  Type “(get-x)”  Shift-enter  Solution appears in the REPL

14  Open intellij  Choose “import project”  Find the files from lynda.com on your computer  Choose the ch02 folder  On next window check “import from external model” then choose “leiningen”  On next window just click “next” to accept the project directory  On next window “Select Leiningen projects to import” just click “next”  On jdk page just click “next”. If you don’t see any paths in the window, you might have a problem.  On next page you can select a new project name or accept the default “ch02”. Click “Finish”  In dialog box select “Yes” when it says that the folder already exists.  You might have to enter your license for Cursive.

15  In the nav bar on left  Open “ch02”, then Open “src”, then Open “ch02” again  Double click “core.clj”  Right click on project name (ch02)  Choose “Run REPL for ch02  Wait for it to start (can take a long time)  In the bottom right panel type: (println “Hello World”)  should result in: Hello World =>nil

16  How do we load the file in the editor in the REPL?  Right click in the center window (with “ch02.core” at the top)  At the bottom of the dialog box choose “REPL” then “Load file in REPL”  Right click in the center window again  Choose REPL then “Switch REPL NS to current file”  Now can use the forms defined in the file in the REPL window  Example: type the letter a in the bottom right hand box (this is the REPL)  The display in the box above prints a => 2

17  Read  Evaluate  Print  Loop Clojure is a compiled language. When you hit the “enter” key, the code that you entered is compiled into a Java class and then run. The code that you entered is called a “form”. Clojure is a one pass compiler. So functions must be defined before they can be used (exception: the declare statement)

18  Can define variables with def  And use them.  If you load the namespace, can see results in the REPL  Can define functions with defn  If use a function before it is defined, must declare that function first. (def a 2) (def b (+ 40 a) ) (declare get-y) (defn get-x [ ] (+ (get-y) 4)) (defn get-y [ ] 38)

19  Scope  Global scope  Everything defined in global scope is accessible  Go to ch02 file and uncomment the #_ line.  What happens when you load the file into the REPL? (def a 2) (def b (+ 40 a) ) (defn fire-the-MISSILES!!! [are-you-sure?] (if are-you-sure? (println "WWooooosh!") (println "zzzzzzzzz"))) #_(fire-the-MISSILES!!! true)

20  Writing functions  Examine the functions in the ch02 file  How do you define a function?  How do you create parameters?  What does the function “i-have-two-arities” do?  How does a function return a value?

21  Writing functions  In the editor, at the bottom of the page, create a function that takes one parameter, multiplies it by 12,adds 6, divides by 3, and returns the value

22 (defn i-have-two-arities ([x] (println x)) ([x y] (println (+ x y))))

23 ;; variadic parameters (defn i-like-everything! [& argument-list] (println argument-list)) ;; variadic parameters (defn i-like-something! [item & argument-list] (println item argument-list))

24 defn make-a-fn-that-knows-the-secret [password] (let [the-secret password] (fn [password] (if (= password the-secret) :ok :fail!)))) (def check-password (make-a-fn-that-knows-the-secret "cats")) When run: (check-password “cats”)  :ok (check-password “dogs”) => :fail!


Download ppt "Clojure.  Follow instructions on lynda.com  Can install all this in other ways, but this is the easiest way to get a development environment working."

Similar presentations


Ads by Google