Skip to content

Commit

Permalink
#33: Documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jaccomoc committed Jan 9, 2024
1 parent 14abb32 commit b0a9528
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 8 additions & 9 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ stream(nextLine).filter{ /^# /r }
Another example showing Jactl's pattern matching with destructuring:
```groovy
switch (x) {
/Type:(\w+), Cost:(\d+)/n -> [$1, $2] // regex match with capture vars
[1,*] -> 'matched' // list whose first element is 1
[_,_] -> 'matched' // list with 2 elements
[int,String,_] -> 'matched' // 3 element list. 1st is an int, 2nd is a String
[a,_,a] -> a * 2 // 3 element list. 1st and last elements the same
[a,*,a] if a < 10 -> a * 3 // list with at least 2 elements. 1st and last the same and < 10
[a,${2*a},${3*a}] -> a // match if list is of form [2,4,6] or [3,6,9] etc
[a,b,c,d] -> a+b+c+d // 4 element list
[[2],[a],[_,b],[_,_,c]] -> "a=$a,b=$b,c=$c" // List of lists with a,b,c bound to different parts
/X=(\d+),Y=(\d+)/n -> $1 + $2 // regex match with capture vars
[1,*] -> 'matched' // list whose first element is 1
[_,_] -> 'matched' // list with 2 elements
[int,String,_] -> 'matched' // 3 element list. 1st is an int, 2nd is a String
[a,_,a] -> a * 2 // 1st and last elements the same in 3 element list
[a,*,a] if a < 10 -> a * 3 // list with at least 2 elements. 1st and last the same and < 10
[a,${2*a},${3*a}] -> a // match if list is of form [2,4,6] or [3,6,9] etc
[a,b,c,d] -> a+b+c+d // 4 element list
}
```

Expand Down
17 changes: 8 additions & 9 deletions docs/pages/language-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,14 @@ Jactl provides `switch` expressions that can match on literal values but can als
match on the structure of the data and bind variables to different parts of the structure:
```groovy
switch (x) {
/Type:(\w+), Cost:(\d+)/n -> [$1, $2] // regex match with capture vars
[1,*] -> 'matched' // list whose first element is 1
[_,_] -> 'matched' // list with 2 elements
[int,String,_] -> 'matched' // 3 element list. 1st is an int, 2nd is a String
[a,_,a] -> a * 2 // 3 element list. 1st and last elements the same
[a,*,a] if a < 10 -> a * 3 // list with at least 2 elements. 1st and last the same and < 10
[a,${2*a},${3*a}] -> a // match if list is of form [2,4,6] or [3,6,9] etc
[a,b,c,d] -> a+b+c+d // 4 element list
[[2],[a],[_,b],[_,_,c]] -> "a=$a,b=$b,c=$c" // List of lists with a,b,c bound to different parts
/X=(\d+),Y=(\d+)/n -> $1 + $2 // regex match with capture vars
[1,*] -> 'matched' // list whose first element is 1
[_,_] -> 'matched' // list with 2 elements
[int,String,_] -> 'matched' // 3 element list. 1st is an int, 2nd is a String
[a,_,a] -> a * 2 // 1st and last elements the same in 3 element list
[a,*,a] if a < 10 -> a * 3 // list with at least 2 elements. 1st and last the same and < 10
[a,${2*a},${3*a}] -> a // match if list is of form [2,4,6] or [3,6,9] etc
[a,b,c,d] -> a+b+c+d // 4 element list
}
```

Expand Down

0 comments on commit b0a9528

Please sign in to comment.