Skip to content

Commit 0603b14

Browse files
authored
Merge pull request #73 from jvelez1/crystal
Crystal With Map
2 parents e15548e + 0559ba7 commit 0603b14

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

crystal/with_map.cr

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
sections = [
2+
{
3+
"title": "Getting started",
4+
"reset_lesson_position": false,
5+
"lessons": [
6+
{"name": "Welcome"},
7+
{"name": "Installation"}
8+
]
9+
},
10+
11+
{
12+
"title": "Basic operator",
13+
"reset_lesson_position": false,
14+
"lessons": [
15+
{"name": "Addition / Subtraction"},
16+
{"name": "Multiplication / Division"}
17+
]
18+
},
19+
20+
{
21+
"title": "Advanced topics",
22+
"reset_lesson_position": true,
23+
"lessons": [
24+
{"name": "Mutability"},
25+
{"name": "Immutability"}
26+
]
27+
}
28+
]
29+
30+
def indexing_lessons(lessons : Array)
31+
lessons.map_with_index(1) { |lesson, index| lesson.merge({ position: index}) }
32+
end
33+
34+
selected_lessons = sections.map { |section| section[:lessons] }.flatten
35+
indexed_lessons = indexing_lessons(selected_lessons)
36+
37+
formated_section = sections.map do |section|
38+
formated_lessons = indexed_lessons.shift(section[:lessons].size)
39+
if section[:reset_lesson_position]
40+
formated_lessons = indexing_lessons(section[:lessons])
41+
end
42+
section.merge(lessons: formated_lessons)
43+
end
44+
45+
p formated_section

0 commit comments

Comments
 (0)