DEV Community

Discussion on: Daily Challenge #137 - Help the Bookseller

Collapse
 
nickholmesde profile image
Nick Holmes

Another F# one.

(But as I read it, the return value should be a string, not a list, and therefore a little input validation is needed to handle the input arrays being empty).

let stockByCategory(books: string[]) (cats: string[]): string =
    if books = [||] || cats = [||] then
        ""
    else
        cats
        |> Seq.map
            (fun cat -> books |> Seq.filter (fun book -> book.StartsWith cat)
                              |> Seq.sumBy(fun book -> book.Split(' ').[1] |> int)
                              |> (sprintf "(%s : %d)" cat))
        |> String.concat " - "