https://github.com/phmatray/monads
Read my post on LinkedIn: https://www.linkedin.com/pulse/monads-c-rigorous-exploration-practical-examples-philippe-matray--gm7qe/
https://github.com/phmatray/monads
Last synced: 10 months ago
JSON representation
Read my post on LinkedIn: https://www.linkedin.com/pulse/monads-c-rigorous-exploration-practical-examples-philippe-matray--gm7qe/
- Host: GitHub
- URL: https://github.com/phmatray/monads
- Owner: phmatray
- Created: 2024-11-08T15:25:57.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-08T19:56:54.000Z (about 1 year ago)
- Last Synced: 2025-02-01T07:27:53.212Z (12 months ago)
- Language: C#
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Monads in C#
## Overview
Welcome to the **Monads in C#** project! 🎉 This repository provides a practical example of how monad-like patterns can be implemented in C# to enhance code quality, modularity, and side effect management. If you've heard about monads in functional programming and wondered how they could benefit your C# projects, you're in the right place.
## What Are Monads?
Monads are a powerful concept from functional programming that helps manage transformations and side effects in a clean and predictable way. By encapsulating values and their associated operations, monads allow for clear and composable code—making even complex workflows more maintainable. In C#, we can adapt monad-like patterns to achieve similar results.
## Features
- **Transformations**: Apply a series of operations to a value while keeping your code organized.
- **Side Effect Management**: Manage logs or other side effects through each transformation without cluttering your logic.
- **Modularity**: Break down operations into composable units that can be easily tested and reused.
## Example
The code in this repository demonstrates how to implement a simple monad-like wrapper, `NumberWithLogs`, that allows you to:
1. **Wrap a Value**: Start by wrapping an initial value to create a `NumberWithLogs` instance.
2. **Apply Transformations**: Use functions like `AddOne`, `Square`, or `MultiplyByThree` to transform the value while collecting logs.
3. **Chaining Operations**: Sequentially chain multiple transformations to achieve your desired result.
Here's a brief look at the example:
```csharp
var result = RunWithLogsMultiple(WrapWithLogs(5), AddOne, Square, MultiplyByThree);
PrintResult(result);
```
In this example, we start with the value `5`, add `1`, square the result, and then multiply by `3`, all while maintaining a clear log of each step.
## How to Use
1. **Clone the Repository**
```
git clone https://github.com/yourusername/monads-in-csharp.git
```
2. **Open the Project**
Open the project in your favorite C# editor, such as Visual Studio or VS Code.
3. **Run the Example**
Build and run the project to see how the transformations and logs work together.
## To Go Further
If you'd like to explore more about monads and their use in functional programming, check out these resources:
- [Monads in C#: A Rigorous Exploration with Practical Examples (LinkedIn)](https://www.linkedin.com/pulse/monads-c-rigorous-exploration-practical-examples-philippe-matray--gm7qe/)
- [The Absolute Best Intro to Monads For Software Engineers (YouTube)](https://www.youtube.com/watch?v=C2w45qRc3aU)
- [Monad (functional programming) on Wikipedia](https://en.wikipedia.org/wiki/Monad_(functional_programming))
## Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to enhance the project, add more examples, or improve documentation.
## License
This project is licensed under the MIT License. See the `LICENSE` file for more details.
## Contact
If you have any questions or suggestions, feel free to reach out or create an issue in the repository.
Happy coding! 🚀