Skip to content

Commit

Permalink
update tests and README
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdagos committed Aug 28, 2016
1 parent 0bb67b0 commit 0b32106
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def is-zero [num] =
}
```

Will only return `true` for `is-zero $ 0`

```
def is-single-element [list] =
match list {
Expand All @@ -148,6 +150,8 @@ def is-single-element [list] =
}
```

Will only return true for lists with one item, of whatever type and value.

A more advanced usage example would be

Checking a static prefix:
Expand All @@ -159,7 +163,13 @@ def list-starts-with-one? [list] =
}
```

Will return true for lists like
`[1], [1, 2], [1, 2, 3], [1, "a"], [1, 4], [1,-1]`, but never for lists that
don't start with a `1`.


Or

```
def list-has-one-a-wildcard-and-three? [list] =
match list {
Expand All @@ -168,21 +178,37 @@ def list-has-one-a-wildcard-and-three? [list] =
}
```

Will return true for a list like `[1, 3, 3]` or `[1, -1, 3, 4, 5]`.

And of course some classic examples using pattern matching

```
// Gets first element of a given list
// Example: (list-head $ [1, 2, 3]) => 1
def list-head [list] =
match list {
[] -> error $ "Empty list";
[ x | _ ] -> x;
}
// Discards the first element of a list and returns the rest
// Example: (list-tail $ [1, 2, 3]) => [2, 3]
def list-tail [list] =
match list {
[] -> error $ "Empty list";
[ _ | xs ] -> xs;
}
// Reverses a list
// Example: (list-reverse $ [1, 2, 3, 4]) => [4, 3, 2, 1]
def list-reverse [list] =
match list {
[] -> [];
[ x | xs ] -> concat-lists $ (list-reverse $ xs) [x];
}
// Gets a list of successive pairs for a given list
// Example: (list-to-pairs $ [1, 2, 3, 4]) => [[1, 2], [3, 4]]
def list-to-pairs [list] =
match list {
[] -> [];
Expand Down
9 changes: 8 additions & 1 deletion test/examples/pattern_match_advanced.bl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
output: "1\n[4,3,2,1]\n[[1,2],[3,4]]\nTrue\n"
output: "1\n[2,3,4]\n[4,3,2,1]\n[[1,2],[3,4]]\nTrue\n"
---

module Main where
Expand All @@ -10,6 +10,12 @@ def list-head [list] =
[ x | _ ] -> x;
}

def list-tail [list] =
match list {
[] -> error $ "Empty list";
[ _ | xs ] -> xs;
}

def list-reverse [list] =
match list {
[] -> [];
Expand Down Expand Up @@ -37,6 +43,7 @@ def main [] = {
val my-list = [1, 2, 3, 4];

puts-ln $ (to-string $ (list-head $ my-list));
puts-ln $ (to-string $ (list-tail $ my-list));
puts-ln $ (to-string $ (list-reverse $ my-list));
puts-ln $ (to-string $ (list-to-pairs $ my-list));
puts-ln $ (to-string $ (applyTwoEqualsFour? $ (+ $ 2)));
Expand Down
7 changes: 7 additions & 0 deletions test/parser_tests/pattern_match_advanced.bl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def list-head [list] =
[ x | _ ] -> x;
}

def list-tail [list] =
match list {
[] -> error $ "Empty list";
[ _ | xs ] -> xs;
}

def list-reverse [list] =
match list {
[] -> [];
Expand Down Expand Up @@ -33,6 +39,7 @@ def main [] = {
val my-list = [1, 2, 3, 4];

puts-ln $ (to-string $ (list-head $ my-list));
puts-ln $ (to-string $ (list-tail $ my-list));
puts-ln $ (to-string $ (list-reverse $ my-list));
puts-ln $ (to-string $ (list-to-pairs $ my-list));
puts-ln $ (to-string $ (applyTwoEqualsFour? $ (+ $ 2)));
Expand Down

0 comments on commit 0b32106

Please sign in to comment.