|
@@ -134,7 +134,164 @@ int my_func(int x) {
|
|
|
note that compiler can optimise few comparison conditions for ```switch``` better than that in ```if .. else```.
|
|
|
|
|
|
|
|
|
-### 2.4.1 pattern matching examples
|
|
|
+### 2.4.1 pattern matching on strings
|
|
|
+There are Pattern Matching on strings/binaries in Erlang, that looks like:
|
|
|
+```
|
|
|
+my_func(<<"test", Rest/binary>>) -> true;
|
|
|
+my_func(_) -> false.
|
|
|
+
|
|
|
+% or
|
|
|
+my_func("test" ++ Rest) -> true;
|
|
|
+my_func(_) -> false.
|
|
|
+```
|
|
|
+similar code in D - dlang:
|
|
|
+```
|
|
|
+bool my_func(string str){
|
|
|
+ if(str.length >= 4 && str[0..4] == "test"){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+// also we can get rest
|
|
|
+
|
|
|
+import std.stdio;
|
|
|
+
|
|
|
+bool my_func(string str, out string rest){
|
|
|
+ if(str.length >= 4 && str[0..4] == "test"){
|
|
|
+ rest = str[4..$];
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ rest = str;
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+void main(){
|
|
|
+ string rest;
|
|
|
+ writeln(my_func("test123", rest)); // true, rest = "123"
|
|
|
+ writeln(rest); // "123"
|
|
|
+ writeln(my_func("example", rest)); // false, rest = "example"
|
|
|
+}
|
|
|
+
|
|
|
+// also we can to match from string ending
|
|
|
+bool has_end_test(string str){
|
|
|
+ if(str.length >= 4){
|
|
|
+ if(str[$-4 .. $] == "test"){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+```
|
|
|
+In My Humble Opinion be better to make this syntax similar (laconic) as in Erlang.
|
|
|
+
|
|
|
+
|
|
|
+### 2.4.2 pattern matching examples
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+bool = string str
|
|
|
+has_begin_test "test" ++ _ = true
|
|
|
+has_begin_test _ = false
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+bool has_begin_test(string str){
|
|
|
+ if(str.length >= 4 && str[0..4] == "test"){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+bool = string str, out string rest
|
|
|
+has_begin_test "test" ++ rest = true
|
|
|
+has_begin_test _ = false
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+// import std.stdio;
|
|
|
+
|
|
|
+bool has_begin_test(string str, out string rest){
|
|
|
+ if(str.length >= 4 && str[0..4] == "test"){
|
|
|
+ rest = str[4..$];
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ rest = str;
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+void main(){
|
|
|
+ string rest;
|
|
|
+
|
|
|
+ writeln(has_begin_test("test123", rest)); // true, rest = "123"
|
|
|
+ writeln(rest); // "123"
|
|
|
+
|
|
|
+ writeln(has_begin_test("test", rest)); // true, rest = ""
|
|
|
+ writeln(rest); // ""
|
|
|
+
|
|
|
+ writeln(has_begin_test("example", rest)); // false, rest = "example"
|
|
|
+ writeln(rest); // "example"
|
|
|
+}
|
|
|
+*/
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+bool = string str
|
|
|
+has_end_test _ ++ "test" = true
|
|
|
+has_end_test _ = false
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+// match from string ending
|
|
|
+bool has_end_test(string str){
|
|
|
+ if(str.length >= 4){
|
|
|
+ if(str[$-4 .. $] == "test"){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+bool = string str, out string rest
|
|
|
+has_end_test rest ++ "test" = true
|
|
|
+has_end_test _ = false
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+// import std.stdio;
|
|
|
+
|
|
|
+bool has_end_test(string str, out string rest){
|
|
|
+ if(str.length >= 4 && str[$-4 .. $] == "test"){
|
|
|
+ rest = str[0 .. $-4];
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ rest = str;
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+void main() {
|
|
|
+ string rest;
|
|
|
+
|
|
|
+ writeln(has_end_test("hellotest", rest)); // true, rest = "hello"
|
|
|
+ writeln(rest); // "hello"
|
|
|
+
|
|
|
+ writeln(has_end_test("example", rest)); // false, rest = "example"
|
|
|
+ writeln(rest); // "example"
|
|
|
+
|
|
|
+ writeln(has_end_test("test", rest)); // true, rest = ""
|
|
|
+ writeln(rest); // ""
|
|
|
+}
|
|
|
+*/
|
|
|
+```
|
|
|
+
|
|
|
```
|
|
|
// left = function type, right = arguments type
|
|
|
int = int x
|