1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| // 불변 데이터를 위한 클래스
class Task {
constructor(id, title, status, priority, dueDate) {
Object.freeze({
id,
title,
status,
priority,
dueDate: new Date(dueDate)
});
}
}
// 순수 함수들
const filterByStatus = (tasks, status) =>
tasks.filter(task => task.status === status);
const sortByPriority = tasks =>
[...tasks].sort((a, b) => b.priority - a.priority);
const filterByDueDate = (tasks, date) =>
tasks.filter(task => task.dueDate.toDateString() === date.toDateString());
const groupByStatus = tasks =>
tasks.reduce((acc, task) => ({
…acc,
[task.status]: […(acc[task.status] || []), task]
}), {});
// 고차 함수: 함수 조합을 위한 유틸리티
const compose = (…fns) =>
initialValue =>
fns.reduceRight((value, fn) => fn(value), initialValue);
const pipe = (…fns) =>
initialValue =>
fns.reduce((value, fn) => fn(value), initialValue);
// 작업 관리를 위한 함수형 파이프라인
const createTaskManager = (initialTasks = []) => {
// 불변성을 위해 깊은 복사 수행
const tasks = […initialTasks];
const getHighPriorityTasks = () =>
pipe(
filterByStatus,
sortByPriority
)(tasks, 'pending');
const getTodaysTasks = () =>
pipe(
tasks => filterByDueDate(tasks, new Date()),
sortByPriority
)(tasks);
const getTasksSummary = () => ({
totalTasks: tasks.length,
statusGroups: groupByStatus(tasks),
highPriorityCount: getHighPriorityTasks().length,
todaysTasks: getTodaysTasks()
});
// 새로운 작업 추가 (불변성 유지)
const addTask = (taskData) => {
const newTask = new Task(
taskData.id,
taskData.title,
taskData.status,
taskData.priority,
taskData.dueDate
);
return createTaskManager([…tasks, newTask]);
};
return {
getHighPriorityTasks,
getTodaysTasks,
getTasksSummary,
addTask
};
};
// 사용 예시
const main = () => {
const initialTasks = [
new Task(1, "기능 구현", "pending", 3, "2024-12-01"),
new Task(2, "버그 수정", "in_progress", 5, "2024-12-01"),
new Task(3, "문서 작성", "completed", 2, "2024-12-02"),
new Task(4, "코드 리뷰", "pending", 4, "2024-12-01")
];
const taskManager = createTaskManager(initialTasks);
console.log("작업 요약:", taskManager.getTasksSummary());
console.log("높은 우선순위 작업:", taskManager.getHighPriorityTasks());
console.log("오늘의 작업:", taskManager.getTodaysTasks());
// 새 작업 추가 (불변성 유지)
const updatedManager = taskManager.addTask({
id: 5,
title: "테스트 작성",
status: "pending",
priority: 4,
dueDate: "2024-12-01"
});
console.log("업데이트된 작업 요약:", updatedManager.getTasksSummary());
};
main();
|