Slide 1

Slide 1 text

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

Slide 6

Slide 6 text

André Willik Valenti ● Familiar languages: ○ Java ○ JavaScript/NodeJS/CoffeeScript ○ HTML, CSS ○ Ruby, Python ○ C#, Lua, PHP, C++, Haskell, ML ○ Clojure 6

Slide 7

Slide 7 text

IFSP 7

Slide 8

Slide 8 text

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

Slide 20

Slide 20 text

What does Clojure code look like? 20

Slide 21

Slide 21 text

(defn to-text [game-data] (->> game-data to-char-matrix (reduce (fn [ret row-data] (str ret (reduce #(str %1 %2 \game-data) "" row-data) \newline)) ""))) (defn to-json [game-data] {:id (game-data :id) :board (for [row-data (to-char-matrix game-data)] (reduce str row-data))}) 21

Slide 22

Slide 22 text

Course history 22

Slide 23

Slide 23 text

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.

Slide 29

Slide 29 text

Dec. 2018 - Jan. 2019: Promotion 29 ● IFSP website ● E-mail ● Physical A4 posters

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

Feb. 2019 32 ● Acceptance of proposal by Vice-Rectory of Extension ● Course started of Feb. 12th

Slide 33

Slide 33 text

In numbers ● 51 registered (inscritos) ● 22 enrolled (matriculados) ● 16 present after first month ● 9 finished 33

Slide 34

Slide 34 text

In numbers Of those 16 present after first month... 34

Slide 35

Slide 35 text

In numbers 35 Current IFSP students 3 Former IFSP students 5 Colleagues of former 6 UNICAMP students 4

Slide 36

Slide 36 text

How many people... ...came because of the poster or the QR Code? 36

Slide 37

Slide 37 text

0

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

Marketing matters 39 Lesson learned #1 You gotta sow a lotta seeds, on proper soil. Remember the Conversion Funnel!

Slide 40

Slide 40 text

Learning Clojure 40

Slide 41

Slide 41 text

2011 41 http://learnyouahaskell.com/

Slide 42

Slide 42 text

2014 42 https://www.coursera.org/learn/programming-languages

Slide 43

Slide 43 text

2019 43 https://www.clojure.org/guides/learn/syntax

Slide 44

Slide 44 text

2019 44 REPL (lein repl)

Slide 45

Slide 45 text

2019 45 https://learnxinyminutes.com

Slide 46

Slide 46 text

● 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

Slide 48

Slide 48 text

Commands to install VSCode extensions code --install-extension avli.clojure code --install-extension tonsky.clojure-warrior code --install-extension leocardoso94.clojure-snippets 48

Slide 49

Slide 49 text

Course overview 49

Slide 50

Slide 50 text

● 30 hours ● 18 weeks ● Tuesdays, 7:00 - 8:40 PM (classroom) Course load 50

Slide 51

Slide 51 text

● Functions, immutability ● Lists, recursion ● Clojure syntax ● Tools, libraries ● Mutability ● Project structuring ● Records, macros and parallelism ● Project presentation Curriculum 51

Slide 52

Slide 52 text

1. Functional programming concepts (with JavaScript) 2. Clojure introduction 3. Project development Structure 52

Slide 53

Slide 53 text

1. Functional programming concepts (with JavaScript) ○ Functions, values ○ Immutability, side-effects, purity ○ Recursion, tail-recursion Structure 53

Slide 54

Slide 54 text

2. Clojure introduction ○ Prefix notation, functions, variables ○ Conditionals, recursion ○ vector, list, set, map Structure 54

Slide 55

Slide 55 text

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

Slide 63

Slide 63 text

Prefix notation Write these expressions in Clojure: 1. (tf - 32) / 1.8 2. 1.8tc + 32 3. (-b + sqrt(b² - 4ac)) / 2a 4. (-b - sqrt(b² - 4ac)) / 2a 63

Slide 64

Slide 64 text

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

Slide 80

Slide 80 text

Simple? Easy? https://www.infoq.com/presentations/Simple-Made-Easy/ 80

Slide 81

Slide 81 text

When developing a real system... ● Prefer simple design and implementation ○ …not necessarily easy ones 81

Slide 82

Slide 82 text

But, when teaching an introductory course... 82

Slide 83

Slide 83 text

Prefer the easiest stuff (not necessarily the simplest) function(x) { return x * 2; } instead of x => x * 2 83 Lesson learned #2

Slide 84

Slide 84 text

Clojure impressions 84

Slide 85

Slide 85 text

Parentheses 85 ● Not that hard ● You get used to them ● Do use a rainbow parentheses plugin ● Better yet, use quickie

Slide 86

Slide 86 text

Pros ● Documentation ● Built-in data structures ● Automated testing ● Fun to learn and use ● "Finally free!" feeling 86

Slide 87

Slide 87 text

Cons ● Complex ● Many ways to do everything ● Awful error messages 87

Slide 88

Slide 88 text

Pro: automated testing (ns sample-project.core-test (:require [clojure.test :refer :all] [sample-project.core :refer :all])) (deftest a-test (testing "FIXME, I fail." (is (= 0 1)))) 88

Slide 89

Slide 89 text

Pro: automated testing (ns sample-project.core-test (:require [clojure.test :refer :all] [sample-project.core :refer :all])) (deftest a-test (testing "Are a's equal to b's?" (are [a b] (= a b) 1 1 2 2 3 3))) 89

Slide 90

Slide 90 text

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

Slide 101

Slide 101 text

101

Slide 102

Slide 102 text

def: innocuous? (with-redefs [a 30] (f)) ; 31 (f) ; 21 102

Slide 103

Slide 103 text

def: innocuous? ● Use def carefully ● Prefer let whenever possible ● For private functions, use defn- ○ visible only inside namespace 103

Slide 104

Slide 104 text

"No syntax" ' " @ # % & ( ) \ [ ] { } ^ ` ; : / . #{} -> ->> ~@ 104

Slide 105

Slide 105 text

References 105

Slide 106

Slide 106 text

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

Slide 117

Slide 117 text

Thank you! Questions? 117 Contact: ● [email protected] ● https://twitter.com/awvalenti ● https://about.me/awvalenti