16
16
[ ![ codecov] ( https://codecov.io/gh/BowenFu/matchit.cpp/branch/main/graph/badge.svg?token=G5B0RE6THD )] ( https://codecov.io/gh/BowenFu/matchit.cpp )
17
17
18
18
[ badge.godbolt ] : https://img.shields.io/badge/try-godbolt-blue
19
- [ godbolt ] : https://godbolt.org/z/8YMr8Kz8j
19
+ [ godbolt ] : https://godbolt.org/z/Psf3qrxW4
20
20
21
21
## Features
22
22
@@ -165,7 +165,7 @@ constexpr int32_t factorial(int32_t n)
165
165
using namespace matchit;
166
166
assert(n >= 0);
167
167
return match(n)(
168
- pattern | 0 = expr(1) ,
168
+ pattern | 0 = 1 ,
169
169
pattern | _ = [n] { return n * factorial(n - 1); }
170
170
);
171
171
}
@@ -186,7 +186,7 @@ This is a function call and will return some value returned by handlers. The ret
186
186
When handlers return values, the patterns must be exhaustive. A runtime error will happen if all patterns do not get matched.
187
187
It is not an error if handlers' return types are all void.
188
188
189
- `expr` in the above sample is a helper function that can be used to generate a nullary function that returns a value . `expr(1) ` is equivalent to ` [ ] {return 1;}`. It can be useful for short functions .
189
+ The handler can also be a value or an Id variable . `1 ` is equivalent to `[]{return 1;}`.
190
190
191
191
The wildcard ` _ ` will match any values. It is a common practice to always use it as the last pattern, playing the same role in our library as ` default case ` does for ` switch ` statements, to avoid case escaping.
192
192
@@ -220,8 +220,8 @@ constexpr bool contains(Map const& map, Key const& key)
220
220
{
221
221
using namespace matchit;
222
222
return match(map.find(key))(
223
- pattern | map.end() = expr( false) ,
224
- pattern | _ = expr( true)
223
+ pattern | map.end() = false,
224
+ pattern | _ = true
225
225
);
226
226
}
227
227
```
@@ -234,8 +234,9 @@ We can use **Predicate Pattern** to put some restrictions on the value to be mat
234
234
constexpr double relu (double value)
235
235
{
236
236
return match(value)(
237
- pattern | (_ >= 0) = expr(value),
238
- pattern | _ = expr(0));
237
+ pattern | (_ >= 0) = value,
238
+ pattern | _ = 0
239
+ );
239
240
}
240
241
241
242
static_assert(relu(5) == 5);
@@ -253,8 +254,8 @@ constexpr bool isValid(int32_t n)
253
254
{
254
255
using namespace matchit;
255
256
return match(n)(
256
- pattern | or_(1, 3, 5) = expr( true) ,
257
- pattern | _ = expr( false)
257
+ pattern | or_(1, 3, 5) = true,
258
+ pattern | _ = false
258
259
);
259
260
}
260
261
@@ -280,8 +281,8 @@ constexpr bool isLarge(double value)
280
281
{
281
282
using namespace matchit;
282
283
return match(value)(
283
- pattern | app(_ * _, _ > 1000) = expr( true) ,
284
- pattern | _ = expr( false)
284
+ pattern | app(_ * _, _ > 1000) = true,
285
+ pattern | _ = false
285
286
);
286
287
}
287
288
@@ -307,7 +308,7 @@ bool checkAndlogLarge(double value)
307
308
pattern | app(_ * _ , s.at(_ > 1000)) = [ &] {
308
309
std::cout << value << "^2 = " << * s << " > 1000!" << std::endl;
309
310
return true; },
310
- pattern | _ = expr( false) );
311
+ pattern | _ = false);
311
312
}
312
313
```
313
314
@@ -330,8 +331,8 @@ constexpr bool symmetric(std::array<int32_t, 5> const& arr)
330
331
using namespace matchit;
331
332
Id<int32_t> i, j;
332
333
return match(arr)(
333
- pattern | ds(i, j, _, j, i) = expr( true) ,
334
- pattern | _ = expr( false)
334
+ pattern | ds(i, j, _, j, i) = true,
335
+ pattern | _ = false
335
336
);
336
337
}
337
338
@@ -383,8 +384,8 @@ constexpr auto dsByMember(DummyStruct const&v)
383
384
constexpr auto dsA = dsVia(&DummyStruct::size, &DummyStruct::name);
384
385
Id<char const*> name;
385
386
return match(v)(
386
- pattern | dsA(2, name) = expr( name) ,
387
- pattern | _ = expr( "not matched")
387
+ pattern | dsA(2, name) = name,
388
+ pattern | _ = "not matched"
388
389
);
389
390
};
390
391
@@ -411,8 +412,8 @@ constexpr bool sumIs(std::array<int32_t, 2> const& arr, int32_t s)
411
412
using namespace matchit;
412
413
Id<int32_t> i, j;
413
414
return match(arr)(
414
- pattern | ds(i, j) | when(i + j == s) = expr( true) ,
415
- pattern | _ = expr( false) );
415
+ pattern | ds(i, j) | when(i + j == s) = true,
416
+ pattern | _ = false);
416
417
}
417
418
418
419
static_assert(sumIs(std::array<int32_t, 2>{5, 6}, 11));
@@ -435,10 +436,10 @@ constexpr int32_t detectTuplePattern(Tuple const& tuple)
435
436
using namespace matchit;
436
437
return match(tuple)
437
438
(
438
- pattern | ds(2, ooo, 2) = expr(4) ,
439
- pattern | ds(2, ooo ) = expr(3) ,
440
- pattern | ds(ooo, 2 ) = expr(2) ,
441
- pattern | ds(ooo ) = expr(1)
439
+ pattern | ds(2, ooo, 2) = 4 ,
440
+ pattern | ds(2, ooo ) = 3 ,
441
+ pattern | ds(ooo, 2 ) = 2 ,
442
+ pattern | ds(ooo ) = 1
442
443
);
443
444
}
444
445
@@ -457,8 +458,8 @@ constexpr bool recursiveSymmetric(Range const &range)
457
458
Id<SubrangeT<Range const >> subrange;
458
459
return match(range)(
459
460
pattern | ds(i, subrange.at(ooo), i) = [ &] { return recursiveSymmetric(* subrange); },
460
- pattern | ds(_ , ooo, _ ) = expr( false) ,
461
- pattern | _ = expr( true)
461
+ pattern | ds(_ , ooo, _ ) = false,
462
+ pattern | _ = true
462
463
);
463
464
```
464
465
@@ -485,7 +486,7 @@ constexpr auto square(std::optional<T> const& t)
485
486
Id<T> id;
486
487
return match(t)(
487
488
pattern | some(id) = id * id,
488
- pattern | none = expr(0) );
489
+ pattern | none = 0 );
489
490
}
490
491
constexpr auto x = std::make_optional(5);
491
492
static_assert(square(x) == 25);
@@ -524,8 +525,8 @@ constexpr auto getClassName(T const& v)
524
525
{
525
526
using namespace matchit;
526
527
return match(v)(
527
- pattern | as<char const* >(_ ) = expr( "chars") ,
528
- pattern | as<int32_t>(_ ) = expr( "int32_t")
528
+ pattern | as<char const* >(_ ) = "chars",
529
+ pattern | as<int32_t>(_ ) = "int32_t"
529
530
);
530
531
}
531
532
@@ -546,8 +547,8 @@ struct Square : Shape {};
546
547
auto getClassName(Shape const &s)
547
548
{
548
549
return match(s)(
549
- pattern | as<Circle>(_) = expr( "Circle") ,
550
- pattern | as<Square>(_) = expr( "Square")
550
+ pattern | as<Circle>(_) = "Circle",
551
+ pattern | as<Square>(_) = "Square"
551
552
);
552
553
}
553
554
```
@@ -610,9 +611,9 @@ int32_t staticCastAs(Num const& input)
610
611
{
611
612
using namespace matchit;
612
613
return match(input)(
613
- pattern | as<One >(_ ) = expr(1) ,
614
- pattern | kind< Kind::kTWO > = expr(2) ,
615
- pattern | _ = expr(3) );
614
+ pattern | as<One >(_ ) = 1 ,
615
+ pattern | kind< Kind::kTWO > = 2 ,
616
+ pattern | _ = 3 );
616
617
}
617
618
618
619
int32_t main()
@@ -712,6 +713,15 @@ If you are interested in `match(it)`, you may also be interested in [hspp](https
712
713
713
714
Please star the repo, share the repo, or sponsor one dollar to let me know this library matters.
714
715
716
+ ## Contributor(s)
717
+
718
+ Thanks to all for contributing code and sending in bugs.
719
+
720
+ In particular, thanks to the following contributors:
721
+
722
+ Hugo Etchegoyen (@[ hugoetchegoyen] ( https://github.com/hugoetchegoyen ) )
723
+
724
+
715
725
## Sponsor(s)
716
726
717
727
Thanks to @[ e-dant] ( https://github.com/e-dant ) for sponsoring this project.
0 commit comments