| 1 |
--- |
| 2 |
kernelspec: |
| 3 |
display_name: Coconut |
| 4 |
language: coconut |
| 5 |
name: coconut |
| 6 |
--- |
| 7 |
|
| 8 |
Taken from [coconut-lang.org](coconut-lang.org) |
| 9 |
|
| 10 |
+++ |
| 11 |
|
| 12 |
pipeline-style programming |
| 13 |
|
| 14 |
```{code-cell} coconut |
| 15 |
"hello, world!" |> print |
| 16 |
``` |
| 17 |
|
| 18 |
prettier lambdas |
| 19 |
|
| 20 |
```{code-cell} coconut |
| 21 |
x -> x ** 2 |
| 22 |
``` |
| 23 |
|
| 24 |
partial application |
| 25 |
|
| 26 |
```{code-cell} coconut |
| 27 |
range(10) |> map$(pow$(?, 2)) |> list |
| 28 |
``` |
| 29 |
|
| 30 |
pattern-matching |
| 31 |
|
| 32 |
```{code-cell} coconut |
| 33 |
match [head] + tail in [0, 1, 2, 3]: |
| 34 |
print(head, tail) |
| 35 |
``` |
| 36 |
|
| 37 |
destructuring assignment |
| 38 |
|
| 39 |
```{code-cell} coconut |
| 40 |
{"list": [0] + rest} = {"list": [0, 1, 2, 3]} |
| 41 |
``` |
| 42 |
|
| 43 |
infix notation |
| 44 |
|
| 45 |
```{code-cell} coconut |
| 46 |
# 5 `mod` 3 == 2 |
| 47 |
``` |
| 48 |
|
| 49 |
operator functions |
| 50 |
|
| 51 |
```{code-cell} coconut |
| 52 |
product = reduce$(*) |
| 53 |
``` |
| 54 |
|
| 55 |
function composition |
| 56 |
|
| 57 |
```{code-cell} coconut |
| 58 |
# (f..g..h)(x, y, z) |
| 59 |
``` |
| 60 |
|
| 61 |
lazy lists |
| 62 |
|
| 63 |
```{code-cell} coconut |
| 64 |
# (| first_elem() |) :: rest_elems() |
| 65 |
``` |
| 66 |
|
| 67 |
parallel programming |
| 68 |
|
| 69 |
```{code-cell} coconut |
| 70 |
range(100) |> parallel_map$(pow$(2)) |> list |
| 71 |
``` |
| 72 |
|
| 73 |
tail call optimization |
| 74 |
|
| 75 |
```{code-cell} coconut |
| 76 |
def factorial(n, acc=1): |
| 77 |
case n: |
| 78 |
match 0: |
| 79 |
return acc |
| 80 |
match _ is int if n > 0: |
| 81 |
return factorial(n-1, acc*n) |
| 82 |
``` |
| 83 |
|
| 84 |
algebraic data types |
| 85 |
|
| 86 |
```{code-cell} coconut |
| 87 |
data Empty() |
| 88 |
data Leaf(n) |
| 89 |
data Node(l, r) |
| 90 |
|
| 91 |
def size(Empty()) = 0 |
| 92 |
|
| 93 |
addpattern def size(Leaf(n)) = 1 |
| 94 |
|
| 95 |
addpattern def size(Node(l, r)) = size(l) + size(r) |
| 96 |
``` |
| 97 |
|
| 98 |
and much more! |
| 99 |
|
| 100 |
Like what you see? Don't forget to star Coconut on GitHub! |
| 101 |
|
| 102 |
```{code-cell} coconut |
| 103 |
|
| 104 |
``` |