Skip to content

KDSbami/meowBe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

MeowBe Monad

Overview

The MeowBe class implements a monadic type that simulates Schrödinger's Cat in a box, represented as either meowable (alive) or not meowable (dead). This template class is designed to manage the lifecycle of objects safely, handling both copy and move semantics efficiently.

Features

  • Encapsulation of "nullable" or "optional" objects.
  • Supports copy and move semantics for seamless usage in different contexts.

Class Declaration

template <typename Cat>
class MeowBe
  • Cat: The type of the cat in the MeowBe box. Could be any cat from a Persian cat to a Danger kitty (can be any C++ type).

Data Members:

  • bool isMeowable: Indicates whether the cat (or object) is "meowable" (i.e., meowing and well).
  • Cat kuro: The cat itself being managed. (Kuro here is a Sri-lankan gangmember of the clan.)

Constructors

1. Default Constructor

MeowBe();
  • Creates a box with no meow in it (non-meowable).

2. Parameterized Constructor

MeowBe(const Cat &yourCat);
  • Creates a box with a meowable cat (yourcat).

3. Copy Constructor

MeowBe(MeowBe &randomBox);
  • Copies the contents of another MeowBe instance.

4. Move Constructor

MeowBe(MeowBe&& randomBox) noexcept;
  • Transfers ownership of the cat from randomBox to the new instance, leaving randomBox empty.

Destructor

Destructor

~MeowBe();
  • Destroys the managed box and releasing any held cats from the memory.

Assignment Operators

1. Copy Assignment

MeowBe& operator=(const MeowBe& otherBox) noexcept;
  • Copies the cat of another MeowBe box in a new box while avoiding the new cat going in the old box.

2. Move Assignment

MeowBe& operator=(MeowBe&& otherBox) noexcept;
  • Moves the contents of another MeowBe box, transferring cat and its ownership to a new box and destroying the original empty box.

Member Functions

isMeowing()

bool isMeowing() const;
  • Returns true if the cat is preset and meowing.

isNotMeowing()

bool isNotMeowing() const;
  • Returns true if the box is empty.

pure()

static MeowBe<RandomKitty> pure(const RandomKitty& neko);
  • takes a random kitty and puts it in a MeowBe box and returns the static box

bind()

MeowBe<UpgradedCat> bind(std::function<MeowBe<UpgradedCat>(Cat)> f);

Basically takes a cat out of the box, pets/feeds/doesWhateverYouWantWithTheCat and expects that you are responsible for putting the cat in the box and return.

  • Applies a petting/feeding function f to the cat (if present), returning a new MeowBe box containing the upgraded cat.

map()

MeowBe<UpgradedCat> map(std::function<UpgradedCat(Cat)> f);

Basically takes a cat out of the box, pets/feeds/doesWhateverYouWantWithTheCat and puts the new cat in the box and returns.

  • Applies a petting/feeding function f to the cat (if present), returning a new MeowBe box containing the upgraded cat.

fromMeowBe()

Cat fromMeowBe(Cat someRandomCat);
  • Returns the contained cat if present, otherwise returns a provided fallback someRandomCat.

Non Member Functions

meow()

MeowBe<RandomCat> meow(const RandomCat& randomKuro);
  • Simply a constructor like function to give a randomCat a box. Can be used like just().

noMeow()

MeowBe<RandomCat> noMeow();
  • Simply a constructor like function to give an empty box with a specific type. Can be used like nothing().

Example Usage

Basic Example:

#include "meowBe.hpp"
#include <iostream>

int main() {
    auto value = meow<int>(42);
    auto value2 = noMeow<int>();
    std::cout << "Value: " << value.fromMeowBe(0) << std::endl;
    std::cout << "Value2: " << value2.fromMeowBe(0) << std::endl;
    return 0;
}

Output:

Value: 42
Value2: 0

Requirements

  • C++17 or higher (for std::function and move semantics).

Notes

  • The class uses manual placement new and destructor calls for flexibility.
  • It implements the Monad pattern for functional programming style transformations.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages