Skip to content

Commit c2f6736

Browse files
committed
Fix Function Composition implementation issues
1 parent 0e979af commit c2f6736

3 files changed

Lines changed: 53 additions & 32 deletions

File tree

JavaScript/2629. Function Composition/Claude Code Sonnet 4.6 extended/FunctionComposition_TS.ipynb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@
4141
"\n",
4242
"---\n",
4343
"\n",
44-
"## 4. 実装コード\n",
45-
"\n",
46-
"```typescript\n",
44+
"## 4. 実装コード"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "implementation",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
4754
"// Analyze Complexity\n",
4855
"// Runtime 56 ms\n",
4956
"// Beats 55.84%\n",
@@ -66,24 +73,25 @@
6673
" };\n",
6774
"}\n",
6875
"\n",
69-
"/**\n",
70-
" * const fn = compose([x => x + 1, x => 2 * x])\n",
71-
" * fn(4) // 9\n",
72-
" */\n",
73-
"```\n",
76+
"// 動作確認\n",
77+
"const fn1 = compose([x => x + 1, x => x * x, x => 2 * x]);\n",
78+
"console.log(\"Example 1 (x=4): 2*4=8 -> 8*8=64 -> 64+1=65 :\", fn1(4));\n",
7479
"\n",
75-
"**動作確認(コメントで検証):**\n",
76-
"```typescript\n",
77-
"// Example 1: [x+1, x*x, 2*x], x=4\n",
78-
"// 2*4=8 → 8*8=64 → 64+1=65 ✓\n",
80+
"const fn2 = compose([x => 10 * x, x => 10 * x, x => 10 * x]);\n",
81+
"console.log(\"Example 2 (x=1): 10 -> 100 -> 1000 :\", fn2(1));\n",
7982
"\n",
80-
"// Example 2: [10x, 10x, 10x], x=1\n",
81-
"// 10 → 100 → 1000 ✓\n",
82-
"\n",
83-
"// Example 3: [], x=42\n",
84-
"// reduceRight の初期値 42 がそのまま返る → 42 ✓\n",
85-
"```\n",
83+
"const fn3 = compose([]);\n",
84+
"console.log(\"Example 3 (x=42): 42 :\", fn3(42));\n",
8685
"\n",
86+
"// Interactive checks\n",
87+
"compose([x => x + 1, x => 2 * x])(4)"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"id": "points",
93+
"metadata": {},
94+
"source": [
8795
"**ポイント:**\n",
8896
"- `functions` を `readonly F[]` とすることで入力配列の不変性を型レベルで保証。\n",
8997
"- `reduceRight` の初期値 `x` が空配列時の恒等関数の役割を自然に担うため、空配列の特別処理が不要。\n",
@@ -101,7 +109,7 @@
101109
"file_extension": ".ts",
102110
"mimetype": "text/typescript",
103111
"name": "typescript",
104-
"version": "5.3.3"
112+
"version": "5.9.3"
105113
}
106114
},
107115
"nbformat": 4,

JavaScript/2629. Function Composition/Claude Code Sonnet 4.6 extended/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- [正しさのスケッチ](#correctness)
99
- [計算量](#complexity)
1010
- [TypeScript 実装](#impl)
11-
- [TypeScript / V8 最適化ポイント](#cpython)
11+
- [TypeScript / V8 最適化ポイント](#typescript-v8)
1212
- [エッジケースと検証観点](#edgecases)
1313
- [FAQ](#faq)
1414

@@ -49,11 +49,9 @@ compose([f, g, h])(x) = f(g(h(x)))
4949

5050
```mermaid
5151
flowchart TD
52-
Start[compose called with functions array] --> Empty{functions.length is 0?}
53-
Empty -- Yes --> Identity[Return identity x equals x]
54-
Empty -- No --> RetFn[Return closure fn x]
52+
Start[compose called] --> RetFn[Return closure fn x]
5553
RetFn --> Call[fn x is called]
56-
Call --> RR[reduceRight over functions]
54+
Call --> RR[Start reduceRight with initial value x]
5755
RR --> Step{More functions?}
5856
Step -- Yes --> Apply[Apply current fn to acc]
5957
Apply --> Step
@@ -157,7 +155,7 @@ function compose(functions: readonly F[]): F {
157155

158156
---
159157

160-
<h2 id="cpython">TypeScript / V8 最適化ポイント</h2>
158+
<h2 id="typescript-v8">TypeScript / V8 最適化ポイント</h2>
161159

162160
| 観点 | 内容 |
163161
| --------------------------- | ----------------------------------------------------------------------------------------- |

JavaScript/2629. Function Composition/Claude Code Sonnet 4.6 extended/README_react.html

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>LeetCode 2629 - Function Composition</title>
7-
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
8-
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
7+
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
8+
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
99
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
1010
<script src="https://cdn.tailwindcss.com"></script>
1111
<link
@@ -167,6 +167,17 @@ <h3 class="text-lg font-bold text-[#e53170] mb-3">入出力例</h3>
167167
</div>
168168
<div class="text-[#e53170] font-bold mono mt-1">Output: 65</div>
169169
</div>
170+
<div class="bg-[#0f0e17] rounded-2xl p-4 border border-white/5">
171+
<div class="text-xs text-[#a7a9be] mb-2 mono">Example 2</div>
172+
<div class="mono text-sm text-[#e7c7d4]">
173+
functions = [x=&gt;10*x, x=&gt;10*x, x=&gt;10*x]
174+
</div>
175+
<div class="mono text-sm text-[#a7a9be]">x = 1</div>
176+
<div class="mono text-sm text-[#fffffe] mt-2">
177+
// 10*1=10 → 10*10=100 → 10*100=1000
178+
</div>
179+
<div class="text-[#e53170] font-bold mono mt-1">Output: 1000</div>
180+
</div>
170181
<div class="bg-[#0f0e17] rounded-2xl p-4 border border-white/5">
171182
<div class="text-xs text-[#a7a9be] mb-2 mono">Example 3</div>
172183
<div class="mono text-sm text-[#e7c7d4]">functions = []</div>
@@ -885,6 +896,8 @@ <h3 class="text-lg font-bold text-[#e53170] mb-3">入出力例</h3>
885896
<svg
886897
viewBox="0 0 580 220"
887898
style={{ maxWidth: '100%', height: 'auto', marginTop: '24px' }}
899+
role="img"
900+
aria-label={`Step visualization: ${label || 'Initial state'}. Accumulator is ${acc}.`}
888901
>
889902
<defs>
890903
<marker
@@ -1125,7 +1138,7 @@ <h3 class="text-lg font-bold text-[#e53170] mb-3">入出力例</h3>
11251138
fontFamily="DM Mono"
11261139
fontWeight="900"
11271140
>
1128-
4
1141+
{acc}
11291142
</text>
11301143
</g>
11311144
)}
@@ -1138,6 +1151,12 @@ <h3 class="text-lg font-bold text-[#e53170] mb-3">入出力例</h3>
11381151
const [isPlaying, setIsPlaying] = useState(false);
11391152
const timerRef = useRef(null);
11401153

1154+
useEffect(() => {
1155+
if (window.Prism) {
1156+
window.Prism.highlightAll();
1157+
}
1158+
}); // Run on every render to ensure code blocks are highlighted
1159+
11411160
useEffect(() => {
11421161
if (isPlaying) {
11431162
if (activeStep > stepsData.length) {
@@ -1278,10 +1297,6 @@ <h3 className="text-lg font-bold text-[#fffffe] mt-0 mb-2">
12781297

12791298
const root = ReactDOM.createRoot(document.getElementById('react-root'));
12801299
root.render(<StepViz />);
1281-
1282-
setTimeout(() => {
1283-
Prism.highlightAll();
1284-
}, 300);
12851300
</script>
12861301
</body>
12871302
</html>

0 commit comments

Comments
 (0)