Skip to content

Commit a11d13c

Browse files
committed
Add the text version of the lua example notebook
Plus, remove the final space in markdown cells that is not stable in round trips with MyST notebooks
1 parent cefe5c4 commit a11d13c

File tree

9 files changed

+392
-7
lines changed

9 files changed

+392
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Jupytext ChangeLog
22
==================
33

4+
1.16.3-dev (2024-07-??)
5+
-------------------
6+
7+
**Added**
8+
- Added support for Lua notebooks ([#1252](https://github.com/mwouts/jupytext/pull/1252)) - thanks to [erentar](https://github.com/erentar) for this contribution
9+
10+
411
1.16.2 (2024-05-05)
512
-------------------
613

src/jupytext/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Jupytext's version number"""
22

3-
__version__ = "1.16.2"
3+
__version__ = "1.16.3-dev"

tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
"cell_type": "markdown",
1919
"metadata": {},
2020
"source": [
21-
"Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator). "
21+
"Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator)."
2222
]
2323
},
2424
{
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: "
28+
"A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:"
2929
]
3030
},
3131
{
@@ -45,7 +45,7 @@
4545
"cell_type": "markdown",
4646
"metadata": {},
4747
"source": [
48-
" Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: "
48+
" Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:"
4949
]
5050
},
5151
{
@@ -74,14 +74,14 @@
7474
"cell_type": "markdown",
7575
"metadata": {},
7676
"source": [
77-
"Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. "
77+
"Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table."
7878
]
7979
},
8080
{
8181
"cell_type": "markdown",
8282
"metadata": {},
8383
"source": [
84-
"As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: "
84+
"As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:"
8585
]
8686
},
8787
{
@@ -109,7 +109,7 @@
109109
"cell_type": "markdown",
110110
"metadata": {},
111111
"source": [
112-
" With this function, it is easy to print those function names in alphabetical order. The loop "
112+
" With this function, it is easy to print those function names in alphabetical order. The loop"
113113
]
114114
},
115115
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
jupyter:
3+
kernelspec:
4+
display_name: Lua
5+
language: lua
6+
name: lua
7+
---
8+
9+
Source: https://www.lua.org/pil/19.3.html
10+
11+
12+
# Sort
13+
14+
15+
Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator).
16+
17+
18+
A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:
19+
20+
```{lua}
21+
lines = {
22+
luaH_set = 10,
23+
luaH_get = 24,
24+
luaH_present = 48,
25+
}
26+
```
27+
28+
Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:
29+
30+
```{lua}
31+
a = {}
32+
for n in pairs(lines) do table.insert(a, n) end
33+
table.sort(a)
34+
for i,n in ipairs(a) do print(n) end
35+
```
36+
37+
Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table.
38+
39+
40+
As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:
41+
42+
```{lua}
43+
function pairsByKeys (t, f)
44+
local a = {}
45+
for n in pairs(t) do table.insert(a, n) end
46+
table.sort(a, f)
47+
local i = 0 -- iterator variable
48+
local iter = function () -- iterator function
49+
i = i + 1
50+
if a[i] == nil then return nil
51+
else return a[i], t[a[i]]
52+
end
53+
end
54+
return iter
55+
end
56+
```
57+
58+
With this function, it is easy to print those function names in alphabetical order. The loop
59+
60+
```{lua}
61+
for name, line in pairsByKeys(lines) do
62+
print(name, line)
63+
end
64+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- -*- coding: utf-8 -*-
2+
-- ---
3+
-- jupyter:
4+
-- kernelspec:
5+
-- display_name: Lua
6+
-- language: lua
7+
-- name: lua
8+
-- ---
9+
10+
-- %% [markdown]
11+
-- Source: https://www.lua.org/pil/19.3.html
12+
13+
-- %% [markdown]
14+
-- # Sort
15+
16+
-- %% [markdown]
17+
-- Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator).
18+
19+
-- %% [markdown]
20+
-- A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:
21+
22+
-- %%
23+
lines = {
24+
luaH_set = 10,
25+
luaH_get = 24,
26+
luaH_present = 48,
27+
}
28+
29+
-- %% [markdown]
30+
-- Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:
31+
32+
-- %%
33+
a = {}
34+
for n in pairs(lines) do table.insert(a, n) end
35+
table.sort(a)
36+
for i,n in ipairs(a) do print(n) end
37+
38+
-- %% [markdown]
39+
-- Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table.
40+
41+
-- %% [markdown]
42+
-- As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:
43+
44+
-- %%
45+
function pairsByKeys (t, f)
46+
local a = {}
47+
for n in pairs(t) do table.insert(a, n) end
48+
table.sort(a, f)
49+
local i = 0 -- iterator variable
50+
local iter = function () -- iterator function
51+
i = i + 1
52+
if a[i] == nil then return nil
53+
else return a[i], t[a[i]]
54+
end
55+
end
56+
return iter
57+
end
58+
59+
-- %% [markdown]
60+
-- With this function, it is easy to print those function names in alphabetical order. The loop
61+
62+
-- %%
63+
for name, line in pairsByKeys(lines) do
64+
print(name, line)
65+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
jupyter:
3+
kernelspec:
4+
display_name: Lua
5+
language: lua
6+
name: lua
7+
---
8+
9+
Source: https://www.lua.org/pil/19.3.html
10+
11+
12+
# Sort
13+
14+
15+
Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator).
16+
17+
18+
A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:
19+
20+
```lua
21+
lines = {
22+
luaH_set = 10,
23+
luaH_get = 24,
24+
luaH_present = 48,
25+
}
26+
```
27+
28+
Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:
29+
30+
```lua
31+
a = {}
32+
for n in pairs(lines) do table.insert(a, n) end
33+
table.sort(a)
34+
for i,n in ipairs(a) do print(n) end
35+
```
36+
37+
Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table.
38+
39+
40+
As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:
41+
42+
```lua
43+
function pairsByKeys (t, f)
44+
local a = {}
45+
for n in pairs(t) do table.insert(a, n) end
46+
table.sort(a, f)
47+
local i = 0 -- iterator variable
48+
local iter = function () -- iterator function
49+
i = i + 1
50+
if a[i] == nil then return nil
51+
else return a[i], t[a[i]]
52+
end
53+
end
54+
return iter
55+
end
56+
```
57+
58+
With this function, it is easy to print those function names in alphabetical order. The loop
59+
60+
```lua
61+
for name, line in pairsByKeys(lines) do
62+
print(name, line)
63+
end
64+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
kernelspec:
3+
display_name: Lua
4+
language: lua
5+
name: lua
6+
---
7+
8+
Source: https://www.lua.org/pil/19.3.html
9+
10+
+++
11+
12+
# Sort
13+
14+
+++
15+
16+
Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<´ operator).
17+
18+
+++
19+
20+
A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this:
21+
22+
```{code-cell}
23+
lines = {
24+
luaH_set = 10,
25+
luaH_get = 24,
26+
luaH_present = 48,
27+
}
28+
```
29+
30+
Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result:
31+
32+
```{code-cell}
33+
a = {}
34+
for n in pairs(lines) do table.insert(a, n) end
35+
table.sort(a)
36+
for i,n in ipairs(a) do print(n) end
37+
```
38+
39+
Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table.
40+
41+
+++
42+
43+
As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table:
44+
45+
```{code-cell}
46+
function pairsByKeys (t, f)
47+
local a = {}
48+
for n in pairs(t) do table.insert(a, n) end
49+
table.sort(a, f)
50+
local i = 0 -- iterator variable
51+
local iter = function () -- iterator function
52+
i = i + 1
53+
if a[i] == nil then return nil
54+
else return a[i], t[a[i]]
55+
end
56+
end
57+
return iter
58+
end
59+
```
60+
61+
With this function, it is easy to print those function names in alphabetical order. The loop
62+
63+
```{code-cell}
64+
for name, line in pairsByKeys(lines) do
65+
print(name, line)
66+
end
67+
```

0 commit comments

Comments
 (0)