diff --git a/docs/README.md b/docs/README.md index 7b07807..e196c4c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 } ``` diff --git a/docs/pages/language-features.md b/docs/pages/language-features.md index d450dc5..d0bc295 100644 --- a/docs/pages/language-features.md +++ b/docs/pages/language-features.md @@ -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 } ```