# Nested collection functions and comprehensions

**URL:** <https://forums.fsharp.org/t/nested-collection-functions-and-comprehensions/2520>\
**Category:** General\
**Tags:** beginners\
**Created:** [November 24, 2022, 2:17pm UTC](https://forums.fsharp.org/t/nested-collection-functions-and-comprehensions/2520 "2022-11-24T14:17:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![krilcebre](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@krilcebre](https://forums.fsharp.org/u/krilcebre)\
**Post date:** [November 24, 2022, 2:17pm UTC](https://forums.fsharp.org/t/nested-collection-functions-and-comprehensions/2520/1 "2022-11-24T14:17:45Z")

</div>

Hi, I’ve recently started learning F# using the currently MEAP book F# in Action and Grokking functional programming (which uses Scala for code demonstrations).

I’ve been doing an example on nested collection functions, where author of the FP book mentioned Comprehensions as a cleaner way of representing nested collection functions, and I can’t seem to find an equivalent in F# anywhere.

Currently, my code looks like this :

```auto
type Book = {
    Title : string
    Authors : string list
}

type Movie = {
    Title : string
}

let books = [
    { Title = "FP in Scala"; Authors = ["Chiusano"; "Bjarnason"] }
    { Title = "The Hobbit"; Authors = ["Tolkien"]}
    { Title = "Modern Java in Action"; Authors = ["Urma"; "Fusco"; "Mycroft"]}
]

let bookAdaptations author = 
    if author = "Tolkien" then [{ Title = "An Unexpected Journey"}; { Title = "The Desolation of Smaug"}] 
    else List.empty

let recommendationFeed (books : Book list) = 
    books
    |> List.collect (fun book ->
         book.Authors 
         |> List.collect (fun author -> 
            author 
            |> bookAdaptations
            |> List.map (fun movie -> $"You may also like {movie.Title} because you liked {author}'s {book.Title}")))

```

This isn’t very readable. The problem is, I need book, author and a movie in order to output a desired message (hence the nesting). Is there a way to make this more readable in F# (without nesting anything)? Author of the Grokking FP book uses syntax similar to this:

```auto
for {
 book <- books
 author <- book.authors
 movie <- bookAdaptations(author)
} yield ...

```

Thanks!

---

<div class="post-metadata">

**Author:** ![krilcebre](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@krilcebre](https://forums.fsharp.org/u/krilcebre)\
**Post date:** [November 24, 2022, 2:40pm UTC](https://forums.fsharp.org/t/nested-collection-functions-and-comprehensions/2520/2 "2022-11-24T14:40:57Z")

</div>

Okay, after some more experimentation I managed to solve it using

```auto
let recommendationFeedV2 (books : Book list) = 
    [for book in books do
     for author in book.Authors do
     for movie in author |> bookAdaptations do 
        yield $"You may also like {movie.Title} because you liked {author}'s {book.Title}"]

```

Looks similar to the LINQ syntax.
