- A recently-used-list is initially empty.
- Songs in the list are unique, so repeatedly played songs should only appear once in the list
- Songs can be looked up by index, which counts from zero.
- Empty song names are not allowed.
- Add a limit to the number of songs the list contains, with the least recently added items dropped when that limit is reached.
Create a new Clojure project
Use Clojure CLI tools and clj-new to create a new Clojure project.
clojure -M:new app practicalli/recent-songlist
(ns practicalli.recent-songlist-test (:require [clojure.test :refer [deftest is testing]] [playground.recent-song-list :as SUT]))
Define a recent song list
Write a test to check a song-list exists
(deftest song-list-empty-test (testing "Is song list empty if we haven't added any songs" (is (=  recent-songs))))
(deftest song-list-empty-test-2 (testing "Is song list empty if we haven't added any songs" (is (empty? recent-songs))))
Write a test to add a song to the list
(deftest adding-songs-test (testing "add song returns a song list with entries" (is (not (empty? (add-song "Barry Manilow - Love on the rocks" recent-songs))))) (testing "add multiple song returns a song list with entries" (is (not (empty? (->> recent-songs (add-song "Barry Manilow - Love on the rocks") (add-song "Phil Colins - Sususudio" )))))))
The results of the
remove expression are then passed to the
cons expression as its last argument. The
cons expression simply adds the new song to the start of the list, making it the most recent song.
(def recent-songs ) (defn add-song [song song-list] (->> song-list (remove #(= song %)) (cons song)))