From 63f9e171013bf6e1e40f47181c5178e5739d7ca3 Mon Sep 17 00:00:00 2001 From: LeeeeT Date: Tue, 18 Jun 2024 08:47:18 -0800 Subject: [PATCH] Add an example of generating pythagorean triples using monads --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 51c1d89..8cd2385 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,24 @@ main = \ io.run(main) ``` +Finding pythagorean triples (analogue to list comprehension): + +```python +from felis.currying import uncurry +from felis.list import bind, guard, identity, range, then + +pythags = \ + uncurry(bind)(range(1)(20), lambda z: + uncurry(bind)(range(1)(z), lambda x: + uncurry(bind)(range(x)(z), lambda y: + uncurry(then)(guard(x**2 + y**2 == z**2), + identity((x, y, z)), +)))) + +print(pythags) +# [(3, 4, 5), (6, 8, 10), (5, 12, 13), (9, 12, 15), (8, 15, 17)] +``` + That's all monads, btw. 🐈 [docs]: https://felis.LeeeeT.dev