Teaching Clojure
André Willik Valenti
TheConf | São Paulo-SP | Brazil
August 16th 2019
1
Slide 2
Slide 2 text
Disclamer
This presentation reflects the author's opinions.
It does not necessarily reflect employers' opinions.
2
Slide 3
Slide 3 text
Introduction
3
Slide 4
Slide 4 text
4
Slide 5
Slide 5 text
André Willik Valenti
● Academic-industrial background
○ BSc. and MSc. in Computer Science
○ Developer for ~5 years
○ Professor for ~5 years
● Experiences in private and public sectors
5
IFSP
● Federal Institute for Education, Science and Technology of
São Paulo State
● Public teaching institution
● Secondary and tertiary education
8
Slide 9
Slide 9 text
https://www.ifsp.edu.br/sobre-o-campus
9
Slide 10
Slide 10 text
IFSP
● 110-year-old institution
○ 1909: Escola de Aprendizes Artífices
○ 1970: Escola Técnica Federal
○ 1990: CEFET - Centro Federal de Educação Tecnológica
○ 2008: IFSP
● Professor there since 2013
○ 2013 - 2014: São Carlos
○ 2014 - 2019: Campinas
10
Slide 11
Slide 11 text
● Programming Languages, OOP
○ C#, Java
● Game Development
○ JavaScript, Lua
● Web Development
○ HTML, CSS, JavaScript
○ Java, PHP, NodeJS
● Other
○ Python, Agile Methodologies, OS
Regular subjects I've taught
11
Slide 12
Slide 12 text
● 2013: TDD - Test-Driven Development
● 2017: Basic Chess
● 2019: Introduction to FP with Clojure
Extension courses I've applied
12
Slide 13
Slide 13 text
What's an extension course? What's extension?
13
Slide 14
Slide 14 text
Extension
● Teaching, research and extension
○ Pillars of universities and similar institutions in Brazil
● Actions that interest and involve the external community
○ Courses
○ Lectures
○ Events
○ Shows
○ …
14
Slide 15
Slide 15 text
Some extension courses at IFSP Campinas
● Data Science and Artificial Intelligence
● Android Development
● Geoprocessing
● Oracle Databases
● Computing for senior citizens
● …
(https://www2.cmp.ifsp.edu.br/extensao/portfolio.php)
15
Slide 16
Slide 16 text
Why a Functional Programming/Clojure course?
16
Slide 17
Slide 17 text
Motivation
● Audience in Campinas
○ Technology research and software development center
● Functional programming is everywhere
○ …except in colleges curricula
● Clojure is becoming widespread
○ Has job offerings
○ Fits well an ongoing Java project
17
Slide 18
Slide 18 text
Clojure
● Lisp dialect
● Dynamically typed
● Created by Rich Hickey
● Released on 2007
18
Slide 19
Slide 19 text
Clojure
● More pragmatic than philosophical
○ "Getting stuff done" over "following principles"
● Favors immutability
○ ...but does not enforce it
● Favors idiomatic constructs
○ ...but allows everything else (e.g., Java interop)
19
Oct. 2018 Hi, I'm Clojure!
Great ideas
and tools here.
23
Slide 24
Slide 24 text
Oct. 2018
Cool!
I should learn
that stuff…
24
Slide 25
Slide 25 text
(make-extension-course! :clojure 2019)
1. Wait for call for proposals
2. Submit proposal
3. Make requested changes
4. Hope for approval
5. Apply course
6. Bob's your uncle!
25
Slide 26
Slide 26 text
Oct. 2018: Call for proposals
26
Slide 27
Slide 27 text
Nov. 2018: Proposal elaboration
27
Slide 28
Slide 28 text
Nov. 2018: Proposal elaboration
28
● Campinas campus
○ Acceptance of proposal on late Nov.
● Vice-Rectory of Extension
○ Acceptance expected for late Jan.
● Quick, concrete overview:
○ https://learnxinyminutes.com
● Explanations and exercises:
○ https://clojure.org/guides/learn/syntax
● Quick playground:
○ https://repl.it/languages/clojure
How I recommend starting to learn Clojure
46
Slide 47
Slide 47 text
● Local environment:
○ Leiningen (https://leiningen.org/)
○ VSCode (https://code.visualstudio.com) with extensions
How I recommend starting to learn Clojure
47
3. Project development
○ Leiningen
○ Ring (web framework)
○ Atoms, mutability
Structure
55
Slide 56
Slide 56 text
Course application
56
Slide 57
Slide 57 text
Sample exercises
57
Slide 58
Slide 58 text
Immutability
Which of these are pure functions?
1. x => console.log(x)
2. (f, l) => ({ firstName: f, lastName: l })
3. array => array.length + 1
4. array => ++array.length
58
Slide 59
Slide 59 text
Standard functions
Write your own version of a map function.
59
Slide 60
Slide 60 text
Functions as arguments and result
Write a function that generates a multiplier function.
function generate(…) {
return …
}
const triple = generate(3);
triple(10); // 30
60
Slide 61
Slide 61 text
Recursion
What does this function do?
function a(i, j) {
if (i < j) {
return i + a(i + 1, j);
} else {
return i;
}
}
61
Slide 62
Slide 62 text
Recursion
What does this function do?
const c = (i, j) => i < j ? i + c(i + 1, j) : i;
62
Conditionals
Make a tail-recursive factorial function in Clojure.
64
Slide 65
Slide 65 text
Libraries; web development
Let's do a dojo using Ring and Compojure!!
65
Slide 66
Slide 66 text
Project
66
Slide 67
Slide 67 text
Project
● Groups of 2 students
● Choose one theme among many
● Develop a simple application
● Present it in 10 ~ 15 minutes
67
Slide 68
Slide 68 text
● Action game
● Compiler
● Data processing
● Video and/or audio processing
● Web scraping
● E-commerce front-end
● E-commerce back-end
● RESTful API
Available themes
68
Slide 69
Slide 69 text
Surveys
69
Slide 70
Slide 70 text
Week 3/18: Level of difficulty
70
16 answers
Slide 71
Slide 71 text
Week 6/18: Level of difficulty
71
14 answers
Slide 72
Slide 72 text
Week 9/18: Level of difficulty
72
15 answers
100%
Slide 73
Slide 73 text
Week 3/18: Examples/exercises being in JavaScript
73
16 answers
100%
Slide 74
Slide 74 text
Week 6/18: How do you prefer studying Clojure?
74
57%
43%
14 answers
Slide 75
Slide 75 text
Week 13/18: About the coding dojo last class
75
80%
10%
10%
10 answers
Slide 76
Slide 76 text
Week 16/18: "The course was ____ my expectation"
76
8 answers
Slide 77
Slide 77 text
Week 18/18: informal survey
77
Slide 78
Slide 78 text
Stundents found it nice
● REPL
● Standard data structures
● Tools, esp. Leiningen
● Ring middlewares
● Using vectors to store pairs
78
Slide 79
Slide 79 text
Stundents found it difficult
● Find the parenthesis to close
● Error messages
● Tail-recursion
● Chaining functions
79
Con: many ways to do same thing
90
Suppose you want to transform
{:a 1 :b {:c 2}}
into
{:a 1 :b {:c 3}}
Slide 91
Slide 91 text
Con: many ways to do same thing
91
Suppose you have it in a variable m
(def m {:a 1 :b {:c 2}})
Slide 92
Slide 92 text
Con: many ways to do same thing
92
Some ways to do it:
1. {:a (:a m) :b {:c (inc (:c (:b m)))}}
2. (assoc m :b {:c (inc (:c (:b m)))})
3. (assoc-in m [:b :c] (inc (get-in m [:b :c])))
4. (update-in m [:b :c] inc)
Slide 93
Slide 93 text
Con: error messages
93
Slide 94
Slide 94 text
94
Slide 95
Slide 95 text
Punishment!
95
Slide 96
Slide 96 text
Pranks (pegadinhas)
96
Slide 97
Slide 97 text
Pranks (pegadinhas)
97
● conj
○ Inserts at beginning for lists, at end for vectors
● contains?
○ Checks element for sets, checks index for vectors
● clojure.set/intersection
○ Only works for sets
Slide 98
Slide 98 text
def: innocuous?
(def a 10)
(defn f [] (+ 1 a))
(f) ; 11
(def a 20)
(f) ; ???
98
Slide 99
Slide 99 text
def: innocuous?
(def a 10)
(defn f [] (+ 1 a))
(f) ; 11
(def a 20)
(f) ; 21!!!!!
99
Slide 100
Slide 100 text
def: innocuous?
● def creates a Var
● Var's are mutable!!
100
Great Rich Hickey talks
● The Value of Values
○ https://www.youtube.com/watch?v=-6BsiVyC1kM
● Simple Made Easy
○ https://www.infoq.com/presentations/Simple-Made-Easy/
○ Newer version: Simplicity Matters
● Effective Programs - 10 Years of Clojure
○ https://www.youtube.com/watch?v=2V1FtfBDsLU
106
Slide 107
Slide 107 text
● slingshot
○ Enhanced exception handling (idiomatic)
● io.aviso/pretty
○ Pretty printing of unhandled exception (readable)
● quickie
○ Autorunning tests when file is saved (faster cycle)
Some useful Clojure tools
107
Slide 108
Slide 108 text
http://tiny.cc/3dnl3y
Course material (in Portuguese)
108
Slide 109
Slide 109 text
Conclusions
109
Slide 110
Slide 110 text
What's functional programming all about?
110
Slide 111
Slide 111 text
FP is
basically
about:
1. calculating next state
2. switching to it
Mutation only
happens on phase 2.
111
Lesson learned #3
Slide 112
Slide 112 text
Making hard easy
● Yes! You can bring novel knowledge to your workplace.
● Yes! You can make hard stuff easy for other people.
112
Slide 113
Slide 113 text
Clojure is not...
● very welcoming (nasty error messages!)
● appropriate for new programmers (other Lisps might be)
● that hard, nor that easy
● simple!
113
Slide 114
Slide 114 text
Clojure is...
● a great source of ideas and tools for software
development
● a production-ready ecosystem with a big community
● pragmatic!
114
Slide 115
Slide 115 text
Things evolve
according to
people's needs
Lesson learned #4
Not always to the
original principles.
115
Slide 116
Slide 116 text
I'm leaving IFSP...
● ...and joining Lambda3 next week
116