File tree Expand file tree Collapse file tree 1 file changed +95
-0
lines changed
Expand file tree Collapse file tree 1 file changed +95
-0
lines changed Original file line number Diff line number Diff line change 585585; ; **
586586; ;; # LAB: Collections
587587; ;;
588+ ; ;; **Note:** All lab solutions are in the section following this one if you want to check your answers!
589+ ; ;;
588590; ;; ## Building Collections
589591; ;;
590592; ;; ### Representing scores in a game
765767 ___ )
766768; ; @@
767769
770+ ; ; **
771+ ; ;; # Lab Solutions
772+ ; ;;
773+ ; ;; ### Add a new player to the scores
774+ ; ; **
775+
776+ ; ; @@
777+ (assoc scores " Mel" 0 )
778+ ; ; @@
779+
780+ ; ; **
781+ ; ;; ### Representing a player
782+ ; ;;
783+ ; ; **
784+
785+ ; ; @@
786+ ; ; as map
787+ {:name " Una" :ranking 43 }
788+
789+ ; ; as record
790+ (defrecord Player [name ranking])
791+
792+ ; ; define a player
793+ (->Player " Una" 43 )
794+ ; ; @@
795+
796+ ; ; **
797+ ; ;; ### Retrieving a player's name
798+ ; ; **
799+
800+ ; ; @@
801+ (get player :ranking )
802+ (player :ranking ) ; ; will not work for records
803+ (:ranking player)
804+ ; ; @@
805+
806+ ; ; **
807+ ; ;; ### Advance to the next round
808+ ; ; **
809+
810+ ; ; @@
811+ (defn next-round [game]
812+ (update-in game [:round ] inc))
813+ ; ; @@
814+
815+ ; ; **
816+ ; ;; ### Update the scores
817+ ; ;;
818+ ; ; **
819+
820+ ; ; @@
821+ (defn add-score [name score]
822+ (update-in game [:scores name] + score))
823+ ; ; @@
824+
825+ ; ; **
826+ ; ;; ### Add a new player
827+ ; ;;
828+ ; ; **
829+
830+ ; ; @@
831+ (defn add-player [game name]
832+ (update-in (update-in game [:players ] conj {:name name})
833+ [:scores ] assoc name 0 ))
834+ ; ; @@
835+
836+ ; ; **
837+ ; ;; ### Looking up a player
838+ ; ; **
839+
840+ ; ; **
841+ ; ;; A map indexing the players by name.
842+ ; ; **
843+
844+ ; ; **
845+ ; ;; ### Find a player
846+ ; ; **
847+
848+ ; ; @@
849+ (defn find-player [game name]
850+ (get-in game [:players name]))
851+ ; ; @@
852+
853+ ; ; **
854+ ; ;; ### Remove a player
855+ ; ;;
856+ ; ; **
857+
858+ ; ; @@
859+ (defn remove-player [game name]
860+ (update-in game [:players ] dissoc name))
861+ ; ; @@
862+
768863; ; **
769864; ;;
770865; ;; ## Navigation
You can’t perform that action at this time.
0 commit comments