A Intro to Clojure and Clojure Syntax
Key Takeaways
This video introduces Clojure, a dynamically typed functional programming language, and breaks down its syntax by explaining three different functions statement by statement, covering topics such as recursion, macro usage, and function definition. The video also showcases the use of Clojure for tasks like generating the Fibonacci sequence and implementing the Pig Latin function.
Full Transcript
hi guys welcome to the introduction to closure tutorial my name is tensor from the tensor programming blog so today I thought we would take a little look at closure before that however I just wanted to make a few announcements for those of you who are waiting for the next elixir tutorials those are on their way they're just taking a little longer than I anticipated to actually write and to create also those of you who are still watching this channel because you're interested in some of the other languages that we've looked at like Al Margot don't worry we will go back and visit those languages basically I want to get a good base of languages onto this channel so the next language that we want to enter into our channel here is closure and we decided on closure through a poll on Twitter I put up a poll between closure Kotlin f-sharp and dart and for a long period of that poll dart was actually winning by quite a lot and then on the very last day a bunch of people came in and voted for closure and it ended up winning in a landslide closure is a dynamically typed functional programming language that runs on the JVM this of course allows it to provide interrupts with Java one of the major design goals of closure was to make it easier to implement concurrent applications that can access large amounts of data closure also has a build that runs on the.net platform so this means of course that it has interrupt with dotnet this is called closure CLR now this is a version that we probably won't touch on there is also a version called closure script which has interrupt with JavaScript and compiles down to JavaScript this version we definitely will look at later now one of the very nice things about closure is that it's got a very small core and this is mainly due to its heritage in Lisp closure is a derivative of Lisp and Lisp was a language that was created in 1958 it is the second oldest high order programming language ever created and Lisp stands for a list processor and you will actually see why here in a moment when we actually take a look at this code the other thing I kind of you guys to keep in mind as we're going through these functions is that lisped and closure treats code as data now this sort of statement will become more and more pertinent as we continue on all right so this tutorial is going to take sort of a different approach than what we did before so before with elm and with go and with elixir we just opened up a rebel he typed in a bunch of the different features of the programming languages we talked about him for a little bit then we started doing examples in this instance we're going to start by looking at examples however we're going to break these examples down and explain exactly how they're working I feel like this is a better way to at least teach this language to you guys all right so let's look at our first function as you can see here we have this function and it is called factorial and I'm sure you can kind of guess what it does the first thing that you will probably notice are all the parentheses now this is something that is very common in lisps and of course in closure so closure uses what is called a prefix notation normal programming languages use a different style of notation typically with other programming languages you have the function name first then you have the arguments inside of a set of parentheses and then you have the function body either in curly braces or through indentation with closure however we take these parentheses and we extend them all the way to the front of the function name and then to the end of the body and we get rid of the curly braces and the indentation instead so every single time you see the start of a parenthesis in closure you can rest assure that the first statement is actually a function that is actually true for this entire piece of code as well this first statement here is a macro that allows us to create a function and its arguments are the name of the function which is this symbol here a vector of the arguments of the function in this case we only have one argument which is N and then the actual function body itself and by doing this this actually creates a function and as you can see here this is the actual output of running this code here and it tells us that this code created a factorial function on the namespace Tut EPCOR which is the file that we're in now namespaces are like modules and other programming languages and of course there are other programming languages that also use namespaces like c-sharp to read closure code you always kind of want to look at the inner part of the code first and then work your way outwards so if we look at this inner part here we have this function deck and it is passing n through it which is our argument up here now if we just pull this out of the code here and we write deck 10 you can see that it actually decrements the number that we've passed through it that's exactly what this piece of code is doing it's taking the argument and it's decrementing it then we also have a recursive call here so this is actually calling the function inside of the function now ignoring this recursive call here let's go to the next part so the next part we have this star and then we have our argument and of course we have our decremented argument here and the function call so let's just pull out the star part and find out what's actually happening here so the star actually means multiplication and as you can see here we can pass in two arguments 10 and 9 and they multiply together and give us 90 so to find out what the factorial of Dec and actually means we kind of have to look at the rest of our function here more specifically this if statement so we have this if statement and then we have this equals n 1 so what does this equals n 1 actually mean if we type in equals 1/2 we get back false if we type in equals 1 1 we get back true so it's pretty apparent that this is just a boolean statement and it's basically saying if N equals 1 then we do this bit of code which is return n otherwise we run this rest of the function here all right so now we're kind of getting somewhere so what this basically is showing us is that when we put 4 or n it came through here first and it came back as false therefore this bit of code was removed then four was decremented here so it became three and then three was passed back through the function again so passed back through this if statement which it returned false again and it was multiplied by N and of course by being passed back through this if statement as well it also created a decrement of three which became two which was then passed back through the function again so our function actually created this pattern inside of it we pass in four it decrements it to three it multiplies four times three but it then again it passes three back through the function which had done decrements to two and then it multiplies four times three times two passes in two again which had n decrements to one and then one fulfills this if statement and then it passes back n which is 1 and then it multiplies all of these together and we get 24 and as you can see if we call our factorial function again on four we do in fact get 24 all right so now let's look at something that's slightly more complicated so this is a Fibonacci sequence function and we're going to now just go over it real quickly so first we have our defend macro which is defining this function and we're defining it as Dib and then we have one argument that's being passed into here which is n then the next part of the function is the function body however this function actually is acting like a wrapper of sorts because inside of it we have an anonymous function so this FN is actually a macro to create an anonymous function and then this next vector is actually our four arguments so we're creating anonymous function with four arguments and then we are passing it down to this conditional statement and then we are using this record to recursively loop through the actual anonymous function all right so let's start with the inner part of our function and that is this plus a and B we're actually passing these numbers here as arguments to our anonymous function here so one is equal to a 1 is equal to B 2 is equal to C and then this vector here is equal to this sequence variable so let's run fib of 7 and as you can see here it returns a sector of the first seven numbers of the Fibonacci sequence 1 1 2 3 5 8 and 13 so let's figure out how it actually got there so first let's look at our conditional and talk about how this conditional actually works so what a conditional is is it's like a bunch of nested if statements so our first if statement is asking whether or not n which is the argument that the user is passing into the actual function is equal to 1 and if it is equal to 1 so if this comes back is true then we just produce a vector of 1 so if we were to call fib 1 it would just stop here and produce us a vector of 1 and that would be our answer otherwise we come down here to the next part we try to see if n is equal to C now in this case C is 2 so if we were to put 2 in here this would actually just return 1 1 you can see this by actually calling fib 2 it's returning the sequence or the initial binding of seek back to the user then we have this little else keyword after this last conditional and the way this else keyword works is it tells this conditional here this function that any other case we want to do this part of the function so since we know about this part of the function and since we know that 7 does not fulfill this part of the conditional or this part of the conditional let's actually walk through this part of the function as we go up to 7 so in our initial loop through a equals 1 B equals 1 C equals 2 and the sequence is 1 1 now if we look in here we have a plus B which would equal 2 and then we have this cons part so we're calling cons on our vector so 1 1 and then on the result of this function which is 2 now if we actually call con John 1 1 & 2 we can see here that this is actually putting 2 into the end of our vector so we're getting 1 1 2 as a vector we are also running this i NC C piece of code so what does this do so we know C is 2 so if we run I NC 2 we get back three so this would return three so if we write this down there we have three and we have this new sequence then for the next part we're adding a and B together again so 1 + 1 which is 2 and then we are just returning B which in this case is just 1 so for our next loop through this recursion a would equal 1 B would equal 2 C would equal 3 and this would be the sequence so let's then go through the next loop now if we look we are adding a and B together which is 1 & 2 so we get 3 and then we're adding that to our sequence which gives us back 1 1 2 3 then we're incrementing C which takes 3 and gives us back 4 then we're adding a and B together which gives us 3 again and then we're returning B which is 2 so now a equals 2 B equals 3 C equals 4 and then this is our sequence because C is now equal to 4 this condition still hasn't been satisfied so because we've passed 7 in 4 is not equal to 7 so this recursion is going to continue for our next loop through as you can see here a and B together gives us 5 we add 5 into our sequence then we increment C again so that gives us 5 again and a and B together which is another 5 and then B which gives us 3 so now a is 3 B is 5 C is 5 and this is our sequence then of course for our next loop through we now have C equals 6 so this condition still hasn't been met a plus B which is 3 plus 5 is going to give us 8 this is going to be inserted into our vector then C is going to be incremented again so this is going to become 6 a plus B again which is 8 and then we're going to get B back which is 5 so now for our final loop because we know that C is going to be incremented once more going to become 7 which means it's going to be equal when it comes back through this conditional here is our final line of code and as you can see a plus B 5 plus 8 which is 13 then we pop that into our vector then we increment C so that gives us 7 which is going to satisfy this condition here then we add a plus B here which gives us 13 and then we return B which is 8 so now a is 8 B is 13 C is 7 and then the sequence is this vector this part of our conditional actually comes back as true therefore we just return the sequence which in this case is 1 1 2 3 5 8 and 13 and as you can see that was our answer for 7 so I hope you are able to understand how this function actually worked this conditional statement it's very handy and it's something that you use quite a lot inside of closure if you think about it a lot of programs use nested if-else statements and in this case we can just combined if else statements and make them less verbose and more compact also this recur macro is very nice it's optimized for tail end recursion which means that it works better on the CPU stack and allows us to deal with larger numbers and bigger pieces of data all right so here's the next example that we want to take a look at as you can see here we have this def which allows us to create a global variable so we're creating a global variable called Val with a question mark and we're assigning it to set a e i o u so what is set a io you actually produce or we can run it down here and see so set a e i o u actually produces a collection called a set the set starts with a number sign and a bunch of bracket and with this set we have each of our letters as a single element inside of this set we then have a function definition right here this function is called Pig Latin and we are passing a single argument to this function now if we come all the way down to the most inner part of our function we have this subs word 1 so word is what we're passing down to our function so this is the actual argument let's find out what this subs function actually does so if we pass a string of hello to subs and then 1 we get back ello so it removes the H from hello so we can assume that subs actually removes the first letter of our work so now we're kind of getting somewhere so then for our next part we have this STR function which is taking an hour and then it's taking this first letter variable or function or whatever it is as well as this a Y string so we don't know what first letter is quite yet but we can find out if we look at this let binding so what let allows us to do is bind any variable name so in this case first letter to a expression and have it be scoped to the block with which it was created in this case our first letter is only scoped or only defined or this entire block here and then outside of this fantasy it doesn't exist anymore so this is kind of nice and it's actually better than defining a variable global sense in closure because it doesn't introduce as much state into our program our first letter is the first function applied to our word which is our argument so if we pass hello to first we get backslash H back which is our first letter so this means that our first letter function here is actually just quite literally the first letter of the word that we're passing through this function so then we can find out what this STR function does so if we pass in hello and then the backslash H that we're getting from the first hello and then the a why that was hard-coded in here we get ello hey but what about the rest of this so we have this if statement what about this vowel first letter part well this valve first letter part the way it's working is if the vowel has the first letter inside of it so if we have a e io or U as the first letter then we're going to run this statement otherwise we're going to run this statement so in other words if our word has a vowel then we just concatenate a Y to the end of the word otherwise we removed the first letter of the word and then we put the first letter of the word at the end of the word and then we concatenate a Y to the end of that word so let's run our Pig Rutten if we run pig latin onto a word that has a consonant as its first letter you can see that it actually takes the consonant and puts it at the end of the word and then adds a Y to the end of the work if we run Pig Rutan on a word that starts with a vowel say a like address then does is concatenate a why to the end of our word here so this makes sense with what we've worked through with this function alright guys so that's the end of the tutorial I hope you guys enjoyed this style of tutorial if you have any questions or comments any constructive criticism or anything like that I would very much like to hear it whether or not you prefer this over the other style where we just kind of look at syntax if you enjoyed this video feel free to like and subscribe if you have any questions or comments and of course feel free to leave them in the box below and if you dislike this video then by all means slam that dislike button as much as you want I hope you guys have a good day
Original Description
A look at clojure syntax by breaking down 3 different functions statement by statement.
Clojure Cheat sheet: https://clojure.org/api/cheatsheet
Clojure Atom Plugins: https://gist.github.com/jasongilman/d1f70507bed021b48625
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 39 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
▶
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
What Is MLIR and Why Does It Exist?
Dev.to · Fedor Nikolaev
Why Choosing the Right Machine Learning Development Company Matters More Than the AI Model
Medium · Machine Learning
Data privacy in AI training: federated learning, differential privacy, and synthetic data
Dev.to AI
Data Preprocessing: Encoding and Feature Scaling in Machine Learning
Medium · Machine Learning
🎓
Tutor Explanation
DeepCamp AI