Game Template Scala Template

I worked with the Play Framework for a while, but I'm almost new to Scala Templates. For me, as a C-familiar language developer, sometimes it looks a little strange.

I was wondering if someone here can help me understand this code better, I took it from http://www.playframework.com/documentation/2.2.x/JavaGuide3 (Zentask example)

@(projects: List[Project], todoTasks: List[Task]) @main("Welcome to Play") { <header> <hgroup> <h1>Dashboard</h1> <h2>Tasks over all projects</h2> </hgroup> </header> <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> @if(task.dueDate != null) { <time datetime="@task.dueDate"> @task.dueDate.format("MMM dd yyyy")</time> } @if(task.assignedTo != null && task.assignedTo.email != null) { <span class="assignedTo">@task.assignedTo.email</span> } </li> } </ul> </div> } } </article> } 

These 3 lines really confuse me:

 @todoTasks.groupBy(_.project).map { case (project, tasks) => { @tasks.map { task => 

I really appreciate if anyone can explain in more detail to me what exactly these 3 lines do?

Thanks guys,

+4
source share
2 answers

Good, so there are a few transformations going on here.

@todoTasks.groupBy(_.project) says that todoTask has a field called project , and we need to convert this todoTasks list to Map, where the project is the key and the values ​​are all todoTasks that match the key.

.map { case (project, tasks) => { says that now we have a map, where the key is project , and the value is a list of tasks . And if we have these two elements (project, tasks), then we must do something with each task, something for which it follows =>

you don’t need to have a deep knowledge of scala to be productive as a java play developer, just do your data transformations in your Java controller.

+2
source

I do not think that they are specific for template reproduction in general, but rather examples of the Scala idiomatic functionality. The middle line uses matching with an anonymous function, which is very well covered by this guide . The other two are function calls on collections that take functions as parameters. They are called "higher order functions" and are one of the key tools for functional programming..map, in particular, is the key to FP. Daniel Spiewak Scala Collections for easily bored - a great place to start working on such features.

+1
source

Source: https://habr.com/ru/post/1502684/


All Articles