1
+ case class Lesson (name : String , position : Option [Int ] = None )
2
+ case class Section (title : String , resetLessonPosition : Boolean , lessons : List [Lesson ],position : Option [Int ] = None )
3
+
4
+ val sections = List (
5
+ Section (" Getting started" , false , List (
6
+ Lesson (" Welcome" ),
7
+ Lesson (" Installation" )
8
+ )),
9
+ Section (" Basic operator" , false , List (
10
+ Lesson (" Addition / Subtraction" ),
11
+ Lesson (" Multiplication / Division" ),
12
+ )),
13
+ Section (" Advanced topics" , true , List (
14
+ Lesson (" Mutability" ),
15
+ Lesson (" Immutability" ),
16
+ ))
17
+ )
18
+
19
+
20
+ val (newSections,_,_) = sections.foldLeft((List [Section ](),0 ,0 )){
21
+ case ((sections,sectionCounter : Int ,lessonCounter : Int ),current) =>
22
+ val nextSession = sectionCounter + 1
23
+ val lessonBase = if (current.resetLessonPosition) 0 else lessonCounter
24
+ val (lessons,nextLesson)= current.lessons.foldLeft((List [Lesson ](),lessonBase)){
25
+ case ((lessons,lessonCounter : Int ),currentLesson ) =>
26
+ val nextLesson = lessonCounter + 1
27
+ (lessons :+ currentLesson.copy(position= Some (nextLesson)),nextLesson)
28
+ }
29
+ (sections :+ current.copy(position = Some (nextSession),lessons= lessons),nextSession,nextLesson)
30
+ }
31
+ newSections
0 commit comments