Skip to content

A simple project to put in practice Clojure concepts.

Notifications You must be signed in to change notification settings

gustavo-flor/clojure-noob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clojure Noob

A simple project to put Clojure concepts into practice.

Dependencies

  • Java
  • Leiningen

Usage

Execute REPL (Read-Eval-Print Loop)

$ lein repl

You should see something like this:

nREPL server started on port #### on host 127.0.0.1
REPL-y 0.5.1, nREPL 0.8.3
Clojure 1.10.3
Java HotSpot(TM) 64-Bit Server VM 17.0.1+12-LTS-39
Docs: (doc function-name-here)
      (find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e

Now inside REPL we'll execute the main class with:

(-main)

Also, we can execute

(+ 1 2 3 4)
;; 10

(* 1 2 3 4)
;; 24

(first [3, 7, 1, 2])
;; 3

(do (println "Hello World")
    (- 10 5 2))
;; Hello World
;; 3

💫 Happy Hacking!

Learning

Some examples and points of interest to stay connected.

From Java to Clojure

In Java we'll' use the static keyword because Clojure is a functional programming language.

Defining a variable

On Java:

public static final String name = "Gustavo Flôr"

On Clojure:

(def name "Gustavo Flôr")

We don't need to define an access or var type, we just define the symbol and value and that's it.

Also, in Clojure we don't create variables, we bind a value to a symbol. So the closes thing we can do in Java is to use the "final" keyword.


Defining a function

On Java:

public static Object sayHello(final String person) {
    return String.format("Hello %s!", person);    
}

On Clojure:

(defn say-hello 
  [person] 
  (str "Hello " person "!"))

First, we need to pay attention at the keyword "function", on Java we don't have functions we create methods on a class.

Also, on Clojure we don't need to define a return type, because it always returns the last form evaluated (in this example, as we only have one expression, this will be the return).

🚧 Work In Progress...

If you want more, read Clojure for the Brave and True

About

A simple project to put in practice Clojure concepts.

Resources

Stars

Watchers

Forks