Skip to content

Commit 10f6320

Browse files
authored
Update README. (#97)
* Update README. * Update godbolt link. Co-authored-by: Bowen Fu <missing>
1 parent fe25565 commit 10f6320

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

README.md

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![codecov](https://codecov.io/gh/BowenFu/matchit.cpp/branch/main/graph/badge.svg?token=G5B0RE6THD)](https://codecov.io/gh/BowenFu/matchit.cpp)
1717

1818
[badge.godbolt]: https://img.shields.io/badge/try-godbolt-blue
19-
[godbolt]: https://godbolt.org/z/8YMr8Kz8j
19+
[godbolt]: https://godbolt.org/z/Psf3qrxW4
2020

2121
## Features
2222

@@ -165,7 +165,7 @@ constexpr int32_t factorial(int32_t n)
165165
using namespace matchit;
166166
assert(n >= 0);
167167
return match(n)(
168-
pattern | 0 = expr(1),
168+
pattern | 0 = 1,
169169
pattern | _ = [n] { return n * factorial(n - 1); }
170170
);
171171
}
@@ -186,7 +186,7 @@ This is a function call and will return some value returned by handlers. The ret
186186
When handlers return values, the patterns must be exhaustive. A runtime error will happen if all patterns do not get matched.
187187
It is not an error if handlers' return types are all void.
188188

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;}`.
190190

191191
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.
192192

@@ -220,8 +220,8 @@ constexpr bool contains(Map const& map, Key const& key)
220220
{
221221
using namespace matchit;
222222
return match(map.find(key))(
223-
pattern | map.end() = expr(false),
224-
pattern | _ = expr(true)
223+
pattern | map.end() = false,
224+
pattern | _ = true
225225
);
226226
}
227227
```
@@ -234,8 +234,9 @@ We can use **Predicate Pattern** to put some restrictions on the value to be mat
234234
constexpr double relu(double value)
235235
{
236236
return match(value)(
237-
pattern | (_ >= 0) = expr(value),
238-
pattern | _ = expr(0));
237+
pattern | (_ >= 0) = value,
238+
pattern | _ = 0
239+
);
239240
}
240241

241242
static_assert(relu(5) == 5);
@@ -253,8 +254,8 @@ constexpr bool isValid(int32_t n)
253254
{
254255
using namespace matchit;
255256
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
258259
);
259260
}
260261
@@ -280,8 +281,8 @@ constexpr bool isLarge(double value)
280281
{
281282
using namespace matchit;
282283
return match(value)(
283-
pattern | app(_ * _, _ > 1000) = expr(true),
284-
pattern | _ = expr(false)
284+
pattern | app(_ * _, _ > 1000) = true,
285+
pattern | _ = false
285286
);
286287
}
287288
@@ -307,7 +308,7 @@ bool checkAndlogLarge(double value)
307308
pattern | app(_ * _, s.at(_ > 1000)) = [&] {
308309
std::cout << value << "^2 = " << *s << " > 1000!" << std::endl;
309310
return true; },
310-
pattern | _ = expr(false));
311+
pattern | _ = false);
311312
}
312313
```
313314
@@ -330,8 +331,8 @@ constexpr bool symmetric(std::array<int32_t, 5> const& arr)
330331
using namespace matchit;
331332
Id<int32_t> i, j;
332333
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
335336
);
336337
}
337338
@@ -383,8 +384,8 @@ constexpr auto dsByMember(DummyStruct const&v)
383384
constexpr auto dsA = dsVia(&DummyStruct::size, &DummyStruct::name);
384385
Id<char const*> name;
385386
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"
388389
);
389390
};
390391
@@ -411,8 +412,8 @@ constexpr bool sumIs(std::array<int32_t, 2> const& arr, int32_t s)
411412
using namespace matchit;
412413
Id<int32_t> i, j;
413414
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);
416417
}
417418

418419
static_assert(sumIs(std::array<int32_t, 2>{5, 6}, 11));
@@ -435,10 +436,10 @@ constexpr int32_t detectTuplePattern(Tuple const& tuple)
435436
using namespace matchit;
436437
return match(tuple)
437438
(
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
442443
);
443444
}
444445
@@ -457,8 +458,8 @@ constexpr bool recursiveSymmetric(Range const &range)
457458
Id<SubrangeT<Range const>> subrange;
458459
return match(range)(
459460
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
462463
);
463464
```
464465
@@ -485,7 +486,7 @@ constexpr auto square(std::optional<T> const& t)
485486
Id<T> id;
486487
return match(t)(
487488
pattern | some(id) = id * id,
488-
pattern | none = expr(0));
489+
pattern | none = 0);
489490
}
490491
constexpr auto x = std::make_optional(5);
491492
static_assert(square(x) == 25);
@@ -524,8 +525,8 @@ constexpr auto getClassName(T const& v)
524525
{
525526
using namespace matchit;
526527
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"
529530
);
530531
}
531532

@@ -546,8 +547,8 @@ struct Square : Shape {};
546547
auto getClassName(Shape const &s)
547548
{
548549
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"
551552
);
552553
}
553554
```
@@ -610,9 +611,9 @@ int32_t staticCastAs(Num const& input)
610611
{
611612
using namespace matchit;
612613
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);
616617
}
617618

618619
int32_t main()
@@ -712,6 +713,15 @@ If you are interested in `match(it)`, you may also be interested in [hspp](https
712713

713714
Please star the repo, share the repo, or sponsor one dollar to let me know this library matters.
714715

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+
715725
## Sponsor(s)
716726

717727
Thanks to @[e-dant](https://github.com/e-dant) for sponsoring this project.

0 commit comments

Comments
 (0)