DojoDeveloppement/Lundi21Janvier2008Rétrospective du /Lundi14Janvier2008 :
Kata LAGS en Haskell par CT. Objectif pas atteint. Difficulté à progresser par petits pas TDD.
Christophe était fatigué et il n'avait pas de binôme. Le code est devenu illisible sur la fin.
Une structure de données graphe aurait été plus adaptée qu'une liste.
Il y a eu une discussion intéressante sur le refactoring face à la généralisation abusive.
Thèmes possibles :
Le code produit :
module Main
where
import Test.HUnit
import Lags
main = runTestTT $ TestList [caUneDemandeEgalePrixDemande
,caDeuxDemandesNonConnecteesEgalePrixMax
,maxPrendLeMaxDeNombres
,caDeuxDemandesCompatiblesEgalePrixDeuxDemandes
,caUneDemandeSuivieDeDeuxIncompatibles
,testRecetteSurCalculValeurGraphe
,listeVideEnGraphe
,listeUneDemandeEnGraphe
,listeDeuxDemandesIncompatiblesEnGraphe
,listeDeuxDemandesCompatiblesEnGraphe
,listeUneDemandePlusDeuxCompatiblesPremiere
,listeTroisDemandesCompatibles
,superTestRecette]
maxPrendLeMaxDeNombres =
maxListe [-4, -34, -5, -2, -67, -14] ~?= -2
a = Demande 0 5 7
b = Demande 3 7 10
c = Demande 5 8 4
d = Demande 6 8 5
e = Demande 8 10 3
caUneDemandeEgalePrixDemande =
let
graphe = [Noeud a [] ]
in chiffreAffaire graphe ~?= 7
caDeuxDemandesNonConnecteesEgalePrixMax =
let
graphe = [Noeud a [],
Noeud b []]
in chiffreAffaire graphe ~?= 10
caDeuxDemandesCompatiblesEgalePrixDeuxDemandes =
let
graphe = [Noeud a [Noeud c []],
Noeud c []]
in chiffreAffaire graphe ~?= 11
caUneDemandeSuivieDeDeuxIncompatibles =
let
graphe = [Noeud a [Noeud c [],
Noeud d []],
Noeud c [],
Noeud d []]
in chiffreAffaire graphe ~?= 12
testRecetteSurCalculValeurGraphe =
let
graphe = [Noeud a [Noeud c [],
Noeud d []],
Noeud b [],
Noeud c [],
Noeud d []]
in chiffreAffaire graphe ~?= 12
listeVideEnGraphe =
listeEnGraphe [] ~?= []
listeUneDemandeEnGraphe =
listeEnGraphe [a] ~?= [Noeud a []]
listeDeuxDemandesIncompatiblesEnGraphe =
listeEnGraphe [a,b] ~?= [Noeud a [],
Noeud b []]
listeDeuxDemandesCompatiblesEnGraphe =
listeEnGraphe [a,c] ~?= [Noeud a [Noeud c []],
Noeud c []]
listeUneDemandePlusDeuxCompatiblesPremiere =
listeEnGraphe [a,c,d] ~?= [Noeud a [Noeud c [],
Noeud d []],
Noeud c [],
Noeud d []]
listeTroisDemandesCompatibles =
listeEnGraphe [a,c,e] ~?= [Noeud a [Noeud c [Noeud e []],
Noeud e []],
Noeud c [Noeud e []],
Noeud e []]
superTestRecette =
chiffreAffaireListe [a,b,c,d,e] ~?= 15
module Lags
where
data Demande = Demande { debut :: Heure,
fin :: Heure,
prix :: Prix }
deriving (Show, Eq)
type Heure = Integer
type Prix = Integer
data Noeud = Noeud { demande :: Demande,
suivants :: [Noeud] }
deriving (Show, Eq)
type Graphe = [Noeud]
chiffreAffaire :: Graphe -> Prix
chiffreAffaire [] = 0
chiffreAffaire noeuds = maxListe (map valeur noeuds)
valeur (Noeud a suivants) = prix a + (chiffreAffaire suivants)
maxListe [x] = x
maxListe (x:xs) = max x (maxListe xs)
listeEnGraphe :: [Demande]-> Graphe
listeEnGraphe [] = []
listeEnGraphe (d:ds) = Noeud d (noeudsSuivants d ds) : (listeEnGraphe ds)
noeudsSuivants d = listeEnGraphe . filter (`peutSuivre` d)
a `peutSuivre` b = fin b <= debut a
chiffreAffaireListe :: [Demande] -> Prix
chiffreAffaireListe = chiffreAffaire . listeEnGraphe