Skip to content

fmt-Println-MKO/fp_kata

Repository files navigation

codecov

codecov

Functional Programming in Go - Demo Project

Functional Programming Gopher

This project serves as a demo application showcasing how to apply functional programming patterns in Go. While Go traditionally follows an imperative, non-functional style, this project explores how concepts like monads, chaining, and folds can be utilized to write cleaner and more expressive code in Go.

The main branch contains the classic, non-functional Go implementation. Each step towards functional programming is available in separate branches, showing a gradual transformation of the codebase.


Table of Contents

  1. Project Overview
  2. Demo Steps
  3. Setup and Installation
  4. Running the Application
  5. Generating Code
  6. Testing and Coverage
  7. Project Structure
  8. Further Reading
  9. Contributions
  10. License

Project Overview

This project demonstrates the evolution from non-functional Go to functional Go programming style. It follows a step-by-step approach, with each step available as a separate branch for clarity. The transformations include:

  1. Classic Non-Functional Style (Main Branch): The baseline implementation using Go's traditional imperative style.
  2. Step 1 – Applying Monad: The OrdersStorage is refactored to leverage a Monad, encapsulating state and behavior in a composable way.
  3. Step 2 – Chaining Calls: Functional chaining is introduced, improving readability and reducing boilerplate.
  4. Step 3 – Simplification with Fold: Code is further simplified by introducing the fold operation, which consolidates transformations.
  5. Step 4 – Full Functional Refactor: The remaining parts of the codebase are refactored to fully adopt functional programming patterns.

For detailed explanations of each step and additional instructions, visit the project's GitHub page.


Demo Steps

Branches Overview:

  • Main Branch: Classic non-functional implementation.
  • Step 1 Branch: Introduction of Monads with OrdersStorage.
  • Step 2 Branch: Functional chaining of calls and operations.
  • Step 3 Branch: Code simplification using the fold operation.
  • Step 4 Branch: Full adoption of functional programming patterns.

Setup and Installation

Prerequisites:

  1. Go SDK 1.23.5: Ensure Go is installed. Download it here.
  2. Mockery: Install Mockery for generating mocks:
    go install github.com/vektra/mockery/v2@latest
  3. Wire: Install Wire for dependency injection:
    go install github.com/google/wire/cmd/wire@latest

Running the Application

To run the demo application, navigate to the desired branch and use the following command:

go run ./cmd

Generating Code

1. Generate Dependency Injection

Use Wire to auto-generate dependency injection code:

cd internal/app && go generate ./

2. Generate Mocks

Generate mock implementations for interfaces with Mockery:

mockery --all --output=mocks

Testing and Coverage

Run tests and generate a coverage report to ensure the codebase's quality:

# Run tests and generate coverage data
go test -cover -coverpkg=$(go list ./... | grep -v '/mocks' | grep -v '/cmd' | tr '\n' ',') ./... -coverprofile=coverage.out

# View the coverage report in a browser
go tool cover -html=coverage.out

Project Structure

├── cmd/                     # Entry point for the application
├── internal/                # Application's internal implementation
│   ├── app/                 # Core application logic
│   ├── domain/              # Business entities and logic
│   ├── mocks/               # Auto-generated mocks for testing
│   └── ...                  # Additional packages
└── tests/                   # Unit and integration tests

Further Reading

For step-by-step explanations, detailed instructions, and discussions on functional programming in Go, please refer to the official project documentation.


Contributions

As this is a demo application, contributions are welcome! If you’d like to contribute:

  1. Fork the repository.
  2. Create a feature branch:
    git checkout -b feature/<feature-name>
  3. Commit your changes:
    git commit -am 'Add new feature: <feature-description>'
  4. Push to the branch:
    git push origin feature/<feature-name>
  5. Open a Pull Request.

License

This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).

Under this license:

  • You are free to use, modify, and distribute this project.
  • You must retain attribution to the original author in all distributed copies or modified code.
  • Modifications to the source code must also be made available under the same license.

For more details, see the LICENSE file or read the full text of the MPL-2.0 here.

About

how to apply functional programming paradigm in GO (WIP)

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages