Skip to content

Commit c319355

Browse files
committed
Update Part01/Chapter06
1 parent 32e13ba commit c319355

File tree

1 file changed

+130
-3
lines changed

1 file changed

+130
-3
lines changed

Part1-Functional/06-FirstClassFunctions/FirstClassFunction.ipynb

+130-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
"id": "f2fe6be8",
1414
"metadata": {},
1515
"source": [
16-
"> [!NOTE]\n",
17-
">\n",
18-
"> Suggest download it first and then use it"
16+
"> 📍 Suggest download it first and then use it 📍"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"### List Comprehension Alternative To `map`"
1924
]
2025
},
2126
{
@@ -3359,6 +3364,128 @@
33593364
"print([x**2 for x in l if x**2 > 25])\n",
33603365
"print([x for x in l if x**2 > 25])"
33613366
]
3367+
},
3368+
{
3369+
"cell_type": "markdown",
3370+
"id": "7fc66733",
3371+
"metadata": {},
3372+
"source": [
3373+
"#### 📍 NOTE 📍"
3374+
]
3375+
},
3376+
{
3377+
"cell_type": "markdown",
3378+
"id": "df92ce1b",
3379+
"metadata": {},
3380+
"source": [
3381+
"Difference between thease two:"
3382+
]
3383+
},
3384+
{
3385+
"cell_type": "code",
3386+
"execution_count": 1,
3387+
"id": "517c8f3f",
3388+
"metadata": {},
3389+
"outputs": [],
3390+
"source": [
3391+
"l = [1, 2, 3, 4]\n",
3392+
"\n",
3393+
"def sq(x):\n",
3394+
" return x ** 2"
3395+
]
3396+
},
3397+
{
3398+
"cell_type": "markdown",
3399+
"id": "79a4af5b",
3400+
"metadata": {},
3401+
"source": [
3402+
"> When we use map it's like generator and doesn't calculate it.\n",
3403+
"\n",
3404+
"> Memory efficiency."
3405+
]
3406+
},
3407+
{
3408+
"cell_type": "code",
3409+
"execution_count": 11,
3410+
"id": "6c026729",
3411+
"metadata": {},
3412+
"outputs": [
3413+
{
3414+
"name": "stdout",
3415+
"output_type": "stream",
3416+
"text": [
3417+
"1\n",
3418+
"4\n",
3419+
"9\n",
3420+
"16\n"
3421+
]
3422+
}
3423+
],
3424+
"source": [
3425+
"# Use map\n",
3426+
"\n",
3427+
"result = map(sq, l)\n",
3428+
"for i in result:\n",
3429+
" print(i)"
3430+
]
3431+
},
3432+
{
3433+
"cell_type": "markdown",
3434+
"id": "d8f31a0d",
3435+
"metadata": {},
3436+
"source": [
3437+
"> When we use list comprehemsion, it's calculate all of theme"
3438+
]
3439+
},
3440+
{
3441+
"cell_type": "code",
3442+
"execution_count": 12,
3443+
"id": "5379c2fc",
3444+
"metadata": {},
3445+
"outputs": [
3446+
{
3447+
"name": "stdout",
3448+
"output_type": "stream",
3449+
"text": [
3450+
"[1, 4, 9, 16]\n"
3451+
]
3452+
}
3453+
],
3454+
"source": [
3455+
"# use list comprehension\n",
3456+
"\n",
3457+
"result = [x**2 for x in l]\n",
3458+
"print(result)"
3459+
]
3460+
},
3461+
{
3462+
"cell_type": "markdown",
3463+
"id": "d19dccca",
3464+
"metadata": {},
3465+
"source": [
3466+
"> We can use generator"
3467+
]
3468+
},
3469+
{
3470+
"cell_type": "code",
3471+
"execution_count": 13,
3472+
"id": "24b840f2",
3473+
"metadata": {},
3474+
"outputs": [
3475+
{
3476+
"name": "stdout",
3477+
"output_type": "stream",
3478+
"text": [
3479+
"<generator object <genexpr> at 0x7f3644498790>\n"
3480+
]
3481+
}
3482+
],
3483+
"source": [
3484+
"# Use generator comprehension\n",
3485+
"\n",
3486+
"result = (x**2 for x in l)\n",
3487+
"print(result)"
3488+
]
33623489
}
33633490
],
33643491
"metadata": {

0 commit comments

Comments
 (0)