Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Teaching Clojure

Teaching Clojure

Talk presented at TheConf, a Brazilian, English-speaking conference organized by CodeMiner: www.theconf.club

Clojure is a highly trending language with many applications. Many programmers have heard of or studied LISP. Are they willing to learn Clojure and Functional Programming?

Can we teach such hipster stuff on 100+-year-old public institutions? Can we motivate students to return to their college after graduating?

This talk will describe the experience of teaching Clojure and Functional Programming on an extension course in IFSP Campinas, a public institution with more than 20 campuses in the São Paulo state.

André Willik Valenti

August 16, 2019
Tweet

More Decks by André Willik Valenti

Other Decks in Programming

Transcript

  1. Teaching Clojure André Willik Valenti TheConf | São Paulo-SP |

    Brazil August 16th 2019 1
  2. Disclamer This presentation reflects the author's opinions. It does not

    necessarily reflect employers' opinions. 2
  3. Introduction 3

  4. 4

  5. 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
  6. André Willik Valenti • Familiar languages: ◦ Java ◦ JavaScript/NodeJS/CoffeeScript

    ◦ HTML, CSS ◦ Ruby, Python ◦ C#, Lua, PHP, C++, Haskell, ML ◦ Clojure 6
  7. IFSP 7

  8. IFSP • Federal Institute for Education, Science and Technology of

    São Paulo State • Public teaching institution • Secondary and tertiary education 8
  9. https://www.ifsp.edu.br/sobre-o-campus 9

  10. 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
  11. • 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
  12. • 2013: TDD - Test-Driven Development • 2017: Basic Chess

    • 2019: Introduction to FP with Clojure Extension courses I've applied 12
  13. What's an extension course? What's extension? 13

  14. 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
  15. 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
  16. Why a Functional Programming/Clojure course? 16

  17. 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
  18. Clojure • Lisp dialect • Dynamically typed • Created by

    Rich Hickey • Released on 2007 18
  19. 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
  20. What does Clojure code look like? 20

  21. (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
  22. Course history 22

  23. Oct. 2018 Hi, I'm Clojure! Great ideas and tools here.

    23
  24. Oct. 2018 Cool! I should learn that stuff… 24

  25. (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
  26. Oct. 2018: Call for proposals 26

  27. Nov. 2018: Proposal elaboration 27

  28. Nov. 2018: Proposal elaboration 28 • Campinas campus ◦ Acceptance

    of proposal on late Nov. • Vice-Rectory of Extension ◦ Acceptance expected for late Jan.
  29. Dec. 2018 - Jan. 2019: Promotion 29 • IFSP website

    • E-mail • Physical A4 posters
  30. 30

  31. 31

  32. Feb. 2019 32 • Acceptance of proposal by Vice-Rectory of

    Extension • Course started of Feb. 12th
  33. In numbers • 51 registered (inscritos) • 22 enrolled (matriculados)

    • 16 present after first month • 9 finished 33
  34. In numbers Of those 16 present after first month... 34

  35. In numbers 35 Current IFSP students 3 Former IFSP students

    5 Colleagues of former 6 UNICAMP students 4
  36. How many people... ...came because of the poster or the

    QR Code? 36
  37. 0

  38. 38

  39. Marketing matters 39 Lesson learned #1 You gotta sow a

    lotta seeds, on proper soil. Remember the Conversion Funnel!
  40. Learning Clojure 40

  41. 2011 41 http://learnyouahaskell.com/

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

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

  44. 2019 44 REPL (lein repl)

  45. 2019 45 https://learnxinyminutes.com

  46. • 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
  47. • Local environment: ◦ Leiningen (https://leiningen.org/) ◦ VSCode (https://code.visualstudio.com) with

    extensions How I recommend starting to learn Clojure 47
  48. Commands to install VSCode extensions code --install-extension avli.clojure code --install-extension

    tonsky.clojure-warrior code --install-extension leocardoso94.clojure-snippets 48
  49. Course overview 49

  50. • 30 hours • 18 weeks • Tuesdays, 7:00 -

    8:40 PM (classroom) Course load 50
  51. • Functions, immutability • Lists, recursion • Clojure syntax •

    Tools, libraries • Mutability • Project structuring • Records, macros and parallelism • Project presentation Curriculum 51
  52. 1. Functional programming concepts (with JavaScript) 2. Clojure introduction 3.

    Project development Structure 52
  53. 1. Functional programming concepts (with JavaScript) ◦ Functions, values ◦

    Immutability, side-effects, purity ◦ Recursion, tail-recursion Structure 53
  54. 2. Clojure introduction ◦ Prefix notation, functions, variables ◦ Conditionals,

    recursion ◦ vector, list, set, map Structure 54
  55. 3. Project development ◦ Leiningen ◦ Ring (web framework) ◦

    Atoms, mutability Structure 55
  56. Course application 56

  57. Sample exercises 57

  58. 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
  59. Standard functions Write your own version of a map function.

    59
  60. Functions as arguments and result Write a function that generates

    a multiplier function. function generate(…) { return … } const triple = generate(3); triple(10); // 30 60
  61. Recursion What does this function do? function a(i, j) {

    if (i < j) { return i + a(i + 1, j); } else { return i; } } 61
  62. Recursion What does this function do? const c = (i,

    j) => i < j ? i + c(i + 1, j) : i; 62
  63. 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
  64. Conditionals Make a tail-recursive factorial function in Clojure. 64

  65. Libraries; web development Let's do a dojo using Ring and

    Compojure!! 65
  66. Project 66

  67. Project • Groups of 2 students • Choose one theme

    among many • Develop a simple application • Present it in 10 ~ 15 minutes 67
  68. • 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
  69. Surveys 69

  70. Week 3/18: Level of difficulty 70 16 answers

  71. Week 6/18: Level of difficulty 71 14 answers

  72. Week 9/18: Level of difficulty 72 15 answers 100%

  73. Week 3/18: Examples/exercises being in JavaScript 73 16 answers 100%

  74. Week 6/18: How do you prefer studying Clojure? 74 57%

    43% 14 answers
  75. Week 13/18: About the coding dojo last class 75 80%

    10% 10% 10 answers
  76. Week 16/18: "The course was ____ my expectation" 76 8

    answers
  77. Week 18/18: informal survey 77

  78. Stundents found it nice • REPL • Standard data structures

    • Tools, esp. Leiningen • Ring middlewares • Using vectors to store pairs 78
  79. Stundents found it difficult • Find the parenthesis to close

    • Error messages • Tail-recursion • Chaining functions 79
  80. Simple? Easy? https://www.infoq.com/presentations/Simple-Made-Easy/ 80

  81. When developing a real system... • Prefer simple design and

    implementation ◦ …not necessarily easy ones 81
  82. But, when teaching an introductory course... 82

  83. Prefer the easiest stuff (not necessarily the simplest) function(x) {

    return x * 2; } instead of x => x * 2 83 Lesson learned #2
  84. Clojure impressions 84

  85. Parentheses 85 • Not that hard • You get used

    to them • Do use a rainbow parentheses plugin • Better yet, use quickie
  86. Pros • Documentation • Built-in data structures • Automated testing

    • Fun to learn and use • "Finally free!" feeling 86
  87. Cons • Complex • Many ways to do everything •

    Awful error messages 87
  88. 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
  89. 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
  90. Con: many ways to do same thing 90 Suppose you

    want to transform {:a 1 :b {:c 2}} into {:a 1 :b {:c 3}}
  91. Con: many ways to do same thing 91 Suppose you

    have it in a variable m (def m {:a 1 :b {:c 2}})
  92. 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)
  93. Con: error messages 93

  94. 94

  95. Punishment! 95

  96. Pranks (pegadinhas) 96

  97. 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
  98. def: innocuous? (def a 10) (defn f [] (+ 1

    a)) (f) ; 11 (def a 20) (f) ; ??? 98
  99. def: innocuous? (def a 10) (defn f [] (+ 1

    a)) (f) ; 11 (def a 20) (f) ; 21!!!!! 99
  100. def: innocuous? • def creates a Var • Var's are

    mutable!! 100
  101. 101

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

    21 102
  103. def: innocuous? • Use def carefully • Prefer let whenever

    possible • For private functions, use defn- ◦ visible only inside namespace 103
  104. "No syntax" ' " @ # % & ( )

    \ [ ] { } ^ ` ; : / . #{} -> ->> ~@ 104
  105. References 105

  106. 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
  107. • 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
  108. http://tiny.cc/3dnl3y Course material (in Portuguese) 108

  109. Conclusions 109

  110. What's functional programming all about? 110

  111. FP is basically about: 1. calculating next state 2. switching

    to it Mutation only happens on phase 2. 111 Lesson learned #3
  112. Making hard easy • Yes! You can bring novel knowledge

    to your workplace. • Yes! You can make hard stuff easy for other people. 112
  113. Clojure is not... • very welcoming (nasty error messages!) •

    appropriate for new programmers (other Lisps might be) • that hard, nor that easy • simple! 113
  114. Clojure is... • a great source of ideas and tools

    for software development • a production-ready ecosystem with a big community • pragmatic! 114
  115. Things evolve according to people's needs Lesson learned #4 Not

    always to the original principles. 115
  116. I'm leaving IFSP... • ...and joining Lambda3 next week 116

  117. Thank you! Questions? 117 Contact: • awvalenti@gmail.com • https://twitter.com/awvalenti •

    https://about.me/awvalenti