Jeremy
Jeremy

Reputation: 2536

What does this block of Scala code mean

Hi i am relatively new to Scala, i would like to try this Play 2 Framework and stuck with the following code (used as a template)

<article  class="tasks">
        @todoTasks.groupBy(_.project).map {
            case (project, tasks) => {
                <div class="folder" data-folder-id="@project.id">
                    <header>
                        <h3>@project.name</h3>
                    </header>
                    <ul class="list">
                        @tasks.map { task =>
                            <li data-task-id="@task.id">
                                <h4>@task.title</h4>
                            </li>
                        }
                    </ul>
                </div>
            }
        }
    </article>

What does this line mean?

@todoTasks.groupBy(_.project).map {

and how do you use scala *.map in the context of Play 2 Framework.

I'd appreciate it if you could explain it in exact detail as i am relatively new to Scala (coming from Java developer)

Upvotes: 2

Views: 147

Answers (1)

axtavt
axtavt

Reputation: 242686

groupBy() applies the given lambda expression (_.project, which extracts project from a task) to each element of the collection, and groups elements by results of that expression.

So, it converts a list of tasks into a list of tuples (project, tasksOfThatProject).

Now, map() applies its lambda expression to each element of the collection (i.e. to each of these tuples).

Lambda expression given to map() renders a tuple as project name and a list of its tasks.

Upvotes: 4

Related Questions