From a7af989d93ee177b85d2295fdb1626b499271732 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Wed, 24 May 2023 21:57:02 +0530 Subject: [PATCH] Implement Map Function --- ui/utils.go | 7 +++++++ ui/view_album.go | 10 ++++------ ui/view_liked.go | 9 ++++----- ui/view_playlist.go | 10 ++++------ ui/view_recent.go | 9 ++++----- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/ui/utils.go b/ui/utils.go index 3ba8415..e0952c6 100644 --- a/ui/utils.go +++ b/ui/utils.go @@ -131,3 +131,10 @@ func artistName(s []spotify.SimpleArtist) string { func wrap(args ...interface{}) []interface{} { return args } + +func Map[K any, V any](a []K, f func(K) V) (res []V) { + for _, v := range a { + res = append(res, f(v)) + } + return +} diff --git a/ui/view_album.go b/ui/view_album.go index 2fd1cb4..128decc 100644 --- a/ui/view_album.go +++ b/ui/view_album.go @@ -65,12 +65,10 @@ func (a *AlbumView) AddToPlaylist() { } func (a *AlbumView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *tcell.EventKey { - tracks := make([]spotify.ID, 0) - sTracks := (*(*a.currentFullAlbum).Tracks) - for k := start; k <= end; k++ { - tracks = append(tracks, sTracks[k].ID) - } - addToPlaylist(tracks) + addToPlaylist(Map((*(*a.currentFullAlbum).Tracks)[start:end+1], + func(s spotify.SimpleTrack) spotify.ID { + return s.ID + })) return nil } diff --git a/ui/view_liked.go b/ui/view_liked.go index 9e26434..5e0a1b9 100644 --- a/ui/view_liked.go +++ b/ui/view_liked.go @@ -53,11 +53,10 @@ func (l *LikedSongsView) AddToPlaylist() { } func (l *LikedSongsView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *tcell.EventKey { - tracks := make([]spotify.ID, 0) - for k := start; k <= end; k++ { - tracks = append(tracks, (*l.likedSongs)[k].ID) - } - addToPlaylist(tracks) + addToPlaylist(Map((*l.likedSongs)[start:end+1], + func(s spotify.SavedTrack) spotify.ID { + return s.ID + })) return nil } diff --git a/ui/view_playlist.go b/ui/view_playlist.go index a017531..3b24cd5 100644 --- a/ui/view_playlist.go +++ b/ui/view_playlist.go @@ -63,12 +63,10 @@ func (p *PlaylistView) AddToPlaylist() { } func (p *PlaylistView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *tcell.EventKey { - tracks := make([]spotify.ID, 0) - sTracks := (*(*p.currentUserFullPlaylist).Tracks) - for k := start; k <= end; k++ { - tracks = append(tracks, sTracks[k].Track.ID) - } - addToPlaylist(tracks) + addToPlaylist(Map((*(*p.currentUserFullPlaylist).Tracks)[start:end+1], + func(s spotify.PlaylistTrack) spotify.ID { + return s.Track.ID + })) return nil } diff --git a/ui/view_recent.go b/ui/view_recent.go index a298a0e..5fcd12d 100644 --- a/ui/view_recent.go +++ b/ui/view_recent.go @@ -42,11 +42,10 @@ func (r *RecentlyPlayedView) AddToPlaylist() { } func (r *RecentlyPlayedView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *tcell.EventKey { - tracks := make([]spotify.ID, 0) - for k := start; k <= end; k++ { - tracks = append(tracks, r.recentlyPlayed[k].Track.ID) - } - addToPlaylist(tracks) + addToPlaylist(Map(r.recentlyPlayed[start:end+1], + func(r spotify.RecentlyPlayedItem) spotify.ID { + return r.Track.ID + })) return nil }