Skip to content

Commit 676eb2c

Browse files
committed
📚 DOCS: Add example of using other kernels
1 parent 39c1bb9 commit 676eb2c

7 files changed

Lines changed: 36 additions & 7 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
- cache-pip
1414

1515
- run: |
16+
pip install --user "ipython<=7.11.0"
1617
pip install --user .[rtd]
1718
1819
- save_cache:

.readthedocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ python:
77
path: .
88
extra_requirements:
99
- rtd
10+
- requirements: docs/requirements.txt
1011

1112
sphinx:
1213
builder: html

docs/conf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"repository_branch": "master",
6060
"use_edit_page_button": True,
6161
"path_to_docs": "docs/",
62+
"expand_sections": ["use/index", "examples/index"],
6263
}
6364

6465
intersphinx_mapping = {
@@ -85,3 +86,11 @@
8586
myst_amsmath_enable = True
8687
myst_html_img_enable = True
8788
myst_url_schemes = ("http", "https", "mailto")
89+
90+
91+
def setup(app):
92+
import subprocess
93+
94+
# this is required to register the coconut kernel with Jupyter,
95+
# to execute docs/examples/coconut-lang.md
96+
subprocess.check_call(["coconut", "--jupyter"])

docs/examples/coconut-lang.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,34 @@ kernelspec:
1111
name: coconut
1212
---
1313

14-
# Notebooks in other languages
14+
# Other Programming Languages
1515

16-
<http://coconut-lang.org/>
16+
A Jupyter Notebook can utilise any program kernel that implements the [Jupyter messaging protocol](http://jupyter-client.readthedocs.io/en/latest/messaging.html) for executing code.
17+
There are kernels available for [Python](http://ipython.org/notebook.html), [Julia](https://github.com/JuliaLang/IJulia.jl), [Ruby](https://github.com/minad/iruby), [Haskell](https://github.com/gibiansky/IHaskell) and [many other languages](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels).
18+
19+
In this notebook we demonstrate executing code with the [Coconut Programming Language](http://coconut-lang.org), a variant of Python built for *simple, elegant, Pythonic functional programming*.
20+
21+
In the first example we will define a recursive `factorial` function, a fundamentally functional approach that doesn’t involve any state changes or loops:
1722

1823
```{code-cell} coconut
1924
def factorial(n):
2025
"""Compute n! where n is an integer >= 0."""
21-
if n `isinstance` int and n >= 0:
22-
acc = 1
23-
for x in range(1, n+1):
24-
acc *= x
25-
return acc
26+
case n:
27+
match 0:
28+
return 1
29+
match x is int if x > 0:
30+
return x * factorial(x-1)
2631
else:
2732
raise TypeError("the argument to factorial must be an integer >= 0")
2833
2934
3 |> factorial |> print
3035
```
3136

37+
Although this example is very basic, pattern-matching is both one of Coconut’s most powerful and most complicated features.
38+
39+
In the second example, we implement the quick sort algorithm.
40+
This quick_sort algorithm works using a bunch of new constructs:
41+
3242
```{code-cell} coconut
3343
def quick_sort(l):
3444
"""Sort the input iterator using the quick sort algorithm."""
@@ -42,6 +52,8 @@ def quick_sort(l):
4252
[3,0,4,2,1] |> quick_sort |> list |> print
4353
```
4454

55+
Finally, we see that exceptions are raised as one would expect:
56+
4557
```{code-cell} coconut
4658
:tags: [raises-exception]
4759
x

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ For information on using and configuring MyST-NB, as well as some examples of no
6464
outputs, see the pages below:
6565

6666
```{toctree}
67+
:maxdepth: 2
6768
use/index
6869
use/markdown
6970
```
@@ -72,6 +73,7 @@ In addition, here is a reference page that uses the `jupyter-sphinx` package to
7273
outputs, to compare how these outputs look relative to the MyST-NB style.
7374

7475
```{toctree}
76+
:maxdepth: 2
7577
examples/index
7678
```
7779

docs/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# this is only required by coconut kernel
2+
ipython<=7.11.0

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ commands = pytest {posargs}
2424
[testenv:docs]
2525
recreate = false
2626
extras = rtd
27+
deps =
28+
ipython<=7.11.0 # required by coconut
2729
whitelist_externals = rm
2830
commands =
2931
rm -rf docs/_build

0 commit comments

Comments
 (0)