diff --git a/Cours 4.ipynb b/Cours 4.ipynb index 5dc6cb0..8ee2e4a 100644 --- a/Cours 4.ipynb +++ b/Cours 4.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Class numero quatro" + "## Classo numero quatro" ] }, { @@ -23,18 +23,15 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "#'user/lst-2" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5 6 7 8]" + ] } ], "source": [ @@ -43,7 +40,8 @@ "(defn times-2 [n]\n", " (* 2 n))\n", "\n", - "(def lst-2 (map times-2 lst))" + "(def lst-2 (map times-2 lst))\n", + "(print lst)" ] }, { @@ -55,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -64,7 +62,7 @@ "(0 2 4 6 8 10 12 14 16 18)" ] }, - "execution_count": 6, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -84,10 +82,11 @@ }, { "cell_type": "code", - "execution_count": 36, + "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", @@ -116,25 +115,37 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 8, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "#'user/print-odd-or-even" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" + "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\")))" + " (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)" ] }, { @@ -151,51 +162,64 @@ }, { "cell_type": "code", - "execution_count": 32, + "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": 38, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "

We can very easely print stuff from code

" + "

We can very easely print stuff from code

" ] }, - "execution_count": 38, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "(display/html \"

We can very easely print stuff from code

\") " + "(display/html \"

We can very easely print stuff from code

\") " ] }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "#'user/html-h1" + "text/html": [ + "borris\" title \"hiccup is much better" ] }, - "execution_count": 42, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ + ";; Hiccup is THE way to create html content from clojure\n", "(display/hiccup-html [:h3 \"hiccup is much better\"])" ] }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "#'user/hiccup-h1" + "text/html": [ + "

ok

" ] }, - "execution_count": 45, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "(defn hiccup-h1 [title])" + "(defn hiccup-h1 [title]\n", + " [:h1 title])\n", + "(display/hiccup-html (hiccup-h1 \"ok\"))\n" ] }, { @@ -254,19 +281,87 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/dodgeball-data" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "(def dodgeball-data\n", - " [{:team-1 :drummonville :team-2 :montreal :points-1 4 :points-1 5}\n", - " {:team-1 :drummonville :team-2 :montreal :points-1 4 :points-1 5}\n", - " {:team-1 :drummonville :team-2 :montreal :points-1 4 :points-1 5}\n", - " {:team-1 :drummonville :team-2 :montreal :points-1 4 :points-1 5}])\n", + " [{:team-1 :drummonville :team-2 :montreal :points-1 4 :points-2 5}\n", + " {:team-1 :quebec :team-2 :montreal :points-1 12 :points-2 2}\n", + " {:team-1 :gatineau :team-2 :montreal :points-1 7 :points-2 5}\n", + " {:team-1 :amos :team-2 :montreal :points-1 1 :points-2 12}])\n", " \n", " \n", " " ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
VilleResultatVilleResultat
drummonville4montreal5
quebec12montreal2
gatineau7montreal5
amos1montreal12
" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(def bold {:style \"font-weight: bold;\"})\n", + "(def not-bold nil)\n", + "\n", + "(defn team-1-style [game]\n", + " (if (> (game :points-1) (game :points-2))\n", + " bold\n", + " not-bold))\n", + "\n", + "(defn team-2-style [game]\n", + " (if (< (game :points-1) (game :points-2))\n", + " bold\n", + " not-bold))\n", + "\n", + "\n", + "\n", + "(defn create-row [game]\n", + " [:tr [:td (team-1-style game ) (:team-1 game)]\n", + " [:td (:points-1 game)]\n", + " [:td (team-2-style game ) (:team-2 game)]\n", + " [:td (:points-2 game)]])\n", + "\n", + "(defn summary-table [games]\n", + " [:table\n", + " [:thead\n", + " [:tr [:td \"Ville\"]\n", + " [:td \"Resultat\"]\n", + " [:td \"Ville\"]\n", + " [:td \"Resultat\"]]]\n", + " (map create-row games)])\n", + "\n", + "(display/hiccup-html (summary-table dodgeball-data))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/Cours 5.ipynb b/Cours 5.ipynb new file mode 100644 index 0000000..14cb631 --- /dev/null +++ b/Cours 5.ipynb @@ -0,0 +1,472 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Cours 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Premierement, faisons un petit test.\n", + "\n", + "Question\n", + "\n", + "Ecrire une fonction qui calcul la moyenne d'une liste" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/moyenner" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(defn moyenner [lst]\n", + " (/ (apply + lst) (count lst)))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(moyenner [ 10 11 12])" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/double-all" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(defn double-number[n]\n", + " (* 2 n))\n", + "\"\"\n", + "(defn double-all [ lst]\n", + " (map #(* 2 %) lst))\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(90 132 24 90)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(double-all [45 66 12 45])" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + ";; 3 < 4 < 10 < 100\\\n", + ";; (< 3 4 10 100)\n", + "(defn larger-than [n]\n", + " (< 3 n))\n", + " \n", + " \n", + "\n", + "(defn new-sum [lst]\n", + " \"Add all the numbers in lst that are larger than 3.\"\n", + " (apply + lst) \n", + " (filter #(< 3 %) lst)\n", + " (apply + (filter #(< 3 %) lst)))\n", + " \n", + "(new-sum [1 5 2 4 4 0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(#(- %1 %2) 5 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1 2 34] \"hello\" :toto]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(let [x [ 1 2 34]\n", + " y \"hello\"\n", + " z :toto]\n", + " [x y z])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/new-sum" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(defn new-sum [lst]\n", + " \"Add all the numbers in lst that are larger than 3.\"\n", + " (let [sub-lst (filter #(< 3 %) lst)]\n", + " (apply + sub-lst)))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/f" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + ";; (reduce f initial-value lst)\n", + "(defn f [acc new-el]\n", + " ;; update acc\n", + " acc)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "; var initial-value = [];\n", + ";for(var e : lst){\n", + "; initial-value.push(f(e));\n", + ";}" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2 5 9)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(reductions + [2 3 4])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3 2 4 6)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(conj (map #(* 2 %)[ 1 2 3])3)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(reduce + [2 3 4])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[12 14 16]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(defn foo [g lst e] \n", + " (conj lst (g e)))\n", + "\n", + "(defn my-map [f lst]\n", + " (let [poartialled-foo (partial foo f)]\n", + " (reduce poartialled-foo [] lst)))\n", + "\n", + "(my-map #(* 2 %) [6 7 8]) ;; 12 14 16\n", + "\n", + ";;#(conj %1 (f %2))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "#'user/toto" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(defn toto [ x y]\n", + " (+ x y))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "190" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "((partial toto 90) 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(51 24 21 324 156)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + ";; 3 * x + 7\n", + "\n", + ";; 17 * 3 <- 17 <- 10\n", + "(let [f1 (map #(* 3 %))\n", + " f2 (map #(+ % 7))]\n", + " (sequence (comp f2 f1) [10 1 0 101 45]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "$$ f(g(x)) = f \\circ g (x) $$" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"8544\"" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(-> 16 \n", + " (* 3)\n", + " double-number\n", + " (* 89)\n", + " str)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"FOO\"" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(-> :foo name clojure.string/upper-case )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Lein-Clojure", + "language": "clojure", + "name": "lein-clojure" + }, + "language_info": { + "file_extension": ".clj", + "mimetype": "text/x-clojure", + "name": "clojure", + "version": "1.9.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}