Press "Enter" to skip to content

Clojure – FunFunFunction #5


welcome to fun fun function today we are

talking about cloture

more specifically the syntax of cloture

I am doing this episode a wee bit

hungover because yesterday was Halloween

I was at a party with a scary tales

theme

this was my costume it’s the three bears

from Goldilocks so why is it a good idea

to explore other languages we mostly use

JavaScript on this show but we don’t

want to learn JavaScript

we want to learn programming and to do

that we need to explore and expand our

horizons there is a lot of cool things

about closure but I want to focus on one

little detail today and that is the fact

that closure has almost no syntax at all

let’s compare closure to JavaScript this

up here this is valid JavaScript it will

evaluate to 6 now here is the same thing

in closure when I saw this for the first

time it made me go apeshit

why it’s the fluff on the left this

makes no sense this is because what we

are looking at is almost the entire

closure syntax almost all closure looks

like this it’s an open parenthesis

parenthesis I am NOT a native English

speaker and that is a hard word it’s an

open parenthesis then an operator and

then as many operands as you like and

then a closing parenthesis what what

does it mean I’ll show you some more

examples and you’ll get here is a

variable declaration in JavaScript and

the same one in closure we initialize

the variable my ferret with the value

waffles in the closure example we have

our operator death most definitely short

for a define and then two operands for

the variable my ferret and the variable

value waffles I’ll show you the third

example if you look at the JavaScript

here we evaluate a boolean expression

and then if it is true which it always

is in this case we return the first

statement at the question mark you can

have a delicious soup and then if it’s

false which is average we return the no

soup for you straight in the claw drip

code it’s the same bloody thing as the

other examples it’s an open parenthesis

the operator is and then followed by

three operands the first operand is the

boolean being evaluated always true and

the second operand is what we get if the

boolean is true which it always is and

the third operand is what we get if the

boolean is false which it never is in

this example so in closure it’s always

operator operand operand operand and

close parentheses and the closure that

is called a for but where are my curly

brackets what if I want to do multiple

things in my if statement like this if

true is true we should console.log a

message and then console of another

message well you do it the exact same

way so looking at this code you are

probably now recognizing closure as that

language with all the

parentheses but joking aside I mean look

at how elegant is this there is no new

syntax introduced here the only new

thing that we started to do is to nest

forms let’s go through it together

our outer form starts with the operator

if after that comes our first operand

the boolean being evaluated which is

always true and then comes another

nested form with the operator – and the

operator do will simply well do all of

the forms that is passed to it as

operands and that is these two print

line forms and that’s it now you know

90% of the closure syntax and it’s

pretty cool because most other languages

have

tons and tons of tons of different

language constructs while in closure

almost all functionality is enclosed in

the operators and the simple form syntax

you have watched an episode of fun fun

function a weekly show where we tried to

come more excited and confident about

programming by exploring old wisdom wild

ideas and having fun this is a weekly

show released on Mondays but not next

week because I am having a week off so

make sure that you follow me on twitter

at MP Jamie so that you don’t miss out

on that so until next next Monday stay curious

Please follow and like us: