Last active
May 27, 2021 18:53
-
-
Save arnaudgeiser/fc9e2e20d04e568878fec894a1634d4f to your computer and use it in GitHub Desktop.
Clojure - null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Unversité valide! | |
(def universite | |
{:nom "UniFR" | |
:etudiants [{:nom "Geiser" | |
:prenom "Arnaud" | |
:admissions [{:semestre "SP-2020" :statut "Immatricule"} | |
{:semestre "SA-2020" :statut "Immatricule"} | |
{:semestre "SP-2021" :statut "Immatricule"} | |
{:semestre "SA-2021" :statut "Immatricule"}]} | |
{:nom "Rosset" | |
:prenom "Matthieu" | |
:admissions [{:semestre "SP-2021" :statut "Immatricule"} | |
{:semestre "SA-2021" :statut "Admis"}]}]}) | |
;; Université sans étudiants | |
(def universite-pourrie | |
{:nom "UniFR pourrie" | |
:etudiants nil}) | |
;; Université avec étudiants sans admissions | |
(def universite-pourrie2 | |
{:nom "UniFR pourrie" | |
:etudiants [{:nom "Geiser" | |
:admissions nil}]}) | |
;; Nombre d'étudiants immatriculés dans une Uni par semestre | |
(defn nb-etudiants-immatricules [uni semestre] | |
(let [etudiants (:etudiants uni)] | |
(->> etudiants | |
(mapcat :admissions) | |
(filter #(= "Immatricule" (:statut %))) | |
(map :semestre) | |
(filter (partial = semestre)) | |
(count)))) | |
(prn (nb-etudiants-immatricules universite "SP-2020")) ;; => 1 | |
(prn (nb-etudiants-immatricules universite "SP-2021")) ;; => 2 | |
(prn (nb-etudiants-immatricules universite "SA-2021")) ;; => 1 | |
;; Avec null => Pas de problème | |
(prn (nb-etudiants-immatricules nil "SP-2020")) ;; => 0 | |
;; Avec etudiants null => Pas de problème | |
(prn (nb-etudiants-immatricules universite-pourrie "SA-2021")) ;; => 0 | |
;; Avec admissions null => Pas de problème | |
(prn (nb-etudiants-immatricules universite-pourrie2 "SA-2021")) ;; => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment