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. 4

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

    ◦ HTML, CSS ◦ Ruby, Python ◦ C#, Lua, PHP, C++, Haskell, ML ◦ Clojure 6
  4. IFSP • Federal Institute for Education, Science and Technology of

    São Paulo State • Public teaching institution • Secondary and tertiary education 8
  5. 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
  6. • 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
  7. • 2013: TDD - Test-Driven Development • 2017: Basic Chess

    • 2019: Introduction to FP with Clojure Extension courses I've applied 12
  8. 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
  9. 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
  10. 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
  11. 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
  12. (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
  13. (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
  14. Nov. 2018: Proposal elaboration 28 • Campinas campus ◦ Acceptance

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

    • E-mail • Physical A4 posters
  16. 30

  17. 31

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

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

    • 16 present after first month • 9 finished 33
  20. In numbers 35 Current IFSP students 3 Former IFSP students

    5 Colleagues of former 6 UNICAMP students 4
  21. 0

  22. 38

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

    lotta seeds, on proper soil. Remember the Conversion Funnel!
  24. • 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
  25. Commands to install VSCode extensions code --install-extension avli.clojure code --install-extension

    tonsky.clojure-warrior code --install-extension leocardoso94.clojure-snippets 48
  26. • 30 hours • 18 weeks • Tuesdays, 7:00 -

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

    Tools, libraries • Mutability • Project structuring • Records, macros and parallelism • Project presentation Curriculum 51
  28. 1. Functional programming concepts (with JavaScript) ◦ Functions, values ◦

    Immutability, side-effects, purity ◦ Recursion, tail-recursion Structure 53
  29. 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
  30. Functions as arguments and result Write a function that generates

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

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

    j) => i < j ? i + c(i + 1, j) : i; 62
  33. 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
  34. Project • Groups of 2 students • Choose one theme

    among many • Develop a simple application • Present it in 10 ~ 15 minutes 67
  35. • 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
  36. Stundents found it nice • REPL • Standard data structures

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

    • Error messages • Tail-recursion • Chaining functions 79
  38. When developing a real system... • Prefer simple design and

    implementation ◦ …not necessarily easy ones 81
  39. Prefer the easiest stuff (not necessarily the simplest) function(x) {

    return x * 2; } instead of x => x * 2 83 Lesson learned #2
  40. Parentheses 85 • Not that hard • You get used

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

    • Fun to learn and use • "Finally free!" feeling 86
  42. 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
  43. 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
  44. Con: many ways to do same thing 90 Suppose you

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

    have it in a variable m (def m {:a 1 :b {:c 2}})
  46. 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)
  47. 94

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

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

    a)) (f) ; 11 (def a 20) (f) ; 21!!!!! 99
  51. 101

  52. def: innocuous? • Use def carefully • Prefer let whenever

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

    \ [ ] { } ^ ` ; : / . #{} -> ->> ~@ 104
  54. 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
  55. • 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
  56. FP is basically about: 1. calculating next state 2. switching

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

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

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

    for software development • a production-ready ecosystem with a big community • pragmatic! 114