{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Classo numero quatro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Some small remarks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note on immutability" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6 7 8]" ] } ], "source": [ "(def lst [1 2 3 4 5 6 7 8])\n", "\n", "(defn times-2 [n]\n", " (* 2 n))\n", "\n", "(def lst-2 (map times-2 lst))\n", "(print lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note on lazyness" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0 2 4 6 8 10 12 14 16 18)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(def all-numbers (range))\n", "(def all-even-numbers (map times-2 all-numbers))\n", "(take 10 all-even-numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note on comments" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "(def persen {:name \"steph\"}) ;:age 34\n", "(def foo 2) ;; this is a comment. We call it a \"line comment\"\n", "\n", "(comment (println \"This is what we call a block comment, rarely used\"))\n", "\n", "\n", "#_(println \"This is another type of block comment, more often used. (used by david nolen)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Hyperfiddle\n", "\n", "This is the coolest thing ever! http://www.hyperfiddle.net/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `if` and `when`\n", "\n", "Sometime, we only want to do \"stuff\" when something happen. This is moment to use `if` and `when`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "99 is odd\n" ] } ], "source": [ "(defn print-odd-or-even [n]\n", " (if (odd? n)\n", " (println n \" is odd\")\n", " (println n \" is even\")))\n", "\n", "(defn custom-hello [gender]\n", " (if (= gender :male)\n", " \"his\"\n", " (if (= gender :female)\n", " \"her\"\n", " \"hex\")))\n", "\n", "(defn custom-hello-2 [gender]\n", " (when (= gender :male)\n", " (println \"his\")))\n", " \n", "\n", "\n", "(print-odd-or-even 99)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Doing a web page!\n", "\n", "* Intro html tags...\n", "* Let's do some html trash course\n", "* https://www.w3schools.com/TAGS/tryit.asp?filename=tryhtml_headers\n", "* https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ ";; the bellow line \"imports\" the functions required to display html\n", ";; in the jupyter notebook\n", "(require '[clojupyter.misc.display :as display]) ;; https://github.com/clojupyter/clojupyter" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| Ville | Resultat | Ville | Resultat |
| drummonville | 4 | montreal | 5 |
| quebec | 12 | montreal | 2 |
| gatineau | 7 | montreal | 5 |
| amos | 1 | montreal | 12 |