Implement Map Function

This commit is contained in:
aditya-K2
2023-05-24 21:57:02 +05:30
parent 0ca835d3ad
commit a7af989d93
5 changed files with 23 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}