Last active
December 19, 2016 17:24
-
-
Save bowbahdoe/8bb9c4b7d4d895569688c3b5fa3384f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; BASIC LISP SYNTAX TUTORIAL | |
| ;; Calls the function '+' on the arguments 2 and 2 | |
| ;; Returns the value 4 | |
| (+ 2 2) | |
| ;; Calls the function 'println' on the result of | |
| ;; the call to (+ 2 2). Returns no value | |
| (println (+ 2 2)) | |
| ;; Functions that return no value generally have SIDE-EFFECTS | |
| ;; SIDE-EFFECTS are anything that affects some external or internal state | |
| ;; Functions that have no SIDE-EFFECTS and dont rely on any state are considered PURE | |
| ;; So the function + is PURE, because its result depends only on the arguments passed | |
| ;; The function println is IMPURE, because it has a side effect | |
| ;; To declare a variable | |
| (def four (+ 2 2)) | |
| (println four) ;; -> 4 | |
| ;; To define a function | |
| (defn function-name [argument-list] | |
| body-expression) | |
| ;; Example of a function with 1 argument | |
| ;; functions implicitly return the very last statement | |
| (defn square [x] | |
| (* x x)) | |
| ;; Example of a function with zero | |
| (defn returns-3 [] | |
| 3) | |
| (def number (square 3)) ;; 9 | |
| ;; the if function has two forms | |
| ;; The two argument form | |
| (if (condition) | |
| (if-true) | |
| (if-false)) | |
| ;; and the one argument form | |
| (if (condition) | |
| (if-true)) | |
| ;; The two argument form returns the first expression if the condition | |
| ;; is true and the second expression if it is false | |
| ;; the one argument form returns the first expression if true and nothing otherwise | |
| ;; We also have a cond function for more nested conditionals | |
| (cond [(condition1) (result1)] | |
| [(condition2) (result1)] | |
| [:else (result3)]) | |
| ;; which should be easy to figure out on its own, even with the magic-ey :else at the end | |
| ;; So a more complicated function would be | |
| (defn square [x] | |
| (if (< 0 x) | |
| (square (* -1 x)) | |
| (* x x))) | |
| ;; That is useless complexity, but you should be able to get the idea | |
| ;; Now for data structures | |
| 0 ;; A Number | |
| "hi" ;; A String | |
| 'a ;; A symbol | |
| :a ;; A keyword | |
| '(1 2 3) ;; A list | |
| (list 1 2 3) ;; can also be made with the list function | |
| [1 2 3] ;; A Vector | |
| (vector 1 2 3) ;; can also be made with the vector function | |
| {:key 'value1 | |
| "key 2" 'value2} ;; A Map | |
| (hash-map :key 'value1 | |
| "key 2" 'value2) ;; Seeing a pattern? | |
| #{1 2 3} ;; A Set | |
| ;; Ill go into detail in a bit, but for now take a peek at http://cljs.info/cheatsheet/ | |
| ;; For some basic functions to work with these structures | |
| ;; Symbols vs Keywords: | |
| ;; -- Honestly they work about the same, but keywords arent symbols, you can differentiate | |
| ;; quick diversion to something specific: React components | |
| ;; to make a simple component in reagent you need to make one of three things | |
| ;; 1: A function that returns a vector | |
| (defn my-simple-component [] | |
| [:p "hello"]) | |
| ;; If I mount that to root it will render as | |
| ;; <p> hello </p> | |
| ;; The vector you return uses https://github.com/weavejester/hiccup syntax rules | |
| ;; I.E. A vector with the first element as a key renders as <tag> (rest vector) </tag> | |
| ;; A vector with the first element as a function renders the component obtained by | |
| ;; calling that component function | |
| ;; If you supply a map as the second element it is used for properties, ex. below | |
| (defn my-styled-component [] | |
| [:p {:style {:color "red"}} "hello"]) | |
| ;; And here is an example of nesting components | |
| (defn my-nested-component [] | |
| [:div [my-simple-component] | |
| [my-styled-component]]) | |
| ;; This component would render to | |
| ;; <div><p> hello </p> <p style="color: red"> hello </p></div> | |
| ;; One other way to define a component is by supplying a function that returns a function, | |
| ;; and that function will be called to render the component | |
| ;; Note: (fn [args] body) makes annonymous functions | |
| (defn anotherone [] | |
| (fn [] | |
| [:p "hello"])) | |
| ;; If the component function has any arguments, the function returned must have those same arguments | |
| ;; (name doesnt neccisarilly matter, but it will be called with the same) | |
| ;; This has uses once I show you the let function | |
| ;; LETs do that now (fucking kill me) | |
| (defn hypotenuse [a b] | |
| (let [a-squared (* a a) | |
| b-squared (* b b)] | |
| (sqrt (+ a-squared b-squared)))) | |
| ;; Let === Local bindings | |
| ;; Basic form | |
| (let [name value ...] | |
| body) | |
| ;; MORE TO COME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment