Hoi !
I'm very new in Gleam and so I wanted to create a tiny application to grab and fetch music.
So I learned how to do http and start to understand how to wired my brain to use the Result type however I'm not yet flaunt in my thinking.
So earlier I needed to parse from the Tidal API tracks from albums but in their response data they send both a type for video and track but I only cared about track.
So I wanted to make a decoder using the decode package however I didn't found somewhere some builtin function to like decode list with a filter.
So I did find a code that work by making the list decode Option(Track) than later use a filter_map to extract the Some(Track) but is there better way to approach the problem or do you think this is the best I can do ?
pub fn album_tracks_decoder() -> decode.Decoder(AlbumTracks) {
use limit <- decode.field("limit", decode.int)
use offset <- decode.field("offset", decode.int)
use total_number_of_items <- decode.field("totalNumberOfItems", decode.int)
use tracks <- decode.field(
"items",
{
use typed <- decode.field("type", decode.string)
case typed {
"track" -> {
use track <- decode.field("item", track_decoder())
decode.success(Some(track))
}
_ -> decode.success(None)
}
}
|> decode.list,
)
let tracks = list.filter_map(tracks, fn(x) { option.to_result(x, Nil) })
decode.success(AlbumTracks(limit:, offset:, total_number_of_items:, tracks:))
}

