Skip to content

Commit

Permalink
Adds another data example
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Jun 24, 2016
1 parent bac16a9 commit 666e3f7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ Any valid Python identifier may be used for a field name except for names starti

Named tuple instances do not have per-instance dictionaries, so they are lightweight and require no more memory than regular tuples.

##### Example
##### Examples

###### Coconut
```coconut
Expand All @@ -459,6 +459,23 @@ v |> print # all data types come with a built-in __repr__
v |> abs |> print
v.x = 2 # this will fail because data objects are immutable
```
_Showcases the syntax and immutable nature of `data` types._
```coconut
data Empty(): pass
data Leaf(n): pass
data Node(l, r): pass
def size(Empty()) = 0
@addpattern(size)
def size(Leaf(_)) = 1
@addpattern(size)
def size(Node(left, right)) = size(left) + size(right)
size(Node(Empty(), Leaf(10))) == 1
```
_Showcases the algebraic nature of `data` types when combined with pattern-matching._

###### Python
```coconut_python
Expand Down

0 comments on commit 666e3f7

Please sign in to comment.