summaryrefslogtreecommitdiff
path: root/tests/examplefiles/rst/functional.rst.output
blob: 161957d1ff601ed6c903b4bc38180fb7371b5052 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
'Functional Programming HOWTO' Generic.Heading
'\n'          Text

'================================' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'**Version 0.30**' Generic.Strong
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'(This is a first draft.  Please send comments/error' Text
'\n'          Text.Whitespace

'reports/suggestions to amk@amk.ca.  This URL is probably not going to' Text
'\n'          Text.Whitespace

'be the final location of the document, so be careful about linking to' Text
'\n'          Text.Whitespace

'it -- you may want to add a disclaimer.)' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"In this document, we'll take a tour of Python's features suitable for" Text
'\n'          Text.Whitespace

'implementing programs in a functional style.  After an introduction to' Text
'\n'          Text.Whitespace

"the concepts of functional programming, we'll look at language" Text
'\n'          Text.Whitespace

'features such as iterators and generators and relevant library modules' Text
'\n'          Text.Whitespace

'such as '    Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'functools'   Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'..'          Punctuation
' '           Text
'contents'    Operator.Word
'::'          Punctuation
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Introduction' Generic.Heading
'\n'          Text

'----------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'This section explains the basic concept of functional programming; if' Text
'\n'          Text.Whitespace

"you're just interested in learning about Python language features," Text
'\n'          Text.Whitespace

'skip to the next section.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Programming languages support decomposing problems in several different ' Text
'\n'          Text.Whitespace

'ways'        Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' Most programming languages are ' Text
'**procedural**' Generic.Strong
':'           Text
' '           Text
'\n'          Text.Whitespace

'  programs are lists of instructions that tell the computer what to' Text
'\n'          Text.Whitespace

"  do with the program's input." Text
'\n'          Text.Whitespace

'  C, Pascal, and even Unix shells are procedural languages.' Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' In '        Text
'**declarative**' Generic.Strong
' languages, you write a specification that describes ' Text
'\n'          Text.Whitespace

'  the problem to be solved, and the language implementation figures out ' Text
'\n'          Text.Whitespace

'  how to perform the computation efficiently.  SQL is the declarative ' Text
'\n'          Text.Whitespace

"  language you're most likely to be familiar with; a SQL query describes" Text
'\n'          Text.Whitespace

'  the data set you want to retrieve, and the SQL engine decides whether to ' Text
'\n'          Text.Whitespace

'  scan tables or use indexes, which subclauses should be performed first,' Text
'\n'          Text.Whitespace

'  etc.'      Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' '           Text
'**Object-oriented**' Generic.Strong
' programs manipulate  collections of objects.' Text
'\n'          Text.Whitespace

'  Objects have internal state and support methods that query or modify' Text
'\n'          Text.Whitespace

'  this internal state in some way. Smalltalk and Java are' Text
'\n'          Text.Whitespace

'  object-oriented languages.  C++ and Python are languages that' Text
'\n'          Text.Whitespace

"  support object-oriented programming, but don't force the use " Text
'\n'          Text.Whitespace

'  of object-oriented features.' Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' '           Text
'**Functional**' Generic.Strong
' programming decomposes a problem into a set of functions.' Text
'\n'          Text.Whitespace

"  Ideally, functions only take inputs and produce outputs, and don't have any " Text
'\n'          Text.Whitespace

'  internal state that affects the output produced for a given input.' Text
'\n'          Text.Whitespace

'  Well-known functional languages include the ML family (Standard ML,' Text
'\n'          Text.Whitespace

'  OCaml, and other variants) and Haskell.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The designers of some computer languages have chosen one approach to ' Text
'\n'          Text.Whitespace

"programming that's emphasized.  This often makes it difficult to" Text
'\n'          Text.Whitespace

'write programs that use a different approach.  Other languages are' Text
'\n'          Text.Whitespace

'multi-paradigm languages that support several different approaches.  Lisp,' Text
'\n'          Text.Whitespace

'C++, and Python are multi-paradigm; you can write programs or' Text
'\n'          Text.Whitespace

'libraries that are largely procedural, object-oriented, or functional' Text
'\n'          Text.Whitespace

'in all of these languages.  In a large program, different sections' Text
'\n'          Text.Whitespace

'might be written using different approaches; the GUI might be object-oriented' Text
'\n'          Text.Whitespace

'while the processing logic is procedural or functional, for example.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'In a functional program, input flows through a set of functions. Each' Text
'\n'          Text.Whitespace

'function operates on its input and produces some output.  Functional' Text
'\n'          Text.Whitespace

'style frowns upon functions with side effects that modify internal' Text
'\n'          Text.Whitespace

"state or make other changes that aren't visible in the function's" Text
'\n'          Text.Whitespace

'return value.  Functions that have no side effects at all are ' Text
'\n'          Text.Whitespace

'called '     Text
'**purely functional**' Generic.Strong
'.'           Text
'\n'          Text.Whitespace

'Avoiding side effects means not using data structures' Text
'\n'          Text.Whitespace

"that get updated as a program runs; every function's output " Text
'\n'          Text.Whitespace

'must only depend on its input.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Some languages are very strict about purity and don't even have" Text
'\n'          Text.Whitespace

'assignment statements such as ' Text
'``'          Literal.String
'a=3'         Literal.String
'``'          Literal.String
' or '        Text
'``'          Literal.String
'c = a + b'   Literal.String
'``'          Literal.String
", but it's"  Text
'\n'          Text.Whitespace

'difficult to avoid all side effects.  Printing to the screen or' Text
'\n'          Text.Whitespace

'writing to a disk file are side effects, for example.  For example, in' Text
'\n'          Text.Whitespace

'Python a '   Text
'``'          Literal.String
'print'       Literal.String
'``'          Literal.String
' statement or a ' Text
'``'          Literal.String
'time.sleep(1)' Literal.String
'``'          Literal.String
' both return no' Text
'\n'          Text.Whitespace

"useful value; they're only called for their side effects of sending" Text
'\n'          Text.Whitespace

'some text to the screen or pausing execution for a second.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Python programs written in functional style usually won't go to the" Text
'\n'          Text.Whitespace

"extreme of avoiding all I/O or all assignments; instead, they'll" Text
'\n'          Text.Whitespace

'provide a functional-appearing interface but will use non-functional' Text
'\n'          Text.Whitespace

'features internally.  For example, the implementation of a function' Text
'\n'          Text.Whitespace

"will still use assignments to local variables, but won't modify global" Text
'\n'          Text.Whitespace

'variables or have other side effects.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Functional programming can be considered the opposite of' Text
'\n'          Text.Whitespace

'object-oriented programming.  Objects are little capsules containing' Text
'\n'          Text.Whitespace

'some internal state along with a collection of method calls that let' Text
'\n'          Text.Whitespace

'you modify this state, and programs consist of making the right set of' Text
'\n'          Text.Whitespace

'state changes.  Functional programming wants to avoid state changes as' Text
'\n'          Text.Whitespace

'much as possible and works with data flowing between functions.  In' Text
'\n'          Text.Whitespace

'Python you might combine the two approaches by writing functions that' Text
'\n'          Text.Whitespace

'take and return instances representing objects in your application' Text
'\n'          Text.Whitespace

'(e-mail messages, transactions, etc.).' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Functional design may seem like an odd constraint to work under.  Why' Text
'\n'          Text.Whitespace

'should you avoid objects and side effects?  There are theoretical and' Text
'\n'          Text.Whitespace

'practical advantages to the functional style' Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' Formal provability.' Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Modularity.' Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Composability.' Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Ease of debugging and testing.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Formal provability' Generic.Heading
'\n'          Text

"''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"A theoretical benefit is that it's easier to construct a mathematical proof" Text
'\n'          Text.Whitespace

'that a functional program is correct.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'For a long time researchers have been interested in finding ways to' Text
'\n'          Text.Whitespace

'mathematically prove programs correct.  This is different from testing' Text
'\n'          Text.Whitespace

'a program on numerous inputs and concluding that its output is usually' Text
'\n'          Text.Whitespace

"correct, or reading a program's source code and concluding that the" Text
'\n'          Text.Whitespace

'code looks right; the goal is instead a rigorous proof that a program' Text
'\n'          Text.Whitespace

'produces the right result for all possible inputs.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The technique used to prove programs correct is to write down ' Text
'\n'          Text.Whitespace

'**invariants**' Generic.Strong
", properties of the input data and of the program's " Text
'\n'          Text.Whitespace

'variables that are always true.  For each line of code, you then show ' Text
'\n'          Text.Whitespace

'that if invariants X and Y are true ' Text
'**before**'  Generic.Strong
' the line is executed, ' Text
'\n'          Text.Whitespace

"the slightly different invariants X' and Y' are true " Text
'**after**'   Generic.Strong
'\n'          Text.Whitespace

'the line is executed.  This continues until you reach the end of the' Text
'\n'          Text.Whitespace

'program, at which point the invariants should match the desired ' Text
'\n'          Text.Whitespace

"conditions on the program's output." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Functional programming's avoidance of assignments arose because " Text
'\n'          Text.Whitespace

'assignments are difficult to handle with this technique; ' Text
'\n'          Text.Whitespace

'assignments can break invariants that were true before the assignment' Text
'\n'          Text.Whitespace

'without producing any new invariants that can be propagated onward.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Unfortunately, proving programs correct is largely impractical and not' Text
'\n'          Text.Whitespace

'relevant to Python software. Even trivial programs require proofs that' Text
'\n'          Text.Whitespace

'are several pages long; the proof of correctness for a moderately' Text
'\n'          Text.Whitespace

'complicated program would be enormous, and few or none of the programs' Text
'\n'          Text.Whitespace

'you use daily (the Python interpreter, your XML parser, your web' Text
'\n'          Text.Whitespace

'browser) could be proven correct.  Even if you wrote down or generated' Text
'\n'          Text.Whitespace

'a proof, there would then be the question of verifying the proof;' Text
'\n'          Text.Whitespace

"maybe there's an error in it, and you wrongly believe you've proved" Text
'\n'          Text.Whitespace

'the program correct.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Modularity'  Generic.Heading
'\n'          Text

"''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'A more practical benefit of functional programming is that it forces' Text
'\n'          Text.Whitespace

'you to break apart your problem into small pieces.  Programs are more' Text
'\n'          Text.Whitespace

"modular as a result.  It's easier to specify and write a small" Text
'\n'          Text.Whitespace

'function that does one thing than a large function that performs a' Text
'\n'          Text.Whitespace

'complicated transformation.  Small functions are also easier to read' Text
'\n'          Text.Whitespace

'and to check for errors.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Ease of debugging and testing ' Generic.Heading
'\n'          Text

"''''''''''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'Testing and debugging a functional-style program is easier.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Debugging is simplified because functions are generally small and' Text
'\n'          Text.Whitespace

"clearly specified.  When a program doesn't work, each function is an" Text
'\n'          Text.Whitespace

'interface point where you can check that the data are correct.  You' Text
'\n'          Text.Whitespace

'can look at the intermediate inputs and outputs to quickly isolate the' Text
'\n'          Text.Whitespace

"function that's responsible for a bug." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Testing is easier because each function is a potential subject for a' Text
'\n'          Text.Whitespace

"unit test.  Functions don't depend on system state that needs to be" Text
'\n'          Text.Whitespace

'replicated before running a test; instead you only have to synthesize' Text
'\n'          Text.Whitespace

'the right input and then check that the output matches expectations.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Composability' Generic.Heading
'\n'          Text

"''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"As you work on a functional-style program, you'll write a number of" Text
'\n'          Text.Whitespace

'functions with varying inputs and outputs.  Some of these functions' Text
'\n'          Text.Whitespace

'will be unavoidably specialized to a particular application, but' Text
'\n'          Text.Whitespace

'others will be useful in a wide variety of programs.  For example, a' Text
'\n'          Text.Whitespace

'function that takes a directory path and returns all the XML files in' Text
'\n'          Text.Whitespace

'the directory, or a function that takes a filename and returns its' Text
'\n'          Text.Whitespace

'contents, can be applied to many different situations.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Over time you'll form a personal library of utilities.  Often you'll" Text
'\n'          Text.Whitespace

'assemble new programs by arranging existing functions in a new' Text
'\n'          Text.Whitespace

'configuration and writing a few functions specialized for the current' Text
'\n'          Text.Whitespace

'task.'       Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Iterators'   Generic.Heading
'\n'          Text

'-----------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"I'll start by looking at a Python language feature that's an important" Text
'\n'          Text.Whitespace

'foundation for writing functional-style programs' Text
':'           Text
' iterators.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'An iterator is an object representing a stream of data; this object' Text
'\n'          Text.Whitespace

'returns the data one element at a time.  A Python iterator must' Text
'\n'          Text.Whitespace

'support a method called ' Text
'``'          Literal.String
'next()'      Literal.String
'``'          Literal.String
' that takes no arguments and always' Text
'\n'          Text.Whitespace

'returns the next element of the stream.  If there are no more elements' Text
'\n'          Text.Whitespace

'in the stream, ' Text
'``'          Literal.String
'next()'      Literal.String
'``'          Literal.String
' must raise the ' Text
'``'          Literal.String
'StopIteration' Literal.String
'``'          Literal.String
' exception.' Text
'\n'          Text.Whitespace

"Iterators don't have to be finite, though; it's perfectly reasonable" Text
'\n'          Text.Whitespace

'to write an iterator that produces an infinite stream of data.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The built-in ' Text
'``'          Literal.String
'iter()'      Literal.String
'``'          Literal.String
' function takes an arbitrary object and tries' Text
'\n'          Text.Whitespace

"to return an iterator that will return the object's contents or" Text
'\n'          Text.Whitespace

'elements, raising ' Text
'``'          Literal.String
'TypeError'   Literal.String
'``'          Literal.String
" if the object doesn't support" Text
'\n'          Text.Whitespace

"iteration.  Several of Python's built-in data types support iteration," Text
'\n'          Text.Whitespace

'the most common being lists and dictionaries.  An object is called ' Text
'\n'          Text.Whitespace

'an '         Text
'**iterable**' Generic.Strong
' object if you can get an iterator for it.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'You can experiment with the iteration interface manually' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'>>> L = [1,2,3]' Literal.String
'\n'          Text

'    >>> it = iter(L)\n    >>> print it\n    <iterator object at 0x8116870>\n    >>> it.next()\n    1\n    >>> it.next()\n    2\n    >>> it.next()\n    3\n    >>> it.next()\n    Traceback (most recent call last):\n      File "<stdin>", line 1, in ?\n    StopIteration\n    >>>      \n\n' Literal.String

'Python expects iterable objects in several different contexts, the ' Text
'\n'          Text.Whitespace

'most important being the ' Text
'``'          Literal.String
'for'         Literal.String
'``'          Literal.String
' statement.  In the statement ' Text
'``'          Literal.String
'for X in Y'  Literal.String
'``'          Literal.String
','           Text
'\n'          Text.Whitespace

'Y must be an iterator or some object for which ' Text
'``'          Literal.String
'iter()'      Literal.String
'``'          Literal.String
' can create ' Text
'\n'          Text.Whitespace

'an iterator.  These two statements are equivalent' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'for i in iter(obj):' Literal.String
'\n'          Text

'            print i\n\n        for i in obj:\n            print i\n\n' Literal.String

'Iterators can be materialized as lists or tuples by using the' Text
'\n'          Text.Whitespace

'``'          Literal.String
'list()'      Literal.String
'``'          Literal.String
' or '        Text
'``'          Literal.String
'tuple()'     Literal.String
'``'          Literal.String
' constructor functions' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'>>> L = [1,2,3]' Literal.String
'\n'          Text

'    >>> iterator = iter(L)\n    >>> t = tuple(iterator)\n    >>> t\n    (1, 2, 3)\n\n' Literal.String

'Sequence unpacking also supports iterators' Text
':'           Text
' if you know an iterator ' Text
'\n'          Text.Whitespace

'will return N elements, you can unpack them into an N-tuple' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'>>> L = [1,2,3]' Literal.String
'\n'          Text

'    >>> iterator = iter(L)\n    >>> a,b,c = iterator\n    >>> a,b,c\n    (1, 2, 3)\n\n' Literal.String

'Built-in functions such as ' Text
'``'          Literal.String
'max()'       Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'min()'       Literal.String
'``'          Literal.String
' can take a single' Text
'\n'          Text.Whitespace

'iterator argument and will return the largest or smallest element.' Text
'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'"in"'        Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'"not in"'    Literal.String
'``'          Literal.String
' operators also support iterators' Text
':'           Text
' '           Text
'``'          Literal.String
'X in\niterator' Literal.String
'``'          Literal.String
' is true if X is found in the stream returned by the' Text
'\n'          Text.Whitespace

"iterator.  You'll run into obvious problems if the iterator is" Text
'\n'          Text.Whitespace

'infinite; '  Text
'``'          Literal.String
'max()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'min()'       Literal.String
'``'          Literal.String
', and '      Text
'``'          Literal.String
'"not in"'    Literal.String
'``'          Literal.String
' will never return, and' Text
'\n'          Text.Whitespace

'if the element X never appears in the stream, the ' Text
'``'          Literal.String
'"in"'        Literal.String
'``'          Literal.String
' operator'   Text
'\n'          Text.Whitespace

"won't return either." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Note that you can only go forward in an iterator; there's no way to" Text
'\n'          Text.Whitespace

'get the previous element, reset the iterator, or make a copy of it.' Text
'\n'          Text.Whitespace

'Iterator objects can optionally provide these additional capabilities,' Text
'\n'          Text.Whitespace

'but the iterator protocol only specifies the ' Text
'``'          Literal.String
'next()'      Literal.String
'``'          Literal.String
' method.'    Text
'\n'          Text.Whitespace

"Functions may therefore consume all of the iterator's output, and if" Text
'\n'          Text.Whitespace

"you need to do something different with the same stream, you'll have" Text
'\n'          Text.Whitespace

'to create a new iterator.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Data Types That Support Iterators' Generic.Heading
'\n'          Text

"'''''''''''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"We've already seen how lists and tuples support iterators.  In fact," Text
'\n'          Text.Whitespace

'any Python sequence type, such as strings, will automatically support' Text
'\n'          Text.Whitespace

'creation of an iterator.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Calling '    Text
'``'          Literal.String
'iter()'      Literal.String
'``'          Literal.String
' on a dictionary returns an iterator that will loop' Text
'\n'          Text.Whitespace

"over the dictionary's keys" Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
">>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6," Literal.String
'\n'          Text

"    ...      'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}\n    >>> for key in m:\n    ...     print key, m[key]\n    Mar 3\n    Feb 2\n    Aug 8\n    Sep 9\n    May 5\n    Jun 6\n    Jul 7\n    Jan 1\n    Apr 4\n    Nov 11\n    Dec 12\n    Oct 10\n\n" Literal.String

"Note that the order is essentially random, because it's based on the" Text
'\n'          Text.Whitespace

'hash ordering of the objects in the dictionary.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Applying '   Text
'``'          Literal.String
'iter()'      Literal.String
'``'          Literal.String
' to a dictionary always loops over the keys, but' Text
'\n'          Text.Whitespace

'dictionaries have methods that return other iterators.  If you want to' Text
'\n'          Text.Whitespace

'iterate over keys, values, or key/value pairs, you can explicitly call' Text
'\n'          Text.Whitespace

'the '        Text
'``'          Literal.String
'iterkeys()'  Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'itervalues()' Literal.String
'``'          Literal.String
', or '       Text
'``'          Literal.String
'iteritems()' Literal.String
'``'          Literal.String
' methods to' Text
'\n'          Text.Whitespace

'get an appropriate iterator.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'dict()'      Literal.String
'``'          Literal.String
' constructor can accept an iterator that returns a' Text
'\n'          Text.Whitespace

'finite stream of ' Text
'``'          Literal.String
'(key, value)' Literal.String
'``'          Literal.String
' tuples'     Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
">>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')]" Literal.String
'\n'          Text

"    >>> dict(iter(L))\n    {'Italy': 'Rome', 'US': 'Washington DC', 'France': 'Paris'}\n\n" Literal.String

'Files also support iteration by calling the ' Text
'``'          Literal.String
'readline()'  Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'method until there are no more lines in the file.  This means you can' Text
'\n'          Text.Whitespace

'read each line of a file like this' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'for line in file:' Literal.String
'\n'          Text

'        # do something for each line\n        ...\n\n' Literal.String

'Sets can take their contents from an iterable and let you iterate over' Text
'\n'          Text.Whitespace

"the set's elements" Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'S = set((2, 3, 5, 7, 11, 13))' Literal.String
'\n'          Text

'    for i in S:\n        print i\n\n\n\n' Literal.String

'Generator expressions and list comprehensions' Generic.Heading
'\n'          Text

'----------------------------------------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"Two common operations on an iterator's output are 1) performing some" Text
'\n'          Text.Whitespace

'operation for every element, 2) selecting a subset of elements that' Text
'\n'          Text.Whitespace

'meet some condition.  For example, given a list of strings, you might' Text
'\n'          Text.Whitespace

'want to strip off trailing whitespace from each line or extract all' Text
'\n'          Text.Whitespace

'the strings containing a given substring.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'List comprehensions and generator expressions (short form' Text
':'           Text
' "listcomps"' Text
'\n'          Text.Whitespace

'and "genexps") are a concise notation for such operations, borrowed' Text
'\n'          Text.Whitespace

'from the functional programming language Haskell' Text
'\n'          Text.Whitespace

'(http'       Text
':'           Text
'//www.haskell.org).  You can strip all the whitespace from a' Text
'\n'          Text.Whitespace

'stream of strings with the following code' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
"line_list = ['  line 1\\n', 'line 2  \\n', ...]" Literal.String
'\n'          Text

'\n        # Generator expression -- returns iterator\n        stripped_iter = (line.strip() for line in line_list)\n\n        # List comprehension -- returns list\n        stripped_list = [line.strip() for line in line_list]\n\n' Literal.String

'You can select only certain elements by adding an ' Text
'``'          Literal.String
'"if"'        Literal.String
'``'          Literal.String
' condition'  Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'stripped_list = [line.strip() for line in line_list' Literal.String
'\n'          Text

'                         if line != ""]\n\n' Literal.String

'With a list comprehension, you get back a Python list;' Text
'\n'          Text.Whitespace

'``'          Literal.String
'stripped_list' Literal.String
'``'          Literal.String
' is a list containing the resulting lines, not an' Text
'\n'          Text.Whitespace

'iterator.  Generator expressions return an iterator that computes the' Text
'\n'          Text.Whitespace

'values as necessary, not needing to materialize all the values at' Text
'\n'          Text.Whitespace

"once.  This means that list comprehensions aren't useful if you're" Text
'\n'          Text.Whitespace

'working with iterators that return an infinite stream or a very large' Text
'\n'          Text.Whitespace

'amount of data.  Generator expressions are preferable in these' Text
'\n'          Text.Whitespace

'situations.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Generator expressions are surrounded by parentheses ("()") and list' Text
'\n'          Text.Whitespace

'comprehensions are surrounded by square brackets ("' Text
'['           Text
']").  Generator' Text
'\n'          Text.Whitespace

'expressions have the form' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'( expression for expr in sequence1 ' Literal.String
'\n'          Text

'                 if condition1\n                 for expr2 in sequence2\n                 if condition2\n                 for expr3 in sequence3 ...\n                 if condition3\n                 for exprN in sequenceN\n                 if conditionN )\n\n' Literal.String

'Again, for a list comprehension only the outside brackets are' Text
'\n'          Text.Whitespace

'different (square brackets instead of parentheses).' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The elements of the generated output will be the successive values of' Text
'\n'          Text.Whitespace

'``'          Literal.String
'expression'  Literal.String
'``'          Literal.String
'.  The '     Text
'``'          Literal.String
'if'          Literal.String
'``'          Literal.String
' clauses are all optional; if present,' Text
'\n'          Text.Whitespace

'``'          Literal.String
'expression'  Literal.String
'``'          Literal.String
' is only evaluated and added to the result when' Text
'\n'          Text.Whitespace

'``'          Literal.String
'condition'   Literal.String
'``'          Literal.String
' is true.'   Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Generator expressions always have to be written inside parentheses,' Text
'\n'          Text.Whitespace

'but the parentheses signalling a function call also count.  If you' Text
'\n'          Text.Whitespace

'want to create an iterator that will be immediately passed to a' Text
'\n'          Text.Whitespace

'function you can write' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'obj_total = sum(obj.count for obj in list_all_objects())' Literal.String
'\n'          Text

'\n'          Literal.String

'The '        Text
'``'          Literal.String
'for...in'    Literal.String
'``'          Literal.String
' clauses contain the sequences to be iterated over.' Text
'\n'          Text.Whitespace

'The sequences do not have to be the same length, because they are' Text
'\n'          Text.Whitespace

'iterated over from left to right, ' Text
'**not**'     Generic.Strong
' in parallel.  For each' Text
'\n'          Text.Whitespace

'element in ' Text
'``'          Literal.String
'sequence1'   Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'sequence2'   Literal.String
'``'          Literal.String
' is looped over from the' Text
'\n'          Text.Whitespace

'beginning.  ' Text
'``'          Literal.String
'sequence3'   Literal.String
'``'          Literal.String
'  is then looped over for each ' Text
'\n'          Text.Whitespace

'resulting pair of elements from ' Text
'``'          Literal.String
'sequence1'   Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'sequence2'   Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'To put it another way, a list comprehension or generator expression is' Text
'\n'          Text.Whitespace

'equivalent to the following Python code' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'for expr1 in sequence1:' Literal.String
'\n'          Text

'        if not (condition1):\n            continue   # Skip this element\n        for expr2 in sequence2:\n            if not (condition2):\n                continue    # Skip this element\n            ...\n            for exprN in sequenceN:\n                 if not (conditionN):\n                     continue   # Skip this element\n\n                 # Output the value of \n                 # the expression.\n\n' Literal.String

'This means that when there are multiple ' Text
'``'          Literal.String
'for...in'    Literal.String
'``'          Literal.String
' clauses but no' Text
'\n'          Text.Whitespace

'``'          Literal.String
'if'          Literal.String
'``'          Literal.String
' clauses, the length of the resulting output will be equal to' Text
'\n'          Text.Whitespace

'the product of the lengths of all the sequences.  If you have two' Text
'\n'          Text.Whitespace

'lists of length 3, the output list is 9 elements long' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"seq1 = 'abc'" Literal.String
'\n'          Text

"    seq2 = (1,2,3)\n    >>> [ (x,y) for x in seq1 for y in seq2]\n    [('a', 1), ('a', 2), ('a', 3), \n     ('b', 1), ('b', 2), ('b', 3), \n     ('c', 1), ('c', 2), ('c', 3)]\n\n" Literal.String

"To avoid introducing an ambiguity into Python's grammar, if" Text
'\n'          Text.Whitespace

'``'          Literal.String
'expression'  Literal.String
'``'          Literal.String
' is creating a tuple, it must be surrounded with' Text
'\n'          Text.Whitespace

'parentheses.  The first list comprehension below is a syntax error,' Text
'\n'          Text.Whitespace

'while the second one is correct' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'# Syntax error' Literal.String
'\n'          Text

'    [ x,y for x in seq1 for y in seq2]\n    # Correct\n    [ (x,y) for x in seq1 for y in seq2]\n\n\n' Literal.String

'Generators'  Generic.Heading
'\n'          Text

'-----------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'Generators are a special class of functions that simplify the task of' Text
'\n'          Text.Whitespace

'writing iterators.  Regular functions compute a value and return it,' Text
'\n'          Text.Whitespace

'but generators return an iterator that returns a stream of values.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"You're doubtless familiar with how regular function calls work in" Text
'\n'          Text.Whitespace

'Python or C.  When you call a function, it gets a private namespace' Text
'\n'          Text.Whitespace

'where its local variables are created.  When the function reaches a' Text
'\n'          Text.Whitespace

'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
' statement, the local variables are destroyed and the' Text
'\n'          Text.Whitespace

'value is returned to the caller.  A later call to the same function' Text
'\n'          Text.Whitespace

'creates a new private namespace and a fresh set of local' Text
'\n'          Text.Whitespace

"variables. But, what if the local variables weren't thrown away on" Text
'\n'          Text.Whitespace

'exiting a function?  What if you could later resume the function where' Text
'\n'          Text.Whitespace

'it left off?  This is what generators provide; they can be thought of' Text
'\n'          Text.Whitespace

'as resumable functions.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Here's the simplest example of a generator function" Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def generate_ints(N):' Literal.String
'\n'          Text

'        for i in range(N):\n            yield i\n\n' Literal.String

'Any function containing a ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' keyword is a generator function;' Text
'\n'          Text.Whitespace

"this is detected by Python's bytecode compiler which compiles the" Text
'\n'          Text.Whitespace

'function specially as a result.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"When you call a generator function, it doesn't return a single value;" Text
'\n'          Text.Whitespace

'instead it returns a generator object that supports the iterator' Text
'\n'          Text.Whitespace

'protocol.  On executing the ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' expression, the generator' Text
'\n'          Text.Whitespace

'outputs the value of ' Text
'``'          Literal.String
'i'           Literal.String
'``'          Literal.String
', similar to a ' Text
'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'statement.  The big difference between ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' and a'      Text
'\n'          Text.Whitespace

'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
' statement is that on reaching a ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' the'        Text
'\n'          Text.Whitespace

"generator's state of execution is suspended and local variables are" Text
'\n'          Text.Whitespace

"preserved.  On the next call to the generator's " Text
'``'          Literal.String
'.next()'     Literal.String
'``'          Literal.String
' method,'    Text
'\n'          Text.Whitespace

'the function will resume executing.  ' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Here's a sample usage of the " Text
'``'          Literal.String
'generate_ints()' Literal.String
'``'          Literal.String
' generator'  Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'>>> gen = generate_ints(3)' Literal.String
'\n'          Text

'    >>> gen\n    <generator object at 0x8117f90>\n    >>> gen.next()\n    0\n    >>> gen.next()\n    1\n    >>> gen.next()\n    2\n    >>> gen.next()\n    Traceback (most recent call last):\n      File "stdin", line 1, in ?\n      File "stdin", line 2, in generate_ints\n    StopIteration\n\n' Literal.String

'You could equally write ' Text
'``'          Literal.String
'for i in generate_ints(5)' Literal.String
'``'          Literal.String
', or'        Text
'\n'          Text.Whitespace

'``'          Literal.String
'a,b,c = generate_ints(3)' Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Inside a generator function, the ' Text
'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
' statement can only be used' Text
'\n'          Text.Whitespace

'without a value, and signals the end of the procession of values;' Text
'\n'          Text.Whitespace

'after executing a ' Text
'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
' the generator cannot return any further' Text
'\n'          Text.Whitespace

'values.  '   Text
'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
' with a value, such as ' Text
'``'          Literal.String
'return 5'    Literal.String
'``'          Literal.String
', is a syntax' Text
'\n'          Text.Whitespace

"error inside a generator function.  The end of the generator's results" Text
'\n'          Text.Whitespace

'can also be indicated by raising ' Text
'``'          Literal.String
'StopIteration' Literal.String
'``'          Literal.String
' manually, or by' Text
'\n'          Text.Whitespace

'just letting the flow of execution fall off the bottom of the' Text
'\n'          Text.Whitespace

'function.'   Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'You could achieve the effect of generators manually by writing your' Text
'\n'          Text.Whitespace

'own class and storing all the local variables of the generator as' Text
'\n'          Text.Whitespace

'instance variables.  For example, returning a list of integers could' Text
'\n'          Text.Whitespace

'be done by setting ' Text
'``'          Literal.String
'self.count'  Literal.String
'``'          Literal.String
' to 0, and having the' Text
'\n'          Text.Whitespace

'``'          Literal.String
'next()'      Literal.String
'``'          Literal.String
' method increment ' Text
'``'          Literal.String
'self.count'  Literal.String
'``'          Literal.String
' and return it.' Text
'\n'          Text.Whitespace

'However, for a moderately complicated generator, writing a' Text
'\n'          Text.Whitespace

'corresponding class can be much messier.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"The test suite included with Python's library, " Text
'``'          Literal.String
'test_generators.py' Literal.String
'``'          Literal.String
','           Text
'\n'          Text.Whitespace

"contains a number of more interesting examples.  Here's one generator" Text
'\n'          Text.Whitespace

'that implements an in-order traversal of a tree using generators' Text
'\n'          Text.Whitespace

'recursively.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'# A recursive generator that generates Tree leaves in in-order.' Literal.String
'\n'          Text

'    def inorder(t):\n        if t:\n            for x in inorder(t.left):\n                yield x\n\n            yield t.label\n\n            for x in inorder(t.right):\n                yield x\n\n' Literal.String

'Two other examples in ' Text
'``'          Literal.String
'test_generators.py' Literal.String
'``'          Literal.String
' produce'    Text
'\n'          Text.Whitespace

'solutions for the N-Queens problem (placing N queens on an NxN' Text
'\n'          Text.Whitespace

"chess board so that no queen threatens another) and the Knight's Tour" Text
'\n'          Text.Whitespace

'(finding a route that takes a knight to every square of an NxN chessboard' Text
'\n'          Text.Whitespace

'without visiting any square twice).' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Passing values into a generator' Generic.Heading
'\n'          Text

"''''''''''''''''''''''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'In Python 2.4 and earlier, generators only produced output.  Once a' Text
'\n'          Text.Whitespace

"generator's code was invoked to create an iterator, there was no way to" Text
'\n'          Text.Whitespace

'pass any new information into the function when its execution is' Text
'\n'          Text.Whitespace

'resumed.  You could hack together this ability by making the' Text
'\n'          Text.Whitespace

'generator look at a global variable or by passing in some mutable object' Text
'\n'          Text.Whitespace

'that callers then modify, but these approaches are messy.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"In Python 2.5 there's a simple way to pass values into a generator." Text
'\n'          Text.Whitespace

'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' became an expression, returning a value that can be assigned' Text
'\n'          Text.Whitespace

'to a variable or otherwise operated on' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'val = (yield i)' Literal.String
'\n'          Text

'\n'          Literal.String

'I recommend that you ' Text
'**always**'  Generic.Strong
' put parentheses around a ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

"expression when you're doing something with the returned value, as in" Text
'\n'          Text.Whitespace

"the above example.  The parentheses aren't always necessary, but it's" Text
'\n'          Text.Whitespace

"easier to always add them instead of having to remember when they're" Text
'\n'          Text.Whitespace

'needed.'     Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'(PEP 342 explains the exact rules, which are that a' Text
'\n'          Text.Whitespace

'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
'-expression must always be parenthesized except when it' Text
'\n'          Text.Whitespace

'occurs at the top-level expression on the right-hand side of an' Text
'\n'          Text.Whitespace

'assignment.  This means you can write ' Text
'``'          Literal.String
'val = yield i' Literal.String
'``'          Literal.String
' but have to' Text
'\n'          Text.Whitespace

"use parentheses when there's an operation, as in " Text
'``'          Literal.String
'val = (yield i)\n+ 12' Literal.String
'``'          Literal.String
'.)'          Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Values are sent into a generator by calling its' Text
'\n'          Text.Whitespace

'``'          Literal.String
'send(value)' Literal.String
'``'          Literal.String
' method.  This method resumes the ' Text
'\n'          Text.Whitespace

"generator's code and the " Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' expression returns the specified' Text
'\n'          Text.Whitespace

'value.  If the regular ' Text
'``'          Literal.String
'next()'      Literal.String
'``'          Literal.String
' method is called, the' Text
'\n'          Text.Whitespace

'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' returns '   Text
'``'          Literal.String
'None'        Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Here's a simple counter that increments by 1 and allows changing the" Text
'\n'          Text.Whitespace

'value of the internal counter.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def counter (maximum):' Literal.String
'\n'          Text

'        i = 0\n        while i < maximum:\n            val = (yield i)\n            # If value provided, change counter\n            if val is not None:\n                i = val\n            else:\n                i += 1\n\n' Literal.String

"And here's an example of changing the counter" Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'    >>> it = counter(10)' Text
'\n'          Text.Whitespace

'    >>> print it.next()' Text
'\n'          Text.Whitespace

'    0'       Text
'\n'          Text.Whitespace

'    >>> print it.next()' Text
'\n'          Text.Whitespace

'    1'       Text
'\n'          Text.Whitespace

'    >>> print it.send(8)' Text
'\n'          Text.Whitespace

'    8'       Text
'\n'          Text.Whitespace

'    >>> print it.next()' Text
'\n'          Text.Whitespace

'    9'       Text
'\n'          Text.Whitespace

'    >>> print it.next()' Text
'\n'          Text.Whitespace

'    Traceback (most recent call last)' Text
':'           Text
'\n'          Text.Whitespace

'      File ' Text
'``'          Literal.String
"t.py'', line 15, in ?\n        print it.next()\n    StopIteration\n\nBecause " Literal.String
'`'           Literal.String
'`'           Literal.String
'yield'       Literal.String
'``'          Literal.String
' will often be returning ' Text
'``'          Literal.String
'None'        Literal.String
'``'          Literal.String
', you'       Text
'\n'          Text.Whitespace

"should always check for this case.  Don't just use its value in" Text
'\n'          Text.Whitespace

"expressions unless you're sure that the " Text
'``'          Literal.String
'send()'      Literal.String
'``'          Literal.String
' method'     Text
'\n'          Text.Whitespace

'will be the only method used resume your generator function.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'In addition to ' Text
'``'          Literal.String
'send()'      Literal.String
'``'          Literal.String
', there are two other new methods on' Text
'\n'          Text.Whitespace

'generators'  Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' '           Text
'``'          Literal.String
'throw(type, value=None, traceback=None)' Literal.String
'``'          Literal.String
' is used to raise an exception inside the' Text
'\n'          Text.Whitespace

'  generator; the exception is raised by the ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' expression' Text
'\n'          Text.Whitespace

"  where the generator's execution is paused." Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' '           Text
'``'          Literal.String
'close()'     Literal.String
'``'          Literal.String
' raises a '  Text
'``'          Literal.String
'GeneratorExit' Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'  exception inside the generator to terminate the iteration.  ' Text
'\n'          Text.Whitespace

'  On receiving this' Text
'\n'          Text.Whitespace

"  exception, the generator's code must either raise" Text
'\n'          Text.Whitespace

'  '          Text
'``'          Literal.String
'GeneratorExit' Literal.String
'``'          Literal.String
' or '        Text
'``'          Literal.String
'StopIteration' Literal.String
'``'          Literal.String
'; catching the ' Text
'\n'          Text.Whitespace

'  exception and doing anything else is illegal and will trigger' Text
'\n'          Text.Whitespace

'  a '        Text
'``'          Literal.String
'RuntimeError' Literal.String
'``'          Literal.String
'.  '         Text
'``'          Literal.String
'close()'     Literal.String
'``'          Literal.String
' will also be called by ' Text
'\n'          Text.Whitespace

"  Python's garbage collector when the generator is garbage-collected." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'  If you need to run cleanup code when a ' Text
'``'          Literal.String
'GeneratorExit' Literal.String
'``'          Literal.String
' occurs,'    Text
'\n'          Text.Whitespace

'  I suggest using a ' Text
'``'          Literal.String
'try: ... finally:' Literal.String
'``'          Literal.String
' suite instead of ' Text
'\n'          Text.Whitespace

'  catching ' Text
'``'          Literal.String
'GeneratorExit' Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The cumulative effect of these changes is to turn generators from' Text
'\n'          Text.Whitespace

'one-way producers of information into both producers and consumers.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Generators also become ' Text
'**coroutines**' Generic.Strong
', a more generalized form of' Text
'\n'          Text.Whitespace

'subroutines.  Subroutines are entered at one point and exited at' Text
'\n'          Text.Whitespace

'another point (the top of the function, and a ' Text
'``'          Literal.String
'return'      Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'statement), but coroutines can be entered, exited, and resumed at' Text
'\n'          Text.Whitespace

'many different points (the ' Text
'``'          Literal.String
'yield'       Literal.String
'``'          Literal.String
' statements).  ' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Built-in functions' Generic.Heading
'\n'          Text

'----------------------------------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"Let's look in more detail at built-in functions often used with iterators." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Two Python's built-in functions, " Text
'``'          Literal.String
'map()'       Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'filter()'    Literal.String
'``'          Literal.String
', are'       Text
'\n'          Text.Whitespace

'somewhat obsolete; they duplicate the features of list comprehensions' Text
'\n'          Text.Whitespace

'but return actual lists instead of iterators.  ' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'map(f, iterA, iterB, ...)' Literal.String
'``'          Literal.String
' returns a list containing ' Text
'``'          Literal.String
'f(iterA[0],\niterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...' Literal.String
'``'          Literal.String
'.  '         Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def upper(s):' Literal.String
'\n'          Text

"        return s.upper()\n    map(upper, ['sentence', 'fragment']) =>\n      ['SENTENCE', 'FRAGMENT']\n\n    [upper(s) for s in ['sentence', 'fragment']] =>\n      ['SENTENCE', 'FRAGMENT']\n\n" Literal.String

'As shown above, you can achieve the same effect with a list' Text
'\n'          Text.Whitespace

'comprehension.  The ' Text
'``'          Literal.String
'itertools.imap()' Literal.String
'``'          Literal.String
' function does the same thing' Text
'\n'          Text.Whitespace

"but can handle infinite iterators; it'll be discussed later, in the section on " Text
'\n'          Text.Whitespace

'the '        Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' module.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'filter(predicate, iter)' Literal.String
'``'          Literal.String
' returns a list ' Text
'\n'          Text.Whitespace

'that contains all the sequence elements that meet a certain condition,' Text
'\n'          Text.Whitespace

'and is similarly duplicated by list comprehensions.' Text
'\n'          Text.Whitespace

'A '          Text
'**predicate**' Generic.Strong
' is a function that returns the truth value of' Text
'\n'          Text.Whitespace

'some condition; for use with ' Text
'``'          Literal.String
'filter()'    Literal.String
'``'          Literal.String
', the predicate must take a ' Text
'\n'          Text.Whitespace

'single value.  ' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def is_even(x):' Literal.String
'\n'          Text

'        return (x % 2) == 0\n\n    filter(is_even, range(10)) =>\n      [0, 2, 4, 6, 8]\n\n' Literal.String

'This can also be written as a list comprehension' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'>>> [x for x in range(10) if is_even(x)]' Literal.String
'\n'          Text

'    [0, 2, 4, 6, 8]\n\n' Literal.String

'``'          Literal.String
'filter()'    Literal.String
'``'          Literal.String
' also has a counterpart in the ' Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' module,'    Text
'\n'          Text.Whitespace

'``'          Literal.String
'itertools.ifilter()' Literal.String
'``'          Literal.String
', that returns an iterator and ' Text
'\n'          Text.Whitespace

'can therefore handle infinite sequences just as ' Text
'``'          Literal.String
'itertools.imap()' Literal.String
'``'          Literal.String
' can.'       Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'reduce(func, iter, [initial_value])' Literal.String
'``'          Literal.String
" doesn't have a counterpart in" Text
'\n'          Text.Whitespace

'the '        Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' module because it cumulatively performs an operation' Text
'\n'          Text.Whitespace

"on all the iterable's elements and therefore can't be applied to" Text
'\n'          Text.Whitespace

'infinite iterables.  ' Text
'``'          Literal.String
'func'        Literal.String
'``'          Literal.String
' must be a function that takes two elements' Text
'\n'          Text.Whitespace

'and returns a single value.  ' Text
'``'          Literal.String
'reduce()'    Literal.String
'``'          Literal.String
' takes the first two elements' Text
'\n'          Text.Whitespace

'A and B returned by the iterator and calculates ' Text
'``'          Literal.String
'func(A, B)'  Literal.String
'``'          Literal.String
'.  It'       Text
'\n'          Text.Whitespace

'then requests the third element, C, calculates ' Text
'``'          Literal.String
'func(func(A, B),\nC)' Literal.String
'``'          Literal.String
', combines this result with the fourth element returned, and' Text
'\n'          Text.Whitespace

'continues until the iterable is exhausted.  If the iterable returns no' Text
'\n'          Text.Whitespace

'values at all, a ' Text
'``'          Literal.String
'TypeError'   Literal.String
'``'          Literal.String
' exception is raised.  If the initial' Text
'\n'          Text.Whitespace

"value is supplied, it's used as a starting point and" Text
'\n'          Text.Whitespace

'``'          Literal.String
'func(initial_value, A)' Literal.String
'``'          Literal.String
' is the first calculation.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'import operator' Literal.String
'\n'          Text

"    reduce(operator.concat, ['A', 'BB', 'C']) =>\n      'ABBC'\n    reduce(operator.concat, []) =>\n      TypeError: reduce() of empty sequence with no initial value\n    reduce(operator.mul, [1,2,3], 1) =>\n      6\n    reduce(operator.mul, [], 1) =>\n      1\n\n" Literal.String

'If you use ' Text
'``'          Literal.String
'operator.add' Literal.String
'``'          Literal.String
' with '      Text
'``'          Literal.String
'reduce()'    Literal.String
'``'          Literal.String
", you'll add up all the " Text
'\n'          Text.Whitespace

"elements of the iterable.  This case is so common that there's a special" Text
'\n'          Text.Whitespace

'built-in called ' Text
'``'          Literal.String
'sum()'       Literal.String
'``'          Literal.String
' to compute it' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'reduce(operator.add, [1,2,3,4], 0) =>' Literal.String
'\n'          Text

'      10\n    sum([1,2,3,4]) =>\n      10\n    sum([]) =>\n      0\n\n' Literal.String

'For many uses of ' Text
'``'          Literal.String
'reduce()'    Literal.String
'``'          Literal.String
', though, it can be clearer to just write' Text
'\n'          Text.Whitespace

'the obvious ' Text
'``'          Literal.String
'for'         Literal.String
'``'          Literal.String
' loop'       Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'# Instead of:' Literal.String
'\n'          Text

'    product = reduce(operator.mul, [1,2,3], 1)\n\n    # You can write:\n    product = 1\n    for i in [1,2,3]:\n        product *= i\n\n\n' Literal.String

'``'          Literal.String
'enumerate(iter)' Literal.String
'``'          Literal.String
' counts off the elements in the iterable, returning' Text
'\n'          Text.Whitespace

'2-tuples containing the count and each element.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"enumerate(['subject', 'verb', 'object']) =>" Literal.String
'\n'          Text

"      (0, 'subject'), (1, 'verb'), (2, 'object')\n\n" Literal.String

'``'          Literal.String
'enumerate()' Literal.String
'``'          Literal.String
' is often used when looping through a list ' Text
'\n'          Text.Whitespace

'and recording the indexes at which certain conditions are met' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"f = open('data.txt', 'r')" Literal.String
'\n'          Text

"    for i, line in enumerate(f):\n        if line.strip() == '':\n            print 'Blank line at line #%i' % i\n\n" Literal.String

'``'          Literal.String
'sorted(iterable, [cmp=None], [key=None], [reverse=False)' Literal.String
'``'          Literal.String
' '           Text
'\n'          Text.Whitespace

'collects all the elements of the iterable into a list, sorts ' Text
'\n'          Text.Whitespace

'the list, and returns the sorted result.  The ' Text
'``'          Literal.String
'cmp'         Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'key'         Literal.String
'``'          Literal.String
', '          Text
'\n'          Text.Whitespace

'and '        Text
'``'          Literal.String
'reverse'     Literal.String
'``'          Literal.String
' arguments are passed through to the ' Text
'\n'          Text.Whitespace

"constructed list's " Text
'``'          Literal.String
'.sort()'     Literal.String
'``'          Literal.String
' method.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'import random' Literal.String
'\n'          Text

'    # Generate 8 random numbers between [0, 10000)\n    rand_list = random.sample(range(10000), 8)\n    rand_list =>\n      [769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]\n    sorted(rand_list) =>\n      [769, 2207, 6213, 6431, 7953, 8442, 9828, 9878]\n    sorted(rand_list, reverse=True) =>\n      [9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]\n\n' Literal.String

'(For a more detailed discussion of sorting, see the Sorting mini-HOWTO' Text
'\n'          Text.Whitespace

'in the Python wiki at http' Text
':'           Text
'//wiki.python.org/moin/HowTo/Sorting.)' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'any(iter)'   Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'all(iter)'   Literal.String
'``'          Literal.String
' built-ins look at ' Text
'\n'          Text.Whitespace

"the truth values of an iterable's contents.  " Text
'``'          Literal.String
'any()'       Literal.String
'``'          Literal.String
' returns '   Text
'\n'          Text.Whitespace

'True if any element in the iterable is a true value, and ' Text
'``'          Literal.String
'all()'       Literal.String
'``'          Literal.String
' '           Text
'\n'          Text.Whitespace

'returns True if all of the elements are true values' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'any([0,1,0]) =>' Literal.String
'\n'          Text

'      True\n    any([0,0,0]) =>\n      False\n    any([1,1,1]) =>\n      True\n    all([0,1,0]) =>\n      False\n    all([0,0,0]) => \n      False\n    all([1,1,1]) =>\n      True\n\n\n' Literal.String

'Small functions and the lambda statement' Generic.Heading
'\n'          Text

'----------------------------------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"When writing functional-style programs, you'll often need little" Text
'\n'          Text.Whitespace

'functions that act as predicates or that combine elements in some way.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"If there's a Python built-in or a module function that's suitable, you" Text
'\n'          Text.Whitespace

"don't need to define a new function at all" Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'stripped_lines = [line.strip() for line in lines]' Literal.String
'\n'          Text

'        existing_files = filter(os.path.exists, file_list)\n\n' Literal.String

"If the function you need doesn't exist, you need to write it.  One way" Text
'\n'          Text.Whitespace

'to write small functions is to use the ' Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
' statement.  ' Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'takes a number of parameters and an expression combining these parameters,' Text
'\n'          Text.Whitespace

'and creates a small function that returns the value of the expression' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'lowercase = lambda x: x.lower()' Literal.String
'\n'          Text

"\n        print_assign = lambda name, value: name + '=' + str(value)\n\n        adder = lambda x, y: x+y\n\n" Literal.String

'An alternative is to just use the ' Text
'``'          Literal.String
'def'         Literal.String
'``'          Literal.String
' statement and define a' Text
'\n'          Text.Whitespace

'function in the usual way' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'def lowercase(x):' Literal.String
'\n'          Text

"            return x.lower()\n\n        def print_assign(name, value):\n            return name + '=' + str(value)\n\n        def adder(x,y):\n            return x + y\n\n" Literal.String

"Which alternative is preferable?  That's a style question; my usual" Text
'\n'          Text.Whitespace

'course is to avoid using ' Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'One reason for my preference is that ' Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
' is quite limited in' Text
'\n'          Text.Whitespace

'the functions it can define.  The result has to be computable as a' Text
'\n'          Text.Whitespace

"single expression, which means you can't have multiway" Text
'\n'          Text.Whitespace

'``'          Literal.String
'if... elif... else' Literal.String
'``'          Literal.String
' comparisons or ' Text
'``'          Literal.String
'try... except' Literal.String
'``'          Literal.String
' statements.' Text
'\n'          Text.Whitespace

'If you try to do too much in a ' Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
" statement, you'll end up" Text
'\n'          Text.Whitespace

"with an overly complicated expression that's hard to read.  Quick," Text
'\n'          Text.Whitespace

"what's the following code doing?" Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'total = reduce(lambda a, b: (0, a[1] + b[1]), items)[1]' Literal.String
'\n'          Text

'\n'          Literal.String

'You can figure it out, but it takes time to disentangle the expression' Text
'\n'          Text.Whitespace

"to figure out what's going on.  Using a short nested" Text
'\n'          Text.Whitespace

'``'          Literal.String
'def'         Literal.String
'``'          Literal.String
' statements makes things a little bit better' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def combine (a, b):' Literal.String
'\n'          Text

'        return 0, a[1] + b[1]\n\n    total = reduce(combine, items)[1]\n\n' Literal.String

'But it would be best of all if I had simply used a ' Text
'``'          Literal.String
'for'         Literal.String
'``'          Literal.String
' loop'       Text
'::'          Literal.String.Escape
'\n\n'        Text

'     '       Literal.String
'total = 0'   Literal.String
'\n'          Text

'     for a, b in items:\n         total += b\n\n' Literal.String

'Or the '     Text
'``'          Literal.String
'sum()'       Literal.String
'``'          Literal.String
' built-in and a generator expression' Text
'::'          Literal.String.Escape
'\n\n'        Text

'     '       Literal.String
'total = sum(b for a,b in items)' Literal.String
'\n'          Text

'\n'          Literal.String

'Many uses of ' Text
'``'          Literal.String
'reduce()'    Literal.String
'``'          Literal.String
' are clearer when written as ' Text
'``'          Literal.String
'for'         Literal.String
'``'          Literal.String
' loops.'     Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Fredrik Lundh once suggested the following set of rules for refactoring ' Text
'\n'          Text.Whitespace

'uses of '    Text
'``'          Literal.String
'lambda'      Literal.String
'``'          Literal.String
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'1)'          Literal.Number
' Write a lambda function.' Text
'\n'          Text.Whitespace

'2)'          Literal.Number
' Write a comment explaining what the heck that lambda does.' Text
'\n'          Text.Whitespace

'3)'          Literal.Number
' Study the comment for a while, and think of a name that captures' Text
'\n'          Text.Whitespace

'   the essence of the comment.' Text
'\n'          Text.Whitespace

'4)'          Literal.Number
' Convert the lambda to a def statement, using that name.' Text
'\n'          Text.Whitespace

'5)'          Literal.Number
' Remove the comment.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"I really like these rules, but you're free to disagree that this " Text
'\n'          Text.Whitespace

'lambda-free style is better.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The itertools module' Generic.Heading
'\n'          Text

'-----------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' module contains a number of commonly-used iterators' Text
'\n'          Text.Whitespace

'as well as functions for combining several iterators.  This section' Text
'\n'          Text.Whitespace

"will introduce the module's contents by showing small examples." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"The module's functions fall into a few broad classes" Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' Functions that create a new iterator based on an existing iterator.' Text
'\n'          Text.Whitespace

'*'           Literal.Number
" Functions for treating an iterator's elements as function arguments." Text
'\n'          Text.Whitespace

'*'           Literal.Number
" Functions for selecting portions of an iterator's output." Text
'\n'          Text.Whitespace

'*'           Literal.Number
" A function for grouping an iterator's output." Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Creating new iterators' Generic.Heading
'\n'          Text

"''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'``'          Literal.String
'itertools.count(n)' Literal.String
'``'          Literal.String
' returns an infinite stream of' Text
'\n'          Text.Whitespace

'integers, increasing by 1 each time.  You can optionally supply the' Text
'\n'          Text.Whitespace

'starting number, which defaults to 0' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'itertools.count() =>' Literal.String
'\n'          Text

'          0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n        itertools.count(10) =>\n          10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\n\n' Literal.String

'``'          Literal.String
'itertools.cycle(iter)' Literal.String
'``'          Literal.String
' saves a copy of the contents of a provided' Text
'\n'          Text.Whitespace

'iterable and returns a new iterator that returns its elements from' Text
'\n'          Text.Whitespace

'first to last.  The new iterator will repeat these elements infinitely.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'itertools.cycle([1,2,3,4,5]) =>' Literal.String
'\n'          Text

'          1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...\n\n' Literal.String

'``'          Literal.String
'itertools.repeat(elem, [n])' Literal.String
'``'          Literal.String
' returns the provided element ' Text
'``'          Literal.String
'n'           Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'times, or returns the element endlessly if ' Text
'``'          Literal.String
'n'           Literal.String
'``'          Literal.String
' is not provided.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"itertools.repeat('abc') =>" Literal.String
'\n'          Text

"      abc, abc, abc, abc, abc, abc, abc, abc, abc, abc, ...\n    itertools.repeat('abc', 5) =>\n      abc, abc, abc, abc, abc\n\n" Literal.String

'``'          Literal.String
'itertools.chain(iterA, iterB, ...)' Literal.String
'``'          Literal.String
' takes an arbitrary number of' Text
'\n'          Text.Whitespace

'iterables as input, and returns all the elements of the first' Text
'\n'          Text.Whitespace

'iterator, then all the elements of the second, and so on, until all of' Text
'\n'          Text.Whitespace

'the iterables have been exhausted.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"itertools.chain(['a', 'b', 'c'], (1, 2, 3)) =>" Literal.String
'\n'          Text

'      a, b, c, 1, 2, 3\n\n' Literal.String

'``'          Literal.String
'itertools.izip(iterA, iterB, ...)' Literal.String
'``'          Literal.String
' takes one element from each iterable' Text
'\n'          Text.Whitespace

'and returns them in a tuple' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"itertools.izip(['a', 'b', 'c'], (1, 2, 3)) =>" Literal.String
'\n'          Text

"      ('a', 1), ('b', 2), ('c', 3)\n\n" Literal.String

"It's similiar to the built-in " Text
'``'          Literal.String
'zip()'       Literal.String
'``'          Literal.String
" function, but doesn't" Text
'\n'          Text.Whitespace

'construct an in-memory list and exhaust all the input iterators before' Text
'\n'          Text.Whitespace

"returning; instead tuples are constructed and returned only if they're" Text
'\n'          Text.Whitespace

'requested.  (The technical term for this behaviour is ' Text
'\n'          Text.Whitespace

'`lazy evaluation ' Literal.String
'<http://en.wikipedia.org/wiki/Lazy_evaluation>' Literal.String.Interpol
'`__'         Literal.String
'.)'          Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'This iterator is intended to be used with iterables that are all of' Text
'\n'          Text.Whitespace

'the same length.  If the iterables are of different lengths, the' Text
'\n'          Text.Whitespace

'resulting stream will be the same length as the shortest iterable.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"itertools.izip(['a', 'b'], (1, 2, 3)) =>" Literal.String
'\n'          Text

"      ('a', 1), ('b', 2)\n\n" Literal.String

'You should avoid doing this, though, because an element may be taken' Text
'\n'          Text.Whitespace

"from the longer iterators and discarded.  This means you can't go on" Text
'\n'          Text.Whitespace

'to use the iterators further because you risk skipping a discarded' Text
'\n'          Text.Whitespace

'element.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'itertools.islice(iter, [start], stop, [step])' Literal.String
'``'          Literal.String
' returns a stream' Text
'\n'          Text.Whitespace

"that's a slice of the iterator.  With a single " Text
'``'          Literal.String
'stop'        Literal.String
'``'          Literal.String
' argument, ' Text
'\n'          Text.Whitespace

'it will return the first ' Text
'``'          Literal.String
'stop'        Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

"elements.  If you supply a starting index, you'll get " Text
'``'          Literal.String
'stop-start'  Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'elements, and if you supply a value for ' Text
'``'          Literal.String
'step'        Literal.String
'``'          Literal.String
', elements will be' Text
'\n'          Text.Whitespace

"skipped accordingly.  Unlike Python's string and list slicing, you" Text
'\n'          Text.Whitespace

"can't use negative values for " Text
'``'          Literal.String
'start'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'stop'        Literal.String
'``'          Literal.String
', or '       Text
'``'          Literal.String
'step'        Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'itertools.islice(range(10), 8) =>' Literal.String
'\n'          Text

'      0, 1, 2, 3, 4, 5, 6, 7\n    itertools.islice(range(10), 2, 8) =>\n      2, 3, 4, 5, 6, 7\n    itertools.islice(range(10), 2, 8, 2) =>\n      2, 4, 6\n\n' Literal.String

'``'          Literal.String
'itertools.tee(iter, [n])' Literal.String
'``'          Literal.String
' replicates an iterator; it returns ' Text
'``'          Literal.String
'n'           Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'independent iterators that will all return the contents of the source' Text
'\n'          Text.Whitespace

"iterator.  If you don't supply a value for " Text
'``'          Literal.String
'n'           Literal.String
'``'          Literal.String
', the default is 2.' Text
'\n'          Text.Whitespace

'Replicating iterators requires saving some of the contents of the source' Text
'\n'          Text.Whitespace

'iterator, so this can consume significant memory if the iterator is large' Text
'\n'          Text.Whitespace

'and one of the new iterators is consumed more than the others.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'itertools.tee( itertools.count() ) =>' Literal.String
'\n'          Text

'           iterA, iterB\n\n        where iterA ->\n           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n\n        and   iterB ->\n           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n\n\n' Literal.String

'Calling functions on elements' Generic.Heading
'\n'          Text

"'''''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'Two functions are used for calling other functions on the contents of an' Text
'\n'          Text.Whitespace

'iterable.'   Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'itertools.imap(f, iterA, iterB, ...)' Literal.String
'``'          Literal.String
' returns '   Text
'\n'          Text.Whitespace

'a stream containing ' Text
'``'          Literal.String
'f(iterA[0], iterB[0]), f(iterA[1], iterB[1]),\nf(iterA[2], iterB[2]), ...' Literal.String
'``'          Literal.String
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'itertools.imap(operator.add, [5, 6, 5], [1, 2, 3]) =>' Literal.String
'\n'          Text

'      6, 8, 8\n\n' Literal.String

'The '        Text
'``'          Literal.String
'operator'    Literal.String
'``'          Literal.String
' module contains a set of functions ' Text
'\n'          Text.Whitespace

"corresponding to Python's operators.  Some examples are " Text
'\n'          Text.Whitespace

'``'          Literal.String
'operator.add(a, b)' Literal.String
'``'          Literal.String
' (adds two values), ' Text
'\n'          Text.Whitespace

'``'          Literal.String
'operator.ne(a, b)' Literal.String
'``'          Literal.String
' (same as '  Text
'``'          Literal.String
'a!=b'        Literal.String
'``'          Literal.String
'),'          Text
'\n'          Text.Whitespace

'and '        Text
'\n'          Text.Whitespace

'``'          Literal.String
"operator.attrgetter('id')" Literal.String
'``'          Literal.String
' (returns a callable that' Text
'\n'          Text.Whitespace

'fetches the ' Text
'``'          Literal.String
'"id"'        Literal.String
'``'          Literal.String
' attribute).' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'itertools.starmap(func, iter)' Literal.String
'``'          Literal.String
' assumes that the iterable will ' Text
'\n'          Text.Whitespace

'return a stream of tuples, and calls ' Text
'``'          Literal.String
'f()'         Literal.String
'``'          Literal.String
' using these tuples as the ' Text
'\n'          Text.Whitespace

'arguments'   Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'itertools.starmap(os.path.join, ' Literal.String
'\n'          Text

"                      [('/usr', 'bin', 'java'), ('/bin', 'python'),\n                       ('/usr', 'bin', 'perl'),('/usr', 'bin', 'ruby')])\n    =>\n      /usr/bin/java, /bin/python, /usr/bin/perl, /usr/bin/ruby\n\n\n" Literal.String

'Selecting elements' Generic.Heading
'\n'          Text

"''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"Another group of functions chooses a subset of an iterator's elements" Text
'\n'          Text.Whitespace

'based on a predicate.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'itertools.ifilter(predicate, iter)' Literal.String
'``'          Literal.String
' returns all the elements for' Text
'\n'          Text.Whitespace

'which the predicate returns true' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def is_even(x):' Literal.String
'\n'          Text

'        return (x % 2) == 0\n\n    itertools.ifilter(is_even, itertools.count()) =>\n      0, 2, 4, 6, 8, 10, 12, 14, ...\n\n' Literal.String

'``'          Literal.String
'itertools.ifilterfalse(predicate, iter)' Literal.String
'``'          Literal.String
' is the opposite, ' Text
'\n'          Text.Whitespace

'returning all elements for which the predicate returns false' Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'itertools.ifilterfalse(is_even, itertools.count()) =>' Literal.String
'\n'          Text

'      1, 3, 5, 7, 9, 11, 13, 15, ...\n\n' Literal.String

'``'          Literal.String
'itertools.takewhile(predicate, iter)' Literal.String
'``'          Literal.String
' returns elements for as long' Text
'\n'          Text.Whitespace

'as the predicate returns true.  Once the predicate returns false, ' Text
'\n'          Text.Whitespace

'the iterator will signal the end of its results.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'def less_than_10(x):' Literal.String
'\n'          Text

'        return (x < 10)\n\n    itertools.takewhile(less_than_10, itertools.count()) =>\n      0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n\n    itertools.takewhile(is_even, itertools.count()) =>\n      0\n\n' Literal.String

'``'          Literal.String
'itertools.dropwhile(predicate, iter)' Literal.String
'``'          Literal.String
' discards elements while the' Text
'\n'          Text.Whitespace

"predicate returns true, and then returns the rest of the iterable's" Text
'\n'          Text.Whitespace

'results.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'itertools.dropwhile(less_than_10, itertools.count()) =>' Literal.String
'\n'          Text

'      10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\n\n    itertools.dropwhile(is_even, itertools.count()) =>\n      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...\n\n\n' Literal.String

'Grouping elements' Generic.Heading
'\n'          Text

"'''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"The last function I'll discuss, " Text
'``'          Literal.String
'itertools.groupby(iter,\nkey_func=None)' Literal.String
'``'          Literal.String
', is the most complicated.  ' Text
'``'          Literal.String
'key_func(elem)' Literal.String
'``'          Literal.String
' is a'       Text
'\n'          Text.Whitespace

'function that can compute a key value for each element returned by the' Text
'\n'          Text.Whitespace

"iterable.  If you don't supply a key function, the key is simply each" Text
'\n'          Text.Whitespace

'element itself.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'groupby()'   Literal.String
'``'          Literal.String
' collects all the consecutive elements from the' Text
'\n'          Text.Whitespace

'underlying iterable that have the same key value, and returns a stream' Text
'\n'          Text.Whitespace

'of 2-tuples containing a key value and an iterator for the elements' Text
'\n'          Text.Whitespace

'with that key.  ' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
"city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'), " Literal.String
'\n'          Text

"                 ('Anchorage', 'AK'), ('Nome', 'AK'),\n                 ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'), \n                 ...\n                ]\n\n    def get_state ((city, state)):\n        return state\n\n    itertools.groupby(city_list, get_state) =>\n      ('AL', iterator-1),\n      ('AK', iterator-2),\n      ('AZ', iterator-3), ...\n\n    where\n    iterator-1 =>\n      ('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL')\n    iterator-2 => \n      ('Anchorage', 'AK'), ('Nome', 'AK')\n    iterator-3 =>\n      ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')\n\n" Literal.String

'``'          Literal.String
'groupby()'   Literal.String
'``'          Literal.String
" assumes that the underlying iterable's contents will" Text
'\n'          Text.Whitespace

'already be sorted based on the key.  Note that the returned iterators' Text
'\n'          Text.Whitespace

'also use the underlying iterable, so you have to consume the results' Text
'\n'          Text.Whitespace

'of iterator-1 before requesting iterator-2 and its corresponding key.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The functools module' Generic.Heading
'\n'          Text

'----------------------------------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'functools'   Literal.String
'``'          Literal.String
' module in Python 2.5 contains some higher-order' Text
'\n'          Text.Whitespace

'functions.  A ' Text
'**higher-order function**' Generic.Strong
' takes one or more functions as' Text
'\n'          Text.Whitespace

'input and returns a new function.  The most useful tool in this module' Text
'\n'          Text.Whitespace

'is the '     Text
'``'          Literal.String
'partial()'   Literal.String
'``'          Literal.String
' function.'  Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"For programs written in a functional style, you'll sometimes want to" Text
'\n'          Text.Whitespace

'construct variants of existing functions that have some of the' Text
'\n'          Text.Whitespace

'parameters filled in.  Consider a Python function ' Text
'``'          Literal.String
'f(a, b, c)'  Literal.String
'``'          Literal.String
'; you'       Text
'\n'          Text.Whitespace

'may wish to create a new function ' Text
'``'          Literal.String
'g(b, c)'     Literal.String
'``'          Literal.String
" that's equivalent to" Text
'\n'          Text.Whitespace

'``'          Literal.String
'f(1, b, c)'  Literal.String
'``'          Literal.String
"; you're filling in a value for one of " Text
'``'          Literal.String
'f()'         Literal.String
'``'          Literal.String
"'s parameters.  " Text
'\n'          Text.Whitespace

'This is called "partial function application".' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The constructor for ' Text
'``'          Literal.String
'partial'     Literal.String
'``'          Literal.String
' takes the arguments ' Text
'``'          Literal.String
'(function, arg1,\narg2, ... kwarg1=value1, kwarg2=value2)' Literal.String
'``'          Literal.String
'.  The resulting object is' Text
'\n'          Text.Whitespace

'callable, so you can just call it to invoke ' Text
'``'          Literal.String
'function'    Literal.String
'``'          Literal.String
' with the'   Text
'\n'          Text.Whitespace

'filled-in arguments.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

"Here's a small but realistic example" Text
'::'          Literal.String.Escape
'\n\n'        Text

'    '        Literal.String
'import functools' Literal.String
'\n'          Text

'\n    def log (message, subsystem):\n        "Write the contents of \'message\' to the specified subsystem."\n        print \'%s: %s\' % (subsystem, message)\n        ...\n\n    server_log = functools.partial(log, subsystem=\'server\')\n    server_log(\'Unable to open socket\')\n\n\n' Literal.String

'The operator module' Generic.Heading
'\n'          Text

'-------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'operator'    Literal.String
'``'          Literal.String
' module was mentioned earlier.  It contains a set of' Text
'\n'          Text.Whitespace

"functions corresponding to Python's operators.  These functions " Text
'\n'          Text.Whitespace

'are often useful in functional-style code because they save you ' Text
'\n'          Text.Whitespace

'from writing trivial functions that perform a single operation.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Some of the functions in this module are' Text
':'           Text
'\n'          Text.Whitespace

'\n'          Text

'*'           Literal.Number
' Math operations' Text
':'           Text
' '           Text
'``'          Literal.String
'add()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'sub()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'mul()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'div()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'floordiv()'  Literal.String
'``'          Literal.String
','           Text
'\n'          Text.Whitespace

'  '          Text
'``'          Literal.String
'abs()'       Literal.String
'``'          Literal.String
', ...'       Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Logical operations' Text
':'           Text
' '           Text
'``'          Literal.String
'not_()'      Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'truth()'     Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Bitwise operations' Text
':'           Text
' '           Text
'``'          Literal.String
'and_()'      Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'or_()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'invert()'    Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Comparisons' Text
':'           Text
' '           Text
'``'          Literal.String
'eq()'        Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'ne()'        Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'lt()'        Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'le()'        Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'gt()'        Literal.String
'``'          Literal.String
', and '      Text
'``'          Literal.String
'ge()'        Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'*'           Literal.Number
' Object identity' Text
':'           Text
' '           Text
'``'          Literal.String
'is_()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'is_not()'    Literal.String
'``'          Literal.String
'.'           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Consult '    Text
"`the operator module's documentation " Literal.String
'<http://docs.python.org/lib/module-operator.html>' Literal.String.Interpol
'`__'         Literal.String
' for a complete' Text
'\n'          Text.Whitespace

'list.'       Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The functional module' Generic.Heading
'\n'          Text

'---------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

"Collin Winter's " Text
'`functional module ' Literal.String
'<http://oakwinter.com/code/functional/>' Literal.String.Interpol
'`__'         Literal.String
' '           Text
'\n'          Text.Whitespace

'provides a number of more' Text
'\n'          Text.Whitespace

'advanced tools for functional programming. It also reimplements' Text
'\n'          Text.Whitespace

'several Python built-ins, trying to make them more intuitive to those' Text
'\n'          Text.Whitespace

'used to functional programming in other languages.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'This section contains an introduction to some of the most important' Text
'\n'          Text.Whitespace

'functions in ' Text
'``'          Literal.String
'functional'  Literal.String
'``'          Literal.String
'; full documentation can be found at ' Text
'`'           Text
'the'         Text
'\n'          Text.Whitespace

"project's website <http" Text
':'           Text
'//oakwinter.com/code/functional/documentation/>' Text
'`'           Text
'__.'         Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'``'          Literal.String
'compose(outer, inner, unpack=False)' Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'The '        Text
'``'          Literal.String
'compose()'   Literal.String
'``'          Literal.String
' function implements function composition.' Text
'\n'          Text.Whitespace

'In other words, it returns a wrapper around the ' Text
'``'          Literal.String
'outer'       Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'inner'       Literal.String
'``'          Literal.String
' callables, such' Text
'\n'          Text.Whitespace

'that the return value from ' Text
'``'          Literal.String
'inner'       Literal.String
'``'          Literal.String
' is fed directly to ' Text
'``'          Literal.String
'outer'       Literal.String
'``'          Literal.String
'.  That is,' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'>>> def add(a, b):' Literal.String
'\n'          Text

'        ...     return a + b\n        ...\n        >>> def double(a):\n        ...     return 2 * a\n        ...\n        >>> compose(double, add)(5, 6)\n        22\n\n' Literal.String

'is equivalent to' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'>>> double(add(5, 6))' Literal.String
'\n'          Text

'        22\n                    \n' Literal.String

'The '        Text
'``'          Literal.String
'unpack'      Literal.String
'``'          Literal.String
' keyword is provided to work around the fact that Python functions are not always' Text
'\n'          Text.Whitespace

'`fully curried ' Literal.String
'<http://en.wikipedia.org/wiki/Currying>' Literal.String.Interpol
'`__'         Literal.String
'.'           Text
'\n'          Text.Whitespace

'By default, it is expected that the ' Text
'``'          Literal.String
'inner'       Literal.String
'``'          Literal.String
' function will return a single object and that the ' Text
'``'          Literal.String
'outer'       Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'function will take a single argument. Setting the ' Text
'``'          Literal.String
'unpack'      Literal.String
'``'          Literal.String
' argument causes ' Text
'``'          Literal.String
'compose'     Literal.String
'``'          Literal.String
' to expect a' Text
'\n'          Text.Whitespace

'tuple from ' Text
'``'          Literal.String
'inner'       Literal.String
'``'          Literal.String
' which will be expanded before being passed to ' Text
'``'          Literal.String
'outer'       Literal.String
'``'          Literal.String
'. Put simply,' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'compose(f, g)(5, 6)' Literal.String
'\n'          Text

'                    \n' Literal.String

'is equivalent to' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'f(g(5, 6))'  Literal.String
'\n'          Text

'                    \n' Literal.String

'while'       Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'compose(f, g, unpack=True)(5, 6)' Literal.String
'\n'          Text

'                    \n' Literal.String

'is equivalent to' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'f(*g(5, 6))' Literal.String
'\n'          Text

'\n'          Literal.String

'Even though ' Text
'``'          Literal.String
'compose()'   Literal.String
'``'          Literal.String
" only accepts two functions, it's trivial to" Text
'\n'          Text.Whitespace

"build up a version that will compose any number of functions. We'll" Text
'\n'          Text.Whitespace

'use '        Text
'``'          Literal.String
'reduce()'    Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'compose()'   Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'partial()'   Literal.String
'``'          Literal.String
' (the last of which' Text
'\n'          Text.Whitespace

'is provided by both ' Text
'``'          Literal.String
'functional'  Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'functools'   Literal.String
'``'          Literal.String
').'          Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'from functional import compose, partial' Literal.String
'\n'          Text

'        \n        multi_compose = partial(reduce, compose)\n        \n' Literal.String

'    '        Text
'\n'          Text.Whitespace

'We can also use ' Text
'``'          Literal.String
'map()'       Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'compose()'   Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'partial()'   Literal.String
'``'          Literal.String
' to craft a' Text
'\n'          Text.Whitespace

'version of ' Text
'``'          Literal.String
'"".join(...)' Literal.String
'``'          Literal.String
' that converts its arguments to string' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'from functional import compose, partial' Literal.String
'\n'          Text

'        \n        join = compose("".join, partial(map, str))\n\n\n' Literal.String

'``'          Literal.String
'flip(func)'  Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'                    ' Text
'\n'          Text.Whitespace

'``'          Literal.String
'flip()'      Literal.String
'``'          Literal.String
' wraps the callable in ' Text
'``'          Literal.String
'func'        Literal.String
'``'          Literal.String
' and  '      Text
'\n'          Text.Whitespace

'causes it to receive its non-keyword arguments in reverse order.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'>>> def triple(a, b, c):' Literal.String
'\n'          Text

'        ...     return (a, b, c)\n        ...\n        >>> triple(5, 6, 7)\n        (5, 6, 7)\n        >>>\n        >>> flipped_triple = flip(triple)\n        >>> flipped_triple(5, 6, 7)\n        (7, 6, 5)\n\n' Literal.String

'``'          Literal.String
'foldl(func, start, iterable)' Literal.String
'``'          Literal.String
'\n'          Text.Whitespace

'                    ' Text
'\n'          Text.Whitespace

'``'          Literal.String
'foldl()'     Literal.String
'``'          Literal.String
" takes a binary function, a starting value (usually some kind of 'zero'), and an iterable." Text
'\n'          Text.Whitespace

'The function is applied to the starting value and the first element of the list, then the result of' Text
'\n'          Text.Whitespace

'that and the second element of the list, then the result of that and the third element of the list,' Text
'\n'          Text.Whitespace

'and so on.'  Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'This means that a call such as' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'foldl(f, 0, [1, 2, 3])' Literal.String
'\n'          Text

'\n'          Literal.String

'is equivalent to' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'f(f(f(0, 1), 2), 3)' Literal.String
'\n'          Text

'\n'          Literal.String

'    '        Text
'\n'          Text.Whitespace

'``'          Literal.String
'foldl()'     Literal.String
'``'          Literal.String
' is roughly equivalent to the following recursive function' Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'def foldl(func, start, seq):' Literal.String
'\n'          Text

'            if len(seq) == 0:\n                return start\n\n            return foldl(func, func(start, seq[0]), seq[1:])\n\n' Literal.String

'Speaking of equivalence, the above ' Text
'``'          Literal.String
'foldl'       Literal.String
'``'          Literal.String
' call can be expressed in terms of the built-in ' Text
'``'          Literal.String
'reduce'      Literal.String
'``'          Literal.String
' like'       Text
'\n'          Text.Whitespace

'so'          Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'reduce(f, [1, 2, 3], 0)' Literal.String
'\n'          Text

'\n\n'        Literal.String

'We can use ' Text
'``'          Literal.String
'foldl()'     Literal.String
'``'          Literal.String
', '          Text
'``'          Literal.String
'operator.concat()' Literal.String
'``'          Literal.String
' and '       Text
'``'          Literal.String
'partial()'   Literal.String
'``'          Literal.String
' to'         Text
'\n'          Text.Whitespace

"write a cleaner, more aesthetically-pleasing version of Python's" Text
'\n'          Text.Whitespace

'``'          Literal.String
'"".join(...)' Literal.String
'``'          Literal.String
' idiom'      Text
'::'          Literal.String.Escape
'\n\n'        Text

'        '    Literal.String
'from functional import foldl, partial' Literal.String
'\n'          Text

'        from operator import concat\n        \n        join = partial(foldl, concat, "")\n\n\n' Literal.String

'Revision History and Acknowledgements' Generic.Heading
'\n'          Text

'------------------------------------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'The author would like to thank the following people for offering' Text
'\n'          Text.Whitespace

'suggestions, corrections and assistance with various drafts of this' Text
'\n'          Text.Whitespace

'article'     Text
':'           Text
' Ian Bicking, Nick Coghlan, Nick Efford, Raymond Hettinger,' Text
'\n'          Text.Whitespace

'Jim Jewett, Mike Krell, Leandro Lameiro, Jussi Salmela, ' Text
'\n'          Text.Whitespace

'Collin Winter, Blake Winton.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Version 0.1' Text
':'           Text
' posted June 30 2006.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Version 0.11' Text
':'           Text
' posted July 1 2006.  Typo fixes.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Version 0.2' Text
':'           Text
' posted July 10 2006.  Merged genexp and listcomp' Text
'\n'          Text.Whitespace

'sections into one.  Typo fixes.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Version 0.21' Text
':'           Text
' Added more references suggested on the tutor mailing list.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Version 0.30' Text
':'           Text
' Adds a section on the ' Text
'``'          Literal.String
'functional'  Literal.String
'``'          Literal.String
' module written by' Text
'\n'          Text.Whitespace

'Collin Winter; adds short section on the operator module; a few other' Text
'\n'          Text.Whitespace

'edits.'      Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'References'  Generic.Heading
'\n'          Text

'--------------------' Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'General'     Generic.Heading
'\n'          Text

"'''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'**Structure and Interpretation of Computer Programs**' Generic.Strong
', by '       Text
'\n'          Text.Whitespace

'Harold Abelson and Gerald Jay Sussman with Julie Sussman.' Text
'\n'          Text.Whitespace

'Full text at http' Text
':'           Text
'//mitpress.mit.edu/sicp/.' Text
'\n'          Text.Whitespace

'In this classic textbook of computer science,  chapters 2 and 3 discuss the' Text
'\n'          Text.Whitespace

'use of sequences and streams to organize the data flow inside a' Text
'\n'          Text.Whitespace

'program.  The book uses Scheme for its examples, but many of the' Text
'\n'          Text.Whitespace

'design approaches described in these chapters are applicable to' Text
'\n'          Text.Whitespace

'functional-style Python code.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//www.defmacro.org/ramblings/fp.html' Text
':'           Text
' A general ' Text
'\n'          Text.Whitespace

'introduction to functional programming that uses Java examples' Text
'\n'          Text.Whitespace

'and has a lengthy historical introduction.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//en.wikipedia.org/wiki/Functional_programming' Text
':'           Text
'\n'          Text.Whitespace

'General Wikipedia entry describing functional programming.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//en.wikipedia.org/wiki/Coroutine' Text
':'           Text
'\n'          Text.Whitespace

'Entry for coroutines.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//en.wikipedia.org/wiki/Currying' Text
':'           Text
'\n'          Text.Whitespace

'Entry for the concept of currying.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Python-specific' Generic.Heading
'\n'          Text

"'''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//gnosis.cx/TPiP/' Text
':'           Text
'\n'          Text.Whitespace

"The first chapter of David Mertz's book " Text
':title-reference:' Name.Attribute
'`Text Processing in Python`' Name.Variable
' '           Text
'\n'          Text.Whitespace

'discusses functional programming for text processing, in the section titled' Text
'\n'          Text.Whitespace

'"Utilizing Higher-Order Functions in Text Processing".' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Mertz also wrote a 3-part series of articles on functional programming' Text
'\n'          Text.Whitespace

"for IBM's DeveloperWorks site; see " Text
'\n'          Text.Whitespace

'`part 1 '    Literal.String
'<http://www-128.ibm.com/developerworks/library/l-prog.html>' Literal.String.Interpol
'`__'         Literal.String
','           Text
'\n'          Text.Whitespace

'`part 2 '    Literal.String
'<http://www-128.ibm.com/developerworks/library/l-prog2.html>' Literal.String.Interpol
'`__'         Literal.String
', and'       Text
'\n'          Text.Whitespace

'`part 3 '    Literal.String
'<http://www-128.ibm.com/developerworks/linux/library/l-prog3.html>' Literal.String.Interpol
'`__'         Literal.String
','           Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'\n'          Text.Whitespace

'Python documentation' Generic.Heading
'\n'          Text

"'''''''''''''''''''''''''''" Generic.Heading
'\n'          Text

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//docs.python.org/lib/module-itertools.html' Text
':'           Text
'\n'          Text.Whitespace

'Documentation for the ' Text
'``'          Literal.String
'itertools'   Literal.String
'``'          Literal.String
' module.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//docs.python.org/lib/module-operator.html' Text
':'           Text
'\n'          Text.Whitespace

'Documentation for the ' Text
'``'          Literal.String
'operator'    Literal.String
'``'          Literal.String
' module.'    Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//www.python.org/dev/peps/pep-0289/' Text
':'           Text
'\n'          Text.Whitespace

'PEP 289'     Text
':'           Text
' "Generator Expressions"' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

'http'        Text
':'           Text
'//www.python.org/dev/peps/pep-0342/' Text
'\n'          Text.Whitespace

'PEP 342'     Text
':'           Text
' "Coroutines via Enhanced Generators" describes the new generator' Text
'\n'          Text.Whitespace

'features in Python 2.5.' Text
'\n'          Text.Whitespace

'\n'          Text.Whitespace

".. comment\n\n    Topics to place\n    -----------------------------\n\n    XXX os.walk()\n\n    XXX Need a large example.\n\n    But will an example add much?  I'll post a first draft and see\n    what the comments say.\n\n" Comment.Preproc

'.. comment\n\n    Original outline:\n    Introduction\n            Idea of FP\n                    Programs built out of functions\n                    Functions are strictly input-output, no internal state\n            Opposed to OO programming, where objects have state\n\n            Why FP?\n                    Formal provability\n                            Assignment is difficult to reason about\n                            Not very relevant to Python\n                    Modularity\n                            Small functions that do one thing\n                    Debuggability:\n                            Easy to test due to lack of state\n                            Easy to verify output from intermediate steps\n                    Composability\n                            You assemble a toolbox of functions that can be mixed\n\n    Tackling a problem\n            Need a significant example\n\n    Iterators\n    Generators\n    The itertools module\n    List comprehensions\n    Small functions and the lambda statement\n    Built-in functions\n            map\n            filter\n            reduce\n\n' Comment.Preproc

".. comment\n\n    Handy little function for printing part of an iterator -- used\n    while writing this document.\n\n    import itertools\n    def print_iter(it):\n         slice = itertools.islice(it, 10)\n         for elem in slice[:-1]:\n             sys.stdout.write(str(elem))\n             sys.stdout.write(', ')\n        print elem[-1]\n" Comment.Preproc