[
  {
    "path": ".github/workflows/rust.yml",
    "content": "name: CI\n\non:\n  push:\n    branches: [master]\n  pull_request:\n    branches: [master]\n\nenv:\n  RUST_BACKTRACE: 1\n  CARGO_TERM_COLOR: always\n\njobs:\n  fmt:\n    name: Format Check\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: dtolnay/rust-toolchain@stable\n        with:\n          components: rustfmt\n      - name: Check formatting\n        run: cargo fmt --all -- --check\n\n  clippy:\n    name: Clippy (Lint)\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: dtolnay/rust-toolchain@stable\n        with:\n          components: clippy\n      - name: Run clippy\n        run: cargo clippy --all-targets --all-features -- -D warnings\n        # -D warnings makes lint warnings fail CI (fixes Issue #34)\n\n  test:\n    name: Test Suite\n    runs-on: ${{ matrix.os }}\n    strategy:\n      matrix:\n        os: [ubuntu-latest, macos-latest, windows-latest]\n    steps:\n      - uses: actions/checkout@v4\n      - uses: dtolnay/rust-toolchain@stable\n      - name: Run tests\n        run: cargo test --all-features --workspace\n      - name: Run doc tests\n        run: cargo test --doc --workspace\n"
  },
  {
    "path": ".gitignore",
    "content": "/target\n**/*.rs.bk"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Citizen Code of Conduct\n\n## 1. Purpose\n\nA primary goal of PureRust is to be inclusive to the largest number of contributors, with the most varied and diverse backgrounds possible. As such, we are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual orientation, ability, ethnicity, socioeconomic status, and religion (or lack thereof).\n\nThis code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior.\n\nWe invite all those who participate in PureRust to help us create safe and positive experiences for everyone.\n\n## 2. Open [Source/Culture/Tech] Citizenship\n\nA supplemental goal of this Code of Conduct is to increase open [source/culture/tech] citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community.\n\nCommunities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society.\n\nIf you see someone who is making an extra effort to ensure our community is welcoming, friendly, and encourages all participants to contribute to the fullest extent, we want to know.\n\n## 3. Expected Behavior\n\nThe following behaviors are expected and requested of all community members:\n\n * Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community.\n * Exercise consideration and respect in your speech and actions.\n * Attempt collaboration before conflict.\n * Refrain from demeaning, discriminatory, or harassing behavior and speech.\n * Be mindful of your surroundings and of your fellow participants. Alert community leaders if you notice a dangerous situation, someone in distress, or violations of this Code of Conduct, even if they seem inconsequential.\n * Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations.\n\n## 4. Unacceptable Behavior\n\nThe following behaviors are considered harassment and are unacceptable within our community:\n\n * Violence, threats of violence or violent language directed against another person.\n * Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language.\n * Posting or displaying sexually explicit or violent material.\n * Posting or threatening to post other people's personally identifying information (\"doxing\").\n * Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability.\n * Inappropriate photography or recording.\n * Inappropriate physical contact. You should have someone's consent before touching them.\n * Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances.\n * Deliberate intimidation, stalking or following (online or in person).\n * Advocating for, or encouraging, any of the above behavior.\n * Sustained disruption of community events, including talks and presentations.\n\n## 5. Weapons Policy\n\nNo weapons will be allowed at PureRust events, community spaces, or in other spaces covered by the scope of this Code of Conduct. Weapons include but are not limited to guns, explosives (including fireworks), and large knives such as those used for hunting or display, as well as any other item used for the purpose of causing injury or harm to others. Anyone seen in possession of one of these items will be asked to leave immediately, and will only be allowed to return without the weapon. Community members are further expected to comply with all state and local laws on this matter.\n\n## 6. Consequences of Unacceptable Behavior\n\nUnacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated.\n\nAnyone asked to stop unacceptable behavior is expected to comply immediately.\n\nIf a community member engages in unacceptable behavior, the community organizers may take any action they deem appropriate, up to and including a temporary ban or permanent expulsion from the community without warning (and without refund in the case of a paid event).\n\n## 7. Reporting Guidelines\n\nIf you are subject to or witness unacceptable behavior, or have any other concerns, please notify a community organizer as soon as possible. visualbbasic@gmail.com.\n\n\n\nAdditionally, community organizers are available to help community members engage with local law enforcement or to otherwise help those experiencing unacceptable behavior feel safe. In the context of in-person events, organizers will also provide escorts as desired by the person experiencing distress.\n\n## 8. Addressing Grievances\n\nIf you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify Jason Shin with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies. \n\n\n\n## 9. Scope\n\nWe expect all community participants (contributors, paid or otherwise; sponsors; and other guests) to abide by this Code of Conduct in all community venues--online and in-person--as well as in all one-on-one communications pertaining to community business.\n\nThis code of conduct and its related procedures also applies to unacceptable behavior occurring outside the scope of community activities when such behavior has the potential to adversely affect the safety and well-being of community members.\n\n## 10. Contact info\n\nvisualbbasic@gmail.com\n\n## 11. License and attribution\n\nThe Citizen Code of Conduct is distributed by [Stumptown Syndicate](http://stumptownsyndicate.org) under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/). \n\nPortions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy).\n\n_Revision 2.3. Posted 6 March 2017._\n\n_Revision 2.2. Posted 4 February 2016._\n\n_Revision 2.1. Posted 23 June 2014._\n\n_Revision 2.0, adopted by the [Stumptown Syndicate](http://stumptownsyndicate.org) board on 10 January 2013. Posted 17 March 2013._\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# Contributing to functional-programming-jargon.rs\n\nAll sort of contributions are welcome and there are no complicated rules with it.\nWe appreciate:\n\n- New features\n- Bug fixes\n- Suggestions\n- Ideas\n\n## Issues\n\nFeel free to submit issues, ideas, suggestions and enhancement requests.\n\n## Contributing\n\nPlease refer to each project's style guidelines and guidelines for submitting patches and additions.\nIn general, we follow the \"fork-and-pull\" Git workflow.\n\n1.  **Fork** the repo on GitHub\n2.  **Clone** the project to your own machine\n3.  **Commit** changes to your own branch\n4.  **Push** your work back up to your fork\n5.  Submit a **Pull request** so that we can review your changes\n\nNOTE: Be sure to merge the latest from \"upstream\" before making a pull request!\n\n## Development Environments\n\n| type | version                |\n| ---- | ---------------------- |\n| OS   | Linux, Windows and Mac |\n\nYou will need to install Rust to work on this project. Installation instruction can be found at https://www.rust-lang.org/tools/install.\n\n### Requirements\n\n- **Rust stable** (1.70.0 or later recommended)\n- **No nightly features required** - this project uses only stable Rust features\n\nThe project automatically uses the correct Rust version via `rust-toolchain.toml`.\n\n### Running Tests\n\n```bash\n# Run all tests\ncargo test --workspace\n\n# Run tests for a specific package\ncargo test -p fp-core\n\n# Run with output\ncargo test -- --nocapture\n```\n\n### Code Quality Checks\n\n```bash\n# Format code\ncargo fmt --all\n\n# Check formatting\ncargo fmt --all -- --check\n\n# Run clippy lints\ncargo clippy --all-targets --all-features\n\n# Fix clippy warnings automatically (where possible)\ncargo clippy --all-targets --all-features --fix\n```\n\n## Copyright and Licensing\n\n is an open source project licensed under the MIT license.\n\nfunctional-programming-jargon.rs does not require you to assign the copyright of your contributions, you retain the copyright.\nfunctional-programming-jargon.rs does require that you make your contributions available under the MIT license in order to be\nincluded in the main repo.\n\nIf appropriate, include the MIT license summary at the top of each file along with the copyright info.\nIf you are adding a new file that you wrote, include your name in the copyright notice in the license\nsummary at the top of the file.\n\n## License Summary\n\nYou can copy and paste the MIT license summary from below.\n\n```\nMIT License\n\nCopyright (c) 2026 Jason Shin\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n```\n"
  },
  {
    "path": "Cargo.toml",
    "content": "[workspace]\n\nmembers = [\n  \"fp-examples\",\n  \"fp-core\"\n]\n"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2026 Jason Shin\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "<p align=\"center\"><img src=\"https://i.imgur.com/pqQLDFz.png\" width=\"30%\" /></p>\n\n[![Build Status](https://travis-ci.com/JasonShin/fp-core.rs.svg?branch=master)](https://travis-ci.com/JasonShin/fp-core.rs)\n\nThe project is a library for functional programming in Rust.\n\n* [fp-core.rs](#fp-corers)\n    * [installation](#installation)\n* [functional programming jargon in rust](#functional-programming-jargon-in-rust)\n\n# fp-core.rs\n\nA library for functional programming in Rust.\n\nIt contains purely functional data structures to supplement the functional programming needs alongside\nwith the Rust Standard Library.\n\n## Installation \n\nAdd below line to your Cargo.toml\n\n```rust\nfp-core = \"0.1.9\"\n```\n\nIf you have [Cargo Edit](https://github.com/killercup/cargo-edit) you may simply\n\n```bash\n$ cargo add fp-core\n```\n\n# functional programming jargon in rust\n\nFunctional programming (FP) provides many advantages, and its popularity has been increasing as a result.\nHowever, each programming paradigm comes with its own unique jargon and FP is no exception. By providing a glossary,\nwe hope to make learning FP easier.\n\nWhere applicable, this document uses terms defined in the [Fantasy Land spec](https://github.com/fantasyland/fantasy-land)\nand Rust programming language to give code examples.\n\nThe content of this section was drawn from [Functional Programming Jargon in Javascript](https://github.com/hemanth/functional-programming-jargon) and\nwe sincerely appreciate them for providing the initial baseline.\n\n__Table of Contents__\n<!-- RM(noparent,notop) -->\n\n* [Arity](#arity)\n* [Higher-Order Functions (HOF)](#higher-order-functions-hof)\n* [Closure](#closure)\n* [Partial Application](#partial-application)\n* [Currying](#currying)\n* [Auto Currying](#auto-currying)\n* [Referential Transparency](#referential-transparency)\n* [Lambda](#lambda)\n* [Lambda Calculus](#lambda-calculus)\n* [Purity](#purity)\n* [Side effects](#side-effects)\n* [Idempotent](#idempotent)\n* [Function Composition](#function-composition)\n* [Continuation](#continuation)\n* [Point-Free Style](#point-free-style)\n* [Predicate](#predicate)\n* [Contracts](#contracts)\n* [Category](#category)\n* [Value](#value)\n* [Constant](#constant)\n* [Variance](#variance)\n* [Higher Kinded Type](#higher-kinded-type-hkt)\n* [Functor](#functor)\n* [Pointed Functor](#pointed-functor)\n* [Lifting](#lifting)\n* [Equational Reasoning](#equational-reasoning)\n* [Monoid](#monoid)\n* [Monad](#monad)\n* [Comonad](#comonad)\n* [Applicative](#applicative)\n* [Morphism](#morphism)\n  * [Endomorphism](#endomorphism)\n  * [Isomorphism](#isomorphism)\n  * [Homomorphism](#homomorphism)\n  * [Catamorphism](#catamorphism)\n  * [Hylomorphism](#hylomorphism)\n  * [Anamorphism](#anamorphism)\n* [Setoid](#setoid)\n* [Ord](#ord)\n* [Semigroup](#semigroup)\n* [Foldable](#foldable)\n* [Lens](#lens)\n* [Type Signature](#type-signature)\n* [Algebraic data type](#algebraic-data-type)\n  * [Sum Type](#sum-type)\n  * [Product Type](#product-type)\n* [Option](#option)\n* [Functional Programming References](#functional-programming-references)\n* [Function Programming development in Rust Language](#functional-programming-development-in-rust-language)\n* [Inspiration](#inspiration)\n\n## Arity\n\nThe number of arguments a function takes. From words like unary, binary, ternary, etc.\nThis word has the distinction of being composed of two suffixes, \"-ary\" and \"-ity.\"\nAddition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two.\nSuch a function may sometimes be called \"dyadic\" by people who prefer Greek roots to Latin.\nLikewise, a function that takes a variable number of arguments is called \"variadic,\"\nwhereas a binary function must be given two and only two arguments, currying and partial application notwithstanding (see below).\n\n```rust\nlet sum = |a: i32, b: i32| { a + b }; // The arity of sum is 2\n```\n\n## Higher-Order Functions (HOF)\n\nA function which takes a function as an argument and/or returns a function.\n\n```rust\nlet filter = | predicate: fn(&i32) -> bool, xs: Vec<i32> | {\n    xs.into_iter().filter(predicate).collect::<Vec<i32>>()\n};\n```\n\n```rust\nlet is_even = |x: &i32| { x % 2 == 0 };\n```\n\n```rust\nfilter(is_even, vec![1, 2, 3, 4, 5, 6]);\n```\n\n## Closure\n\nA closure is a scope which retains variables available to a function when it's created. This is important for\n[partial application](#partial-application) to work.\n\n```rust\nlet add_to = |x: i32| move |y: i32| x + y;\n```\n\nWe can call `add_to` with a number and get back a function with a baked-in `x`. Notice that we also need to move the ownership of the x to the internal lambda.\n\n```rust\nlet add_to_five = add_to(5);\n```\n\nIn this case the `x` is retained in `add_to_five`'s closure with the value `5`. We can then call `add_to_five` with the `y`\nand get back the desired number.\n\n```rust\nadd_to_five(3); // => 8\n```\n\nClosures are commonly used in event handlers so that they still have access to variables defined in their parents when they\nare eventually called.\n\n__Further reading__\n* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)\n* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)\n\n## Partial Application\n\nPartially applying a function means creating a new function by pre-filling some of the arguments to the original function.\n\nTo achieve this easily, we will be using a [partial application crate](https://crates.io/crates/partial_application)\n\n```rust\n#[macro_use]\nextern crate partial_application;\n\nfn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 {\n    (a + b*b + c.pow(3) + d.pow(4)) * mul - off\n}\n\nlet bar = partial!( foo(_, _, 10, 42, 10, 10) );\n\nassert_eq!(\n    foo(15, 15, 10, 42, 10, 10),\n    bar(15, 15)\n); // passes\n```\n\nPartial application helps create simpler functions from more complex ones by baking in data when you have it.\nCurried functions are automatically partially applied.\n\n__Further reading__\n* [Partial Application in Haskell](https://wiki.haskell.org/Partial_application)\n\n\n## Currying\n\nThe process of converting a function that takes multiple arguments into a function that takes them one at a time.\n\nEach time the function is called it only accepts one argument and returns a function that takes one argument until all arguments are passed.\n\n\n```rust\nfn add(x: i32) -> impl Fn(i32)-> i32 {\n    move |y| x + y\n}\n\nlet add5 = add(5);\nadd5(10); // 15\n```\n\n__Further reading__\n* [Currying in Rust](https://hashnode.com/post/currying-in-rust-cjpfb0i2z00cm56s2aideuo4z)\n\n## Auto Currying\n\nTransforming a function that takes multiple arguments into one that if given less than its\ncorrect number of arguments returns a function that takes the rest. When the function gets the correct number of\narguments it is then evaluated.\n\nAlthough Auto Currying is not possible in Rust right now, there is a debate on this issue on the Rust forum:\nhttps://internals.rust-lang.org/t/auto-currying-in-rust/149/22\n\n## Referential Transparency\n\nAn expression that can be replaced with its value without changing the behavior of the program is said to be referentially transparent.\n\nSay we have function greet:\n\n```rust\nlet greet = || \"Hello World!\";\n```\n\nAny invocation of `greet()` can be replaced with `Hello World!` hence greet is referentially transparent.\n\n\n## Lambda\n\nAn anonymous function that can be treated like a value.\n\n```rust\nfn  increment(i: i32) -> i32 { i + 1 }\n\nlet closure_annotated = |i: i32| { i + 1 };\nlet closure_inferred = |i| i + 1;\n```\n\nLambdas are often passed as arguments to Higher-Order functions.\nYou can assign a lambda to a variable, as shown above.\n\n## Lambda Calculus\n\nA branch of mathematics that uses functions to create a [universal model of computation](https://en.wikipedia.org/wiki/Lambda_calculus).\n\nThis is in contrast to a [Turing machine](https://www.youtube.com/watch?v=dNRDvLACg5Q), an equivalent model.\n\nLambda calculus has three key components: variables, abstraction, and application. A variable is just some\nsymbol, say `x`. An abstraction is sort of a function: it binds variables into \"formulae\". Applications\nare function calls. This is meaningless without examples.\n\nThe identity function (`|x| x` in rust) looks like `\\ x. x` in most literature (`\\` is a Lambda where Latex\nor Unicode make it available). It is an abstraction. If `1` were a value we could use, `(\\ x. x) 1` would\nbe an application (and evaluating it gives you `1`).\n\nBut there's more...\n\n__Computation in Pure Lambda Calculus__\n\nLet's invent booleans. `\\ x y. x` can be true and `\\ x y. y` can be false.\nIf so, `\\ b1 b2. b1(b2,(\\x y. y))` is `and`. Let's evaluate it to show how:\n\n| `b1` | `b2` | Their `and` |\n| --- | --- | --- |\n| `\\ x y. x` | `\\x y. x` | `\\x y. x` |\n| `\\ x y. x` | `\\x y. y` | `\\x y. y` |\n| `\\ x y. y` | `\\x y. y` | `\\x y. y` |\n| `\\ x y. y` | `\\x y. x` | `\\x y. y` |\n\nI'll leave `or` as an exercise. Furthermore, `if` can now be implemented: `\\c t e. c(t, e)` where `c` is the condition, `t`\nthe consequent (`then`) and `e` the else clause.\n\n[SICP leaves numbers as an exercise.](https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-14.html#%_idx_1474)\nThey define 0 as `\\f . \\x. x` and adding one as `\\n. \\f. \\x. f(n(f)(x))`.\nThat isn't even ASCII art, so let's add: `0 + 1`:\n\n```\n(\\n. \\f. \\x. f(n(f)(x)))(\\f. \\x. x) = \\f. \\x. f((\\x'. x')(x)) = \\f. \\x. f(x)\n```\n\nBasically, the number of `f`s in the expression is the number. I'll leave figuring out larger numbers as a exercise.\nWith patience, you can show that `\\f. \\x. f(f(x))` is two.  This will help with addition: `\\n m. \\f. \\x. n(m(f)(x))`\nshould add two numbers. Let's make 4:\n\n```\n(\\n m. \\f. \\x. n(f)(m(f)(x)))(\\f. x. f(f(x)), \\f. \\x. f(f(x)))\n  = \\f. \\x. (\\f'. \\x'. f'(f'(x')))(f)((\\f'. \\x'. f'(f'(x')))(f)(x))\n  = \\f. \\x. (\\x'. f(f(x')))(f(f(x')))\n  = \\f. \\x. f(f(f(f(x))))\n```\n\nMultiplication is harder and there's better\n[exposition on Wikipedia](https://en.wikipedia.org/wiki/Church_encoding#Calculation_with_Church_numerals).\nAnother good reference is [on stackoverflow](https://stackoverflow.com/questions/3077908/church-numeral-for-addition).\n\n## Purity\n\nA function is pure if the return value is only determined by its input values, and does not produce side effects.\n\n```rust\nlet greet = |name: &str| { format!(\"Hi! {}\", name) };\n\ngreet(\"Jason\"); // Hi! Jason\n```\n\nAs opposed to each of the following:\n\n```rust\nlet name = \"Jason\";\n\nlet greet = || -> String {\n    format!(\"Hi! {}\", name)\n};\n\ngreet(); // String = \"Hi! Jason\"\n```\n\nThe above example's output is based on data stored outside of the function...\n\n```rust\nlet mut greeting: String = \"\".to_string();\n\nlet mut greet = |name: &str| {\n    greeting = format!(\"Hi! {}\", name);\n};\n\ngreet(\"Jason\");\n\nassert_eq!(\"Hi! Jason\", greeting); // Passes\n```\n\n... and this one modifies state outside of the function.\n\n## Side effects\n\nA function or expression is said to have a side effect if apart from returning a value,\nit interacts with (reads from or writes to) external mutable state.\n\n```rust\nuse std::time::SystemTime;\n\nlet now = SystemTime::now();\n```\n\n```rust\nprintln!(\"IO is a side effect!\");\n// IO is a side effect!\n```\n\n## Idempotent\n\nA function is idempotent if reapplying it to its result does not produce a different result.\n\n```rust\n// Custom immutable sort method\nlet sort = |x: Vec<i32>| -> Vec<i32> {\n    let mut x = x;\n    x.sort();\n    x\n};\n```\n\nThen we can use the sort method like\n\n```rust\nlet x = vec![2 ,1];\nlet sorted_x = sort(sort(x.clone()));\nlet expected = vec![1, 2];\nassert_eq!(sorted_x, expected); // passes\n```\n\n```rust\nlet abs = | x: i32 | -> i32 {\n    x.abs()\n};\n\nlet x: i32 = 10;\nlet result = abs(abs(x));\nassert_eq!(result, x); // passes\n```\n\n## Function Composition\n\nThe act of putting two functions together to form a third function where the output of one function is the input of the other.\nBelow is an example of compose function is Rust.\n\n```rust\nmacro_rules! compose {\n    ( $last:expr ) => { $last };\n    ( $head:expr, $($tail:expr), +) => {\n        compose_two($head, compose!($($tail),+))\n    };\n}\n\nfn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C\nwhere\n    F: Fn(A) -> B,\n    G: Fn(B) -> C,\n{\n    move |x| g(f(x))\n}\n```\n\nThen we can use it like\n\n```rust\nlet add = | x: i32 | x + 2;\nlet multiply = | x: i32 | x * 2;\nlet divide = | x: i32 | x / 2;\n\nlet intermediate = compose!(add, multiply, divide);\n\nlet subtract = | x: i32 | x - 1;\n\nlet finally = compose!(intermediate, subtract);\n\nlet expected = 11;\nlet result = finally(10);\nassert_eq!(result, expected); // passes\n```\n\n## Continuation\n\nAt any given point in a program, the part of the code that's yet to be executed is known as a continuation.\n\n```rust\nlet print_as_string = |num: i32| println!(\"Given {}\", num);\n\nlet add_one_and_continue = |num: i32, cc: fn(i32)| {\n    let result = num + 1;\n    cc(result)\n};\n\nadd_one_and_continue(1, print_as_string); // Given 2\n```\n\nContinuations are often seen in asynchronous programming when the program needs to wait to receive data before it can continue.\n The response is often passed off to the rest of the program, which is the continuation, once it's been received.\n\n## Point-Free style\n\nWriting functions where the definition does not explicitly identify the arguments used.\nThis style usually requires currying or other Higher-Order functions. A.K.A Tacit programming.\n\n## Predicate\n\nA predicate is a function that returns true or false for a given value.\nA common use of a predicate is as the callback for array filter.\n\n```rust\nlet predicate = | a: &i32 | a.clone() > 2;\n\nlet result = (vec![1, 2, 3, 4]).into_iter().filter(predicate).collect::<Vec<i32>>();\n\nassert_eq!(result, vec![3, 4]); // passes\n```\n\n## Contracts\n\nA contract specifies the obligations and guarantees of the behavior from a function or expression at runtime.\nThis acts as a set of rules that are expected from the input and output of a function or expression,\nand errors are generally reported whenever a contract is violated.\n\n```rust\nlet contract = | x: &i32 | -> bool {\n    x > &10\n};\n\nlet add_one = | x: &i32 | -> Result<i32, String> {\n    if contract(x) {\n        return Ok(x + 1);\n    }\n    Err(\"Cannot add one\".to_string())\n};\n```\n\nThen you can use `add_one` like\n\n```rust\nlet expected = 12;\nmatch add_one(&11) {\n    Ok(x) => assert_eq!(x, expected),\n    _ => panic!(\"Failed!\")\n}\n```\n\n## Category\n\nA category in category theory is a collection of objects and morphisms between them. In programming, typically types\nact as the objects and functions as morphisms.\n\nTo be a valid category 3 rules must be met:\n\n1. There must be an identity morphism that maps an object to itself.\n    Where `a` is an object in some category,\n    there must be a function from `a -> a`.\n2. Morphisms must compose.\n    Where `a`, `b`, and `c` are objects in some category,\n    and `f` is a morphism from `a -> b`, and `g` is a morphism from `b -> c`;\n    `g(f(x))` must be equivalent to `(g • f)(x)`.\n3. Composition must be associative\n    `f • (g • h)` is the same as `(f • g) • h`\n\nSince these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.\nIn particular, many see this as another foundation of mathematics (so, everything would be a category from this view of math).\nVarious definitions in this guide are related to category theory since this approach applies elegantly to functional programming.\n\n__Examples of categories__\n\nWhen one specifies a category, the objects and morphisms are essential. Additionally, showing that the rules are met is nice\nthough usually left to the reader as an exercise.\n\n* Any type and pure functions on the type. (Note, we require purity since side-effects affect associativity (the third rule).)\n\nThese examples come up in mathematics:\n\n* 1: the category with 1 object and its identity morphism.\n* Monoidal categories: [monoids are defined later](#monoids) but any monoid is a category with 1 object and many morphisms\n  from the object to itself. (Yes, there is also a category of monoids -- this is not that -- this example is that any monoid is its own\n  category.)\n\n__Further reading__\n\n* [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)\n* [Awodey's introduction](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.211.4754&rep=rep1&type=pdf) for those who like math.\n\n## Value\n\nAnything that can be assigned to a variable.\n\n```rust\nlet a = 5;\nlet b = vec![1, 2, 3];\nlet c = \"test\";\n```\n\n## Constant\n\nA variable that cannot be reassigned once defined.\n\n```rust\nlet a = 5;\na = 3; // error!\n```\n\nConstants are [referentially transparent](#referential-transparency).\nThat is, they can be replaced with the values that they represent without affecting the result.\n\n## Variance\n\nVariance in functional programming refers to subtyping between more complex types related to subtyping between\ntheir components.\n\nUnlike other usage of variance in [Object Oriented Programming like Typescript or C#](https://medium.com/@michalskoczylas/covariance-contravariance-and-a-little-bit-of-typescript-2e61f41f6f68)\nor [functional programming language like Scala or Haskell](https://medium.com/@wiemzin/variances-in-scala-9c7d17af9dc4)\n\nVariance in Rust is used during the type checking against type and lifetime parameters. Here are examples:\n- By default, all lifetimes are co-variant except for `'static` because it outlives all others\n- `'static` is always contra-variant to others regardless of where it appears or used\n- It is `in-variant` if you use `Cell<T>` or `UnsafeCell<T>` in `PhatomData`\n\n**Further Reading**\n\n- https://github.com/rust-lang/rustc-guide/blob/master/src/variance.md\n- https://nearprotocol.com/blog/understanding-rust-lifetimes/\n\n## Higher Kinded Type (HKT)\n\nRust does not support Higher Kinded Types [yet](https://github.com/rust-lang/rfcs/issues/324). First of all, HKT is a\ntype with a \"hole\" in it, so you can declare a type signature such as `trait Functor<F<A>>`.\n\nAlthough Rust lacks in a native support for HKT, we always have a walk around called [Lightweight Higher Kinded Type](https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-kinded-polymorphism.pdf)\n\nAn implementation example of above theory in Rust would look like below:\n\n```rust\npub trait HKT<A, B> {\n    type URI;\n    type Target;\n}\n\n// Lifted Option\nimpl<A, B> HKT<A, B> for Option<A> {\n    type URI = Self;\n    type Target = Option<B>;\n}\n```\n\nHigher Kinded Type is crucial for functional programming in general.\n\n**Further Reading**\n\n- https://gist.github.com/CMCDragonkai/a5638f50c87d49f815b8\n- https://www.youtube.com/watch?v=ERM0mBPNLHc\n\n## Functor\n\nAn object that implements a map function which,\nwhile running over each value in the object to produce a new functor of the same type, adheres to two rules:\n\n__Preserves identity__\n\n```\nobject.map(x => x) ≍ object\n```\n\n__Composable__\n\n```\nobject.map(compose(f, g)) ≍ object.map(g).map(f)\n```\n\n(`f`, `g` are arbitrary functions)\n\nFor example, below can be considered as a functor-like operation\n\n```rust\nlet v: Vec<i32> = vec![1, 2, 3].into_iter().map(| x | x + 1).collect();\n\nassert_eq!(v, vec![2, 3, 4]); // passes while mapping the original vector and returns a new vector\n```\n\nWhile leveraging the [HKT implementation](#higher-kinded-type-hkt), You can define a trait that represents Functor like below\n\n```rust\npub trait Functor<A, B>: HKT<A, B> {\n    fn fmap<F>(self, f: F) -> <Self as HKT<A, B>>::Target\n        where F: FnOnce(A) -> B;\n}\n```\n\nThen use it against a type such as [Option](#https://doc.rust-lang.org/std/option/index.html) like\n\n```rust\nimpl<A, B> Functor<A, B> for Option<A> {\n    fn fmap<F>(self, f: F) -> Self::Target\n        where\n            F: FnOnce(A) -> B\n    {\n        self.map(f)\n    }\n}\n\n// This conflicts with the above, so isn't tested\n// below and is commented out. TODO: add a careful\n// implementation that makes sure Optional and this\n// don't conflict.\nimpl<A, B, T> HKT<A, B> for T\nwhere\n    T: Sized + Iterator<Item = A>,\n    U: Sized + Iterator<Item = B>,\n{\n    type URI = Self;\n    type Target = U;\n}\n\nimpl<A, B, T> Functor<A, B> for T\nwhere\n    T: Iterator<Item = A>,\n{\n    fn fmap<F>(self, f: F) -> Self::Target\n    where\n        F: FnOnce(A) -> B,\n        A: Sized,\n        B: Sized,\n    {\n        self.map(f)\n    }\n}\n\n#[test]\nfn test_functor() {\n    let z = Option::fmap(Some(1), |x| x + 1).fmap(|x| x + 1); // Return Option<B>\n    assert_eq!(z, Some(3)); // passes\n}\n```\n\n__The Underlying Math__\n\nThe confusing fact is that functors are morphisms in the category of categories. Really, this means that\na functor from category `C` into `D` preserves properties of the category, so that the data is somewhat\npreserved.\n\nTechnically, every category has a functor into the simplest (non-empty) category (1): since the category `1` just\nhas one object and one function, map all the objects and functions in whatever category you start from into the\nthing in `1`. So, data isn't quite preserved in a \"nice\" sense. Such functors are called forgetful sometimes as\nthey drop structure.\n\nHowever, less forgetful examples provide more insight and empower useful statements about types.\nUnfortunately, these are rather heavy-handed in the mathematics they evoke.\n\n\n## Pointed Functor\n\nAn object with an of function that puts any single value into it.\n\n```rust\n#[derive(Debug, PartialEq, Eq)]\nenum Maybe<T> {\n    Nothing,\n    Just(T),\n}\n\n\nimpl<T> Maybe<T> {\n    fn of(x: T) -> Self {\n        Maybe::Just(x)\n    }\n}\n```\n\nThen use it like\n\n```rust\nlet pointed_functor = Maybe::of(1);\n\nassert_eq!(pointed_functor, Maybe::Just(1));\n```\n\n## Lifting\n\nLifting in functional programming typically means to lift a function into a context (a Functor or Monad).\nFor example, give a function `a -> b` and lift it into a `List` then the signature would\nlook like `List[a] -> List[b]`.\n\n**Further Reading**\n- https://wiki.haskell.org/Lifting\n- https://stackoverflow.com/questions/43482772/difference-between-lifting-and-higher-order-functions\n- https://stackoverflow.com/questions/2395697/what-is-lifting-in-haskell/2395956\n\n## Equational Reasoning\n\nWhen an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts.\n\n## Monoid\n\nAn set with a binary function that \"combines\" pairs from that set into another element of the set.\n\nOne simple monoid is the addition of numbers:\n\n```rust\n1 + 1\n// i32: 2\n```\n\nIn this case numbers are the set and `+` is the function.\n\nAn \"identity\" value must also exist that when combined with a value doesn't change it.\n\nThe identity value for addition is `0`.\n\n```rust\n1 + 0\n// i32: 1\n```\n\nIt's also required that the grouping of operations will not affect the result (associativity):\n\n```rust\n1 + (2 + 3) == (1 + 2) + 3\n// bool: true\n```\n\nArray concatenation also forms a monoid:\n\n```rust\n[vec![1, 2, 3], vec![4, 5, 6]].concat();\n// Vec<i32>: vec![1, 2, 3, 4, 5, 6]\n```\n\nThe identity value is empty array `[]`\n\n```rust\n[vec![1, 2], vec![]].concat();\n// Vec<i32>: vec![1, 2]\n```\n\nIf identity and compose functions are provided, functions themselves form a monoid:\n\n```rust\nfn identity<A>(a: A) -> A {\n    a\n}\n```\n\n`foo` is any function that takes one argument.\n\n```rust\ncompose(foo, identity) ≍ compose(identity, foo) ≍ foo\n```\n\nWe can express Monoid as a Rust trait and the type signature would look like below\n\n```rust\nuse crate::applicative_example::Applicative;\n\ntrait Empty<A> {\n    fn empty() -> A;\n}\n\ntrait Monoid<A, F, B>: Empty<A> + Applicative<A, F, B>\nwhere\n    F: FnOnce(A) -> B,\n{\n}\n```\n\nAccording to Fantasy Land Specification, Monoid should implement `Empty` and `Applicative`.\n\n## Monad\n\nA [Monad](https://github.com/fantasyland/fantasy-land#monad) is a trait that implements `Applicative` and `Chain` specifications. `chain` is\nlike `map` except it un-nests the resulting nested object.\n\nFirst, `Chain` type can be implemented like below:\n\n```rust\npub trait Chain<A, B>: HKT<A, B> {\n    fn chain<F>(self, f: F) -> <Self as HKT<A, B>>::Target\n        where F: FnOnce(A) -> <Self as HKT<A, B>>::Target;\n}\n\nimpl<A, B> Chain<A, B> for Option<A> {\n    fn chain<F>(self, f: F) -> Self::Target\n        where F: FnOnce(A) -> <Self as HKT<A, B>>::Target {\n        self.and_then(f)\n    }\n}\n```\n\nThen `Monad` itself can simply derive `Chain` and `Applicative`\n\n```rust\npub trait Monad<A, F, B>: Chain<A, B> + Applicative<A, F, B>\n    where F: FnOnce(A) -> B {}\n\nimpl<A, F, B> Monad<A, F, B> for Option<A>\n    where F: FnOnce(A) -> B {}\n\n#[test]\nfn monad_example() {\n    let x = Option::of(Some(1)).chain(|x| Some(x + 1));\n    assert_eq!(x, Some(2)); // passes\n}\n```\n\n`pure` is also known as `return` in other functional languages. `flat_map` is also known as `bind` in other languages.\n\nImportantly, it is worth noting that monads are a rather advanced topic in category theory. In fact, they are called\ntriples by some as they involve adjoint functors and their unit -- both of which are rare to see in functional programming.\nThe meme is to think of a monad as a burrito with \"pure\" being the act of taking a tortilla (the empty burrito) and\nadding ingredients using \"chain\".\n\nThe purely mathematical presentation of monads does not look anything like this, but there is an equivalence.\n\n## Comonad\n\nAn object that has `extract` and `extend` functions.\n\n```rust\ntrait Extend<A, B>: Functor<A, B> + Sized {\n    fn extend<W>(self, f: W) -> <Self as HKT<A, B>>::Target\n    where\n        W: FnOnce(Self) -> B;\n}\n\ntrait Extract<A> {\n    fn extract(self) -> A;\n}\n\ntrait Comonad<A, B>: Extend<A, B> + Extract<A> {}\n```\n\nThen we can implement these types for Option\n\n```rust\nimpl<A, B> Extend<A, B> for Option<A> {\n    fn extend<W>(self, f: W) -> Self::Target\n    where\n        W: FnOnce(Self) -> B,\n    {\n        self.map(|x| f(Some(x)))\n    }\n}\n\nimpl<A> Extract<A> for Option<A> {\n    fn extract(self) -> A {\n        self.unwrap() // is there a better way to achieve this?\n    }\n}\n```\n\nExtract takes a value out of a comonad.\n\n```rust\nSome(1).extract(); // 1\n```\n\nExtend runs a function on the Comonad.\n\n```rust\nSome(1).extend(|co| co.extract() + 1); // Some(2)\n```\n\nThis can be thought of as the reverse of a monad. In fact, this is called\nthe \"dual\" in category theory. (Basically, if you know what `A` is, a `coA`\nis everything in `A`'s definition with the arrows reversed.)\n\n## Applicative\n\nAn applicative functor is an object with an `ap` function. `ap` applies a function in the object to a value in another\nobject of the same type. Given a pure program `g: (b: A) -> B`, we must lift it to `g: (fb: F<A>) -> F<B>`. In order to achieve\nthis, we will introduce another [higher kinded type](#higher-kinded-type-hkt), called `HKT3` that is capable of doing this.\n\nFor this example, we will use Option datatype.\n\n```rust\ntrait HKT3<A, B, C> {\n    type Target2;\n}\n\nimpl<A, B, C> HKT3<A, B, C> for Option<A> {\n    type Target2 = Option<B>;\n}\n```\n\nSince Applicative implements Apply for `ap`  and `Pure` for `of` according to [Fantasy Land specification](https://github.com/fantasyland/fantasy-land#applicative)\nwe must implement the types like below:\n\n```rust\n\n// Apply\ntrait Apply<A, F, B> : Functor<A, B> + HKT3<A, F, B>\n    where F: FnOnce(A) -> B,\n{\n    fn ap(self, f: <Self as HKT3<A, F, B>>::Target2) -> <Self as HKT<A, B>>::Target;\n}\n\nimpl<A, F, B> Apply<A, F, B> for Option<A>\n    where F: FnOnce(A) -> B,\n{\n    fn ap(self, f: Self::Target2) -> Self::Target {\n        self.and_then(|v| f.map(|z| z(v)))\n    }\n}\n\n// Pure\ntrait Pure<A>: HKT<A, A> {\n    fn of(self) -> <Self as HKT<A, A>>::Target;\n}\n\nimpl<A> Pure<A> for Option<A> {\n    fn of(self) -> Self::Target {\n        self\n    }\n}\n\n// Applicative\ntrait Applicative<A, F, B> : Apply<A, F, B> + Pure<A>\n    where F: FnOnce(A) -> B,\n{\n} // Simply derives Apply and Pure\n\nimpl<A, F, B> Applicative<A, F, B> for Option<A>\n    where F: FnOnce(A) -> B,\n{\n}\n```\n\nThen we can use Option Applicative like this:\n\n```rust\nlet x = Option::of(Some(1)).ap(Some(|x| x + 1));\nassert_eq!(x, Some(2));\n```\n\n## Morphism\n\nA function that preserves the structure of its domain.\nSee [the category definition](#category) from more.\n\nThe first few (endomorphism, homomorphism, and isomorphism) are easier to\nunderstand than the rest. The rest require the notion of an F-algebra.\nThe simpler Haskell declarations are listed in [the wikipedia on paramorphisms](https://en.wikipedia.org/wiki/Paramorphism)\nbut the notions have yet to be extended to more general category theory.\nBriefly, the view of F-algebras is to take the set-theoretic definition of algebraic\nobjects and redefined them on a purely category theoretic footing: to move the ideas\naway from sets containing elements to collections of objects with morphisms.\nHowever, some of the ideas here have yet to be generalised into this movement.\n\n### Endomorphism\n\nA function where the input type is same as the output.\n\n```rust\n// uppercase :: &str -> String\nlet uppercase = |x: &str| x.to_uppercase();\n\n// decrement :: i32 -> i32\nlet decrement = |x: i32| x - 1;\n```\n\n### Isomorphism\n\nA pair of transformations between 2 types of objects that is invertible.\n\nFor example, 2D coordinates could be stored as a i32 vector [2,3] or a struct {x: 2, y: 3}.\n\n```rust\n#[derive(PartialEq, Debug)]\nstruct Coords {\n    x: i32,\n    y: i32,\n}\n\nlet pair_to_coords = | pair: (i32, i32) | Coords { x: pair.0, y: pair.1 };\nlet coords_to_pair = | coords: Coords | (coords.x, coords.y);\nassert_eq!(\n    pair_to_coords((1, 2)),\n    Coords { x: 1, y: 2 },\n); // passes\nassert_eq!(\n    coords_to_pair(Coords { x: 1, y: 2 }),\n    (1, 2),\n); // passes\n```\n\nIsomorphisms are critical in making structures identical. Since we know that the struct above is\nidentical to a pair, all the functions that exist on the pair can exist on the struct. If `f`\nis the isomorphism and `g` and endomorphism on the codomain: `f^{-1} g f` would extend `g`\nto apply on the domain.\n\n### Homomorphism\n\nA homomorphism is just a structure preserving map. It is the older term of morphism.\nIn fact, a functor is just a homomorphism between categories as it preserves the original category's structure under the mapping.\n\n```rust\nassert_eq!(A::of(f).ap(A::of(x)), A::of(f(x))); // passes\nassert_eq!(\n    Either::of(|x: &str| x.to_uppercase(x)).ap(Either::of(\"oreos\")),\n    Either::of(\"oreos\".to_uppercase),\n); // passes\n```\n\n### Catamorphism\n\nA `reduceRight` function that applies a function against an accumulator and each value of the array (from right-to-left)\nto reduce it to a single value.\n\n```rust\nlet sum = |xs: Vec<i32>| xs.iter().fold(0, |mut sum, &val| { sum += val; sum });\n\nassert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);\n```\n\n### Anamorphism\n\nAn `unfold` function. An `unfold` is the opposite of `fold` (`reduce`). It generates a list from a single value.\n\n```rust\nlet count_down = unfold((8_u32, 1_u32), |state| {\n    let (ref mut x1, ref mut x2) = *state;\n\n    if *x1 == 0 {\n        return None;\n    }\n\n    let next = *x1 - *x2;\n    let ret = *x1;\n    *x1 = next;\n\n    Some(ret)\n});\n\nassert_eq!(\n    count_down.collect::<Vec<u32>>(),\n    vec![8, 7, 6, 5, 4, 3, 2, 1],\n);\n```\n\n### Hylomorphism\n\nThe combination of anamorphism and catamorphism.\n\n### Apomorphism\n\nIt's the opposite of paramorphism, just as anamorphism is the opposite of catamorphism.\nWhereas with paramorphism, you combine with access to the accumulator and what has been accumulated,\napomorphism lets you unfold with the potential to return early.\n\n## Setoid\n\nThis is a set with an equivalence relation.\n\nAn object that has an `equals` function which can be used to compare other objects of the same type.\n\nIt must obey following rules to be `Setoid`\n\n1. `a.equals(a) == true` (reflexivity)\n2. `a.equals(b) == b.equals(a)` (symmetry)\n3. `a.equals(b)` and `b.equals(c)` then `a.equals(c)` (transitivity)\n\nMake a Vector a setoid:\n\nNote that I am treating `Self` / `self` like `a`.\n\n```rust\ntrait Setoid {\n    fn equals(&self, other: &Self) -> bool;\n}\n\nimpl Setoid for Vec<i32> {\n    fn equals(&self, other: &Self) -> bool {\n        self.len() == other.len()\n    }\n}\n\nassert_eq!(vec![1, 2].equals(&vec![1, 2]), true); // passes\n```\n\nIn Rust standard library, it already provides [Eq](https://doc.rust-lang.org/std/cmp/trait.Eq.html), which\nresembles Setoid that was discussed in this section. Also [Eq](https://doc.rust-lang.org/std/cmp/trait.Eq.html)\nhas `equals` implementations that covers a range of data structures that already exist in Rust.\n\n## Ord\n\nAn object or value that implements Ord specification, also implements [Setoid](#setoid) specification.\n\nThe Ord object or value must satisfy below rules for all `a`, `b` or `c`:\n\n1. totality: `a <= b` or `b <= a`\n2. antisymmetric: `a <= b` and `b <= a`, then `a == b`\n3. transivity: `a <= b` and `b <= c`, then `a <= c`\n\nRust documentation for Ord can be found here [Ord](https://doc.rust-lang.org/std/cmp/trait.Ord.html)\n\n## Semigroup\n\nAn object that has a `combine` function that combines it with another object of the same type.\n\nIt must obey following rules to be `Semigroup`\n\n1. `a.add(b).add(c)` is equivalent to `a.add(b.add(c))` (associativity)\n\n```rust\nuse std::ops::Add;\n\npub trait Semigroup<M>: Add<M> {\n}\n\nassert_eq!(\n    vec![1, 2].add(&vec![3, 4]),\n    vec![1, 2, 3, 4],\n); // passes\n\nassert_eq!(\n    a.add(&b).add(&c),\n    a.add(&b.add(&c)),\n); // passes\n```\n\n## Foldable\n\nAn object that has a `foldr/l` function that can transform that object into some other type.\n\n`fold_right` is equivalent to Fantasy Land Foldable's `reduce`, which goes like:\n\n`fantasy-land/reduce :: Foldable f => f a ~> ((b, a) -> b, b) -> b`\n\n```rust\nuse fp_core::foldable::*;\n\nlet k = vec![1, 2, 3];\nlet result = k.reduce(0, |i, acc| i + acc);\nassert_eq!(result, 6);\n```\n\nIf you were to implement `Foldable` manually, the trait of it would look like below\n\n```rust\nuse crate::hkt::HKT;\nuse crate::monoid::Monoid;\n\npub trait Foldable<A, B>: HKT<A, B> {\n    fn reduce<F>(b: B, ba: F) -> <Self as HKT<A, B>>::Target\n    where\n        F: FnOnce(B, A) -> (B, B);\n\n    fn fold_map<M, N, F>(m: M, fa: F) -> M\n    where\n        M: Monoid<N>,\n        F: FnOnce(<Self as HKT<A, B>>::URI) -> M;\n\n    fn reduce_right<F>(b: B, f: F) -> <Self as HKT<A, B>>::Target\n    where\n        F: FnOnce(A, B) -> (B, B);\n}\n```\n\n## Lens\n\nA lens is a type that pairs a getter and a non-mutating setter for some other data structure.\n\n```rust\ntrait Lens<S, A> {\n    fn over(s: &S, f: &Fn(Option<&A>) -> A) -> S {\n        let result: A = f(Self::get(s));\n        Self::set(result, &s)\n    }\n    fn get(s: &S) -> Option<&A>;\n    fn set(a: A, s: &S) -> S;\n}\n\n#[derive(Debug, PartialEq, Clone)]\nstruct Person {\n    name: String,\n}\n\n#[derive(Debug)]\nstruct PersonNameLens;\n\nimpl Lens<Person, String> for PersonNameLens {\n    fn get(s: &Person) -> Option<&String> {\n       Some(&s.name)\n    }\n\n    fn set(a: String, s: &Person) -> Person {\n        Person {\n            name: a,\n        }\n    }\n}\n```\n\nHaving the pair of get and set for a given data structure enables a few key features.\n\n```rust\nlet e1 = Person {\n    name: \"Jason\".to_string(),\n};\nlet name = PersonNameLens::get(&e1);\nlet e2 = PersonNameLens::set(\"John\".to_string(), &e1);\nlet expected = Person {\n    name: \"John\".to_string()\n};\nlet e3 = PersonNameLens::over(&e1, &|x: Option<&String>| {\n    match x {\n        Some(y) => y.to_uppercase(),\n        None => panic!(\"T_T\") // lol...\n    }\n});\n\nassert_eq!(*name.unwrap(), e1.name); // passes\nassert_eq!(e2, expected); // passes\nassert_eq!(e3, Person { name: \"JASON\".to_string() }); // passes\n```\n\nLenses are also composable. This allows easy immutable updates to deeply nested data.\n\n```rust\nstruct FirstLens;\n\nimpl<A> Lens<Vec<A>, A> for FirstLens {\n  fn get(s: &Vec<A>) -> Option<&A> {\n     s.first()\n  }\n\n  fn set(a: A, s: &Vec<A>) -> Vec<A> {\n      unimplemented!() // Nothing to set in FirstLens\n  }\n}\n\nlet people = vec![Person { name: \"Jason\" }, Person { name: \"John\" }];\nLens::over(composeL!(FirstLens, NameLens), &|x: Option<&String>| {\n  match x {\n      Some(y) => y.to_uppercase(),\n      None => panic!(\"T_T\")\n  }\n}, people); // vec![Person { name: \"JASON\" }, Person { name: \"John\" }];\n```\n\n**Further Reading**\n\n- [A Little Lens Starter](https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial)\n- [Monocle Scala](https://scalac.io/scala-optics-lenses-with-monocle/)\n\n## Type Signature\n\nEvery function in Rust will indicate the types of their arguments and return values.\n\n```rust\n// add :: i32 -> i32 -> i32\nfn add(x: i32) -> impl Fn(i32)-> i32 {\n    move |y| x + y\n}\n\n// increment :: i32 -> i32\nfn increment(x: i32) -> i32 {\n    x + 1\n}\n```\n\nIf a function accepts another function as an argument it is wrapped in parentheses.\n\n```rust\n// call :: (a -> b) -> a -> b\nfn call<A, B>(f: &Fn(A) -> B) -> impl Fn(A) -> B + '_ {\n    move |x| f(x)\n}\n```\n\nThe letters `a`, `b`, `c`, `d` are used to signify that the argument can be of any type.\nThe following version of map takes `a` function that transforms `a` value of some type `a` into another type `b`,\nan array of values of type `a`, and returns an array of values of type `b`.\n\n```rust\n// map :: (a -> b) -> [a] -> [b]\nfn map<A, B>(f: &Fn(A) -> B) -> impl Fn(A) -> B + '_ {\n    move |x| f(x)\n}\n```\n\n**Further Reading**\n\n- [Mostly Adequate Guide](https://drboolean.gitbooks.io/mostly-adequate-guide-old/content/ch7.html#tales-from-the-cryptic)\n- [What is Hindley-Milner?](https://stackoverflow.com/questions/399312/what-is-hindley-milner/399392#399392)\n\n## Algebraic data type\n\nA composite type made from putting other types together. Two common classes of algebraic types are [sum](#sum-type) and [product](#product-type).\n\n### Sum Type\n\nA Sum type is the combination of two types together into another one.\nIt is called sum because the number of possible values in the result type is the sum of the input types.\n\nRust has `enum` that literally represent `sum` in ADT.\n\n```rust\nenum WeakLogicValues {\n   True(bool),\n   False(bool),\n   HalfTrue(bool),\n}\n// WeakLogicValues = bool + otherbool + anotherbool\n```\n\n### Product Type\n\nA product type combines types together in a way you're probably more familiar with:\n\n```rust\nstruct Point {\n    x: i32,\n    y: i32,\n}\n// Point = i32 x i32\n```\n\nIt's called a product because the total possible values of the data structure is the product of the different values.\nMany languages have a tuple type which is the simplest formulation of a product type.\n\nSee also [Set Theory](https://en.wikipedia.org/wiki/Set_theory)\n\n**Further Reading**\n\n- [ADT in 4 different languages](https://blog.softwaremill.com/algebraic-data-types-in-four-languages-858788043d4e)\n- [What are Sum Product and Pi Types](https://manishearth.github.io/blog/2017/03/04/what-are-sum-product-and-pi-types/)\n\n## Option\n\nOption is a [sum type](#sum-type) with two cases often called Some and None.\n\nOption is useful for composing functions that might not return a value.\n\n```rust\nlet mut cart = HashMap::new();\nlet mut item = HashMap::new();\nitem.insert(\n    \"price\".to_string(),\n    12\n);\ncart.insert(\n    \"item\".to_string(),\n    item,\n);\n\nfn get_item(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&HashMap<String, i32>> {\n    cart.get(\"item\")\n}\n\nfn get_price(item: &HashMap<String, i32>) -> Option<&i32> {\n    item.get(\"price\")\n}\n\n```\n\nUse [and_then](https://doc.rust-lang.org/std/option/enum.Option.html#method.and_then) or [map](https://doc.rust-lang.org/std/option/enum.Option.html#method.map) to sequence functions that return Options\n\n```rust\nfn get_nested_price(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&i32> {\n    return get_item(cart).and_then(get_price);\n}\n\nlet price = get_nested_price(&cart);\n\nmatch price {\n    Some(v) => assert_eq!(v, &12),\n    None => panic!(\"T_T\"),\n}\n```\n\n`Option` is also known as `Maybe`. `Some` is sometimes called `Just`. `None` is sometimes called `Nothing`.\n\n## Functional Programming references\n\n- [Scala with Cats](https://underscore.io/books/scala-with-cats/)\n- [Haskell Programming](http://haskellbook.com/)\n- [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)\n- [Category Theory Notes](http://cheng.staff.shef.ac.uk/catnotes/categorynotes-cheng.pdf)\n\n## Functional Programming development in Rust Language\n\n- [Higher Kinded Polymorphism RFC](https://github.com/rust-lang/rfcs/issues/324)\n- [Currying in Rust](https://internals.rust-lang.org/t/currying-in-rust/10326)\n- [Auto-Currying in Rust](https://internals.rust-lang.org/t/auto-currying-in-rust/149)\n\n## Inspiration\n\nAs a community, we have chosen our value as \"learn by teaching\".\nWe want to share our knowledge with the world while we are learning.\n\n"
  },
  {
    "path": "fp-core/Cargo.toml",
    "content": "[package]\nname = \"fp-core\"\nversion = \"0.1.9\"\nauthors = [\"Jason Shin <visualbbasic@gmail.com>\", \"Heman Gandhi <hemangandhi@gmail.com>\"]\nedition = \"2018\"\ndescription = \"A library for functional programming in Rust\"\nlicense = \"MIT\"\nkeywords = [\n  \"fp\",\n  \"haskell\",\n  \"applicative\",\n  \"functor\",\n  \"monad\",\n]\nreadme = \"../README.md\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\nitertools = \"0.8.0\"\n"
  },
  {
    "path": "fp-core/src/applicative.rs",
    "content": "use crate::apply::Apply;\nuse crate::pure::Pure;\n\npub trait Applicative<A, B>: Apply<B> + Pure<A> {}\n\nimpl<A, B> Applicative<A, B> for Option<A> {}\n\nimpl<A, B, E> Applicative<A, B> for Result<A, E> {}\n"
  },
  {
    "path": "fp-core/src/apply.rs",
    "content": "use crate::functor::Functor;\nuse crate::hkt::HKT;\n\ntype Applicator<B, S> = <S as HKT<Box<dyn Fn(<S as HKT<B>>::Current) -> B>>>::Target;\n\npub trait Apply<B>: Functor<B> + HKT<Box<dyn Fn(<Self as HKT<B>>::Current) -> B>> {\n    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target;\n}\n\nimpl<A, B> Apply<B> for Option<A> {\n    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {\n        self.and_then(|v| f.map(|z| z(v)))\n    }\n}\n\nimpl<A, B, E> Apply<B> for Result<A, E> {\n    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {\n        self.and_then(|v| f.map(|z| z(v)))\n    }\n}\n"
  },
  {
    "path": "fp-core/src/chain.rs",
    "content": "use crate::hkt::HKT;\n\npub trait Chain<B>: HKT<B> {\n    fn chain<F>(self, f: F) -> <Self as HKT<B>>::Target\n    where\n        F: FnOnce(<Self as HKT<B>>::Current) -> <Self as HKT<B>>::Target;\n}\n\nimpl<A, B> Chain<B> for Option<A> {\n    fn chain<F>(self, f: F) -> Self::Target\n    where\n        F: FnOnce(A) -> <Self as HKT<B>>::Target,\n    {\n        self.and_then(f)\n    }\n}\n\nimpl<A, B, E> Chain<B> for Result<A, E> {\n    fn chain<F>(self, f: F) -> Self::Target\n    where\n        F: FnOnce(A) -> <Self as HKT<B>>::Target,\n    {\n        self.and_then(f)\n    }\n}\n"
  },
  {
    "path": "fp-core/src/comonad.rs",
    "content": "use crate::extend::Extend;\nuse crate::extract::Extract;\n\npub trait Comonad<A, B>: Extend<B> + Extract<A> {}\n"
  },
  {
    "path": "fp-core/src/compose.rs",
    "content": "#[macro_export]\nmacro_rules! compose {\n    ( $last:expr ) => { $last };\n    ( $head:expr, $($tail:expr), +) => {\n        compose_two($head, compose!($($tail),+))\n    };\n}\n\npub fn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C\nwhere\n    F: Fn(A) -> B,\n    G: Fn(B) -> C,\n{\n    move |x| g(f(x))\n}\n"
  },
  {
    "path": "fp-core/src/empty.rs",
    "content": "pub trait Empty {\n    fn empty() -> Self;\n}\n\nmacro_rules! numeric_empty_impl {\n    ($($t:ty)*) => ($(\n        impl Empty for $t {\n            fn empty() -> Self {\n                0\n            }\n        }\n    )*)\n}\n\nnumeric_empty_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }\n\nmacro_rules! floating_numeric_empty_impl {\n    ($($t:ty)*) => ($(\n        impl Empty for $t {\n            fn empty() -> Self {\n                0.0\n            }\n        }\n    )*)\n}\n\nfloating_numeric_empty_impl! { f32 f64 }\n\nimpl<T> Empty for Vec<T> {\n    fn empty() -> Vec<T> {\n        vec![]\n    }\n}\n\nimpl Empty for String {\n    fn empty() -> String {\n        \"\".to_string()\n    }\n}\n"
  },
  {
    "path": "fp-core/src/extend.rs",
    "content": "use crate::functor::Functor;\nuse crate::hkt::HKT;\n\npub trait Extend<B>: Functor<B> + Sized {\n    fn extend<W>(self, f: W) -> <Self as HKT<B>>::Target\n    where\n        W: FnOnce(Self) -> B;\n}\n\nimpl<A, B> Extend<B> for Option<A> {\n    fn extend<W>(self, f: W) -> Self::Target\n    where\n        W: FnOnce(Self) -> B,\n    {\n        self.map(|x| f(Some(x)))\n    }\n}\n\nimpl<A, B, E> Extend<B> for Result<A, E> {\n    fn extend<W>(self, f: W) -> Self::Target\n    where\n        W: FnOnce(Self) -> B,\n    {\n        self.map(|x| f(Ok(x)))\n    }\n}\n"
  },
  {
    "path": "fp-core/src/extract.rs",
    "content": "pub trait Extract<A> {\n    fn extract(self) -> A;\n}\n\nimpl<A> Extract<A> for Option<A> {\n    fn extract(self) -> A {\n        self.unwrap() // is there a better way to achieve this?\n    }\n}\n\nimpl<A, E> Extract<A> for Result<A, E>\nwhere\n    E: std::fmt::Debug,\n{\n    fn extract(self) -> A {\n        self.unwrap() // is there a better way to achieve this?\n    }\n}\n"
  },
  {
    "path": "fp-core/src/foldable.rs",
    "content": "use crate::hkt::HKT;\nuse crate::monoid::Monoid;\n\n// Cheating: all HKT instances exist for any B,\n// so HKT<B> here isn't about Self<B> having any meaning,\n// it's about folding on some Self<A> -- the HKT lets us\n// have an A to speak of.\npub trait Foldable<B>: HKT<B> + Sized {\n    fn reduce<F>(self, b: B, ba: F) -> B\n    where\n        F: Fn(B, &<Self as HKT<B>>::Current) -> B;\n\n    fn reduce_right<F>(self, b: B, f: F) -> B\n    where\n        F: Fn(&<Self as HKT<B>>::Current, B) -> B;\n}\n\n// Biggest hardship with trying to put this into the above:\n// we cannot have B constrained to be a Monoid, so having\n// a default implementation becomes impossible. That said,\n// having this as a separate function might make more sense\n// (in particular, it might be easier to implement Foldable for\n// rust containers as above and not have to worry about our Monoid\n// until \"later\" -- when this function is handy).\npub fn fold_map<M, C, F>(container: C, mapper: F) -> M\nwhere\n    M: Monoid,\n    C: Foldable<M>,\n    F: Fn(&<C as HKT<M>>::Current) -> M,\n{\n    container.reduce(M::empty(), |acc, curr| acc.combine(mapper(curr)))\n}\n\nimpl<A, B> Foldable<B> for Vec<A> {\n    fn reduce<F>(self, b: B, fa: F) -> B\n    where\n        F: Fn(B, &A) -> B,\n    {\n        self.iter().fold(b, fa)\n    }\n\n    // TODO: make sure this is correct.\n    fn reduce_right<F>(self, b: B, fa: F) -> B\n    where\n        F: Fn(&A, B) -> B,\n    {\n        self.iter().rev().fold(b, |x, y| fa(y, x))\n    }\n}\n"
  },
  {
    "path": "fp-core/src/functor.rs",
    "content": "use crate::hkt::HKT;\n\npub trait Functor<B>: HKT<B> {\n    fn fmap<F>(self, f: F) -> Self::Target\n    where\n        F: FnOnce(Self::Current) -> B;\n}\n\nimpl<A, B> Functor<B> for Option<A> {\n    fn fmap<F>(self, f: F) -> Self::Target\n    where\n        // A is Self::Current\n        F: FnOnce(A) -> B,\n    {\n        self.map(f)\n    }\n}\n\nimpl<A, B, E> Functor<B> for Result<A, E> {\n    fn fmap<F>(self, f: F) -> Self::Target\n    where\n        // A is Self::Current\n        F: FnOnce(A) -> B,\n    {\n        self.map(f)\n    }\n}\n"
  },
  {
    "path": "fp-core/src/hkt.rs",
    "content": "use std::collections::HashMap;\n\n// TODO: use a declarative macro (see https://github.com/rust-lang/rust/issues/39412) to make this\n// one macro that is invoked repeatedly.\n\npub trait HKT<U> {\n    type Current;\n    type Target;\n}\n\nmacro_rules! derive_hkt {\n    ($t: ident) => {\n        impl<T, U> HKT<U> for $t<T> {\n            type Current = T;\n            type Target = $t<U>;\n        }\n    };\n}\n\nderive_hkt!(Option);\nderive_hkt!(Vec);\n\nimpl<T, U, E> HKT<U> for Result<T, E> {\n    type Current = T;\n    type Target = Result<U, E>;\n}\n\npub trait HKT3<U1, U2> {\n    type Current1;\n    type Current2;\n    type Target;\n}\n\nmacro_rules! derive_hkt3 {\n    ($t:ident) => {\n        impl<T1, T2, U1, U2> HKT3<U1, U2> for $t<T1, T2> {\n            // The currently contained types\n            type Current1 = T1;\n            type Current2 = T2;\n            // How the U's get filled in.\n            type Target = $t<U1, U2>;\n        }\n    };\n}\n\nderive_hkt3!(HashMap);\n"
  },
  {
    "path": "fp-core/src/identity.rs",
    "content": "pub fn identity<A>(a: A) -> A {\n    a\n}\n"
  },
  {
    "path": "fp-core/src/lens.rs",
    "content": "pub trait Lens<S, A> {\n    fn over(s: &S, f: &dyn Fn(Option<&A>) -> A) -> S {\n        let result: A = f(Self::get(s));\n        Self::set(result, s)\n    }\n    fn get(s: &S) -> Option<&A>;\n    fn set(a: A, s: &S) -> S;\n}\n"
  },
  {
    "path": "fp-core/src/lib.rs",
    "content": "pub mod applicative;\npub mod apply;\npub mod chain;\npub mod comonad;\npub mod compose;\npub mod empty;\npub mod extend;\npub mod extract;\npub mod foldable;\npub mod functor;\npub mod hkt;\npub mod identity;\npub mod lens;\npub mod monad;\npub mod monoid;\npub mod pure;\npub mod semigroup;\npub mod setoid;\n"
  },
  {
    "path": "fp-core/src/monad.rs",
    "content": "use crate::applicative::Applicative;\nuse crate::chain::Chain;\n\npub trait Monad<A, B>: Chain<B> + Applicative<A, B> {}\n\nimpl<A, B> Monad<A, B> for Option<A> {}\n\nimpl<A, B, E> Monad<A, B> for Result<A, E> {}\n"
  },
  {
    "path": "fp-core/src/monoid.rs",
    "content": "use crate::empty::Empty;\nuse crate::semigroup::Semigroup;\n\npub trait Monoid: Empty + Semigroup {}\n\nimpl Monoid for i32 {}\nimpl Monoid for i64 {}\nimpl<T: Clone> Monoid for Vec<T> {}\nimpl Monoid for String {}\n"
  },
  {
    "path": "fp-core/src/pure.rs",
    "content": "use crate::hkt::HKT;\n\npub trait Pure<A>: HKT<A> {\n    fn of(c: Self::Current) -> Self::Target;\n}\n\nimpl<A> Pure<A> for Option<A> {\n    fn of(a: A) -> Self::Target {\n        Some(a)\n    }\n}\n\nimpl<A, E> Pure<A> for Result<A, E> {\n    fn of(a: A) -> Self::Target {\n        Ok(a)\n    }\n}\n"
  },
  {
    "path": "fp-core/src/semigroup.rs",
    "content": "pub trait Semigroup {\n    fn combine(self, other: Self) -> Self;\n}\n\nmacro_rules! semigroup_numeric_impl {\n    ($($t:ty)*) => ($(\n        impl Semigroup for $t {\n            fn combine(self, other: Self) -> Self {\n                self + other\n            }\n        }\n    )*)\n}\n\nsemigroup_numeric_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }\n\nimpl<T: Clone> Semigroup for Vec<T> {\n    fn combine(self, other: Self) -> Self {\n        let mut concat = self.to_vec();\n        concat.extend_from_slice(&other);\n        concat\n    }\n}\n\nimpl Semigroup for String {\n    fn combine(self, other: Self) -> Self {\n        format!(\"{}{}\", self, other)\n    }\n}\n"
  },
  {
    "path": "fp-core/src/setoid.rs",
    "content": "pub trait Setoid {\n    fn equals(&self, other: &Self) -> bool;\n}\n\nimpl Setoid for Vec<i32> {\n    fn equals(&self, other: &Self) -> bool {\n        self.len() == other.len()\n    }\n}\n\nimpl Setoid for &str {\n    fn equals(&self, other: &Self) -> bool {\n        self.eq(other)\n    }\n}\n"
  },
  {
    "path": "fp-examples/Cargo.toml",
    "content": "[package]\nname = \"fp-examples\"\nversion = \"0.1.0\"\nauthors = [\"Jason Shin <visualbbasic@gmail.com>\"]\nedition = \"2018\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\npartial_application = \"0.1.0\"\nitertools = \"0.8.0\"\nfp-core = { path = \"../fp-core\" }\n"
  },
  {
    "path": "fp-examples/src/adt_example.rs",
    "content": "#[test]\nfn adt_example() {\n    #[allow(dead_code)]\n    enum WeakLogicValues {\n        True(bool),\n        False(bool),\n        HalfTrue(bool),\n    }\n    // WeakLogicValues = bool + otherbool + anotherbool\n\n    #[allow(dead_code)]\n    struct Point {\n        x: i32,\n        y: i32,\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/anamorphism_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use itertools::unfold;\n\n    #[test]\n    fn anamorphism_example() {\n        let count_down = unfold((8_u32, 1_u32), |state| {\n            let (ref mut x1, ref mut x2) = *state;\n\n            if *x1 == 0 {\n                return None;\n            }\n\n            let next = *x1 - *x2;\n            let ret = *x1;\n            *x1 = next;\n\n            Some(ret)\n        });\n\n        assert_eq!(\n            count_down.collect::<Vec<u32>>(),\n            vec![8, 7, 6, 5, 4, 3, 2, 1],\n        );\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/applicative_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::apply::*;\n    use fp_core::pure::*;\n\n    #[test]\n    fn applicative_example() {\n        let x = Option::of(1).ap(Some(Box::new(|x| x + 1)));\n        assert_eq!(x, Some(2));\n    }\n\n    #[test]\n    fn applicative_example_on_result() {\n        let x = Result::<_, ()>::of(1).ap(Ok(Box::new(|x| x + 1)));\n        assert_eq!(x, Ok(2));\n    }\n\n    // Additional comprehensive applicative tests\n    #[test]\n    fn test_option_pure() {\n        let result = Option::of(5);\n        assert_eq!(result, Some(5));\n    }\n\n    #[test]\n    fn test_result_pure() {\n        let result: Result<i32, String> = Result::of(5);\n        assert_eq!(result, Ok(5));\n    }\n\n    #[test]\n    fn test_option_apply() {\n        let value = Some(5);\n        let func = Some(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);\n        let result = value.ap(func);\n        assert_eq!(result, Some(10));\n    }\n\n    #[test]\n    fn test_option_apply_none_value() {\n        let value: Option<i32> = None;\n        let func = Some(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);\n        let result = value.ap(func);\n        assert_eq!(result, None);\n    }\n\n    #[test]\n    fn test_option_apply_none_func() {\n        let value = Some(5);\n        let func: Option<Box<dyn Fn(i32) -> i32>> = None;\n        let result = value.ap(func);\n        assert_eq!(result, None);\n    }\n\n    #[test]\n    fn test_result_apply_ok() {\n        let value: Result<i32, String> = Ok(5);\n        let func: Result<Box<dyn Fn(i32) -> i32>, String> =\n            Ok(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);\n        let result = value.ap(func);\n        assert_eq!(result, Ok(10));\n    }\n\n    #[test]\n    fn test_result_apply_err_value() {\n        let value: Result<i32, String> = Err(\"value error\".to_string());\n        let func: Result<Box<dyn Fn(i32) -> i32>, String> =\n            Ok(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);\n        let result = value.ap(func);\n        assert_eq!(result, Err(\"value error\".to_string()));\n    }\n\n    #[test]\n    fn test_result_apply_err_func() {\n        let value: Result<i32, String> = Ok(5);\n        let func: Result<Box<dyn Fn(i32) -> i32>, String> = Err(\"func error\".to_string());\n        let result = value.ap(func);\n        assert_eq!(result, Err(\"func error\".to_string()));\n    }\n\n    // Applicative Laws\n    #[test]\n    fn test_applicative_identity_law_option() {\n        // Identity: pure(id).ap(v) == v\n        let v = Some(42);\n        let id_func = Some(Box::new(|x: i32| x) as Box<dyn Fn(i32) -> i32>);\n        let result = v.ap(id_func);\n        assert_eq!(result, Some(42));\n    }\n\n    #[test]\n    fn test_applicative_homomorphism_option() {\n        // Homomorphism: pure(f).ap(pure(x)) == pure(f(x))\n        let x = 5;\n        let f = |n: i32| n * 2;\n\n        let left = Option::of(x).ap(Some(Box::new(f) as Box<dyn Fn(i32) -> i32>));\n        let right = Option::of(f(x));\n\n        assert_eq!(left, right);\n    }\n\n    #[test]\n    fn test_apply_with_type_change() {\n        let value = Some(42);\n        let func = Some(Box::new(|x: i32| x.to_string()) as Box<dyn Fn(i32) -> String>);\n        let result = value.ap(func);\n        assert_eq!(result, Some(\"42\".to_string()));\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/arity_example.rs",
    "content": "#[test]\nfn arity() {\n    let sum = |a: i32, b: i32| a + b;\n    let result = sum(1, 2);\n    assert_eq!(result, 3);\n}\n"
  },
  {
    "path": "fp-examples/src/catamorphism_example.rs",
    "content": "#[test]\nfn catamorphism_example() {\n    let sum = |xs: Vec<i32>| {\n        xs.iter().fold(0, |mut sum, &val| {\n            sum += val;\n            sum\n        })\n    };\n\n    assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);\n}\n"
  },
  {
    "path": "fp-examples/src/closure_example.rs",
    "content": "#[test]\nfn closure() {\n    let add_to = |x: i32| move |y: i32| x + y;\n\n    let add_to_five = add_to(5);\n\n    assert_eq!(add_to_five(3), 8);\n}\n"
  },
  {
    "path": "fp-examples/src/comonad_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::extend::*;\n    use fp_core::extract::*;\n\n    #[test]\n    fn comonad_test() {\n        let z = Some(1).extend(|x| x.extract() + 1);\n        assert_eq!(z, Some(2));\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/continuation_example.rs",
    "content": "#[test]\nfn continuation() {\n    let print_as_string = |num: i32| println!(\"Given {}\", num);\n\n    let add_one_and_continue = |num: i32, cc: fn(i32)| {\n        let result = num + 1;\n        cc(result)\n    };\n\n    add_one_and_continue(1, print_as_string);\n}\n"
  },
  {
    "path": "fp-examples/src/contracts_example.rs",
    "content": "#[test]\nfn contracts_example() {\n    let contract = |x: &i32| -> bool { x > &10 };\n\n    let add_one = |x: &i32| -> Result<i32, String> {\n        if contract(x) {\n            return Ok(x + 1);\n        }\n        Err(\"Cannot add one\".to_string())\n    };\n\n    let expected = 12;\n    match add_one(&11) {\n        Ok(x) => assert_eq!(x, expected),\n        _ => panic!(\"Failed!\"),\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/currying_example.rs",
    "content": "#[test]\nfn currying() {\n    fn add(x: i32) -> impl Fn(i32) -> i32 {\n        move |y| x + y\n    }\n\n    let add5 = add(5);\n    let result = add5(10);\n    assert_eq!(result, 15);\n}\n"
  },
  {
    "path": "fp-examples/src/empty_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::empty::*;\n\n    #[test]\n    fn empty_example_vec() {\n        let empty_vec = Vec::<i32>::empty();\n        assert_eq!(empty_vec, vec![])\n    }\n\n    #[test]\n    fn empty_example_string() {\n        let empty_str = String::empty();\n        assert_eq!(empty_str, \"\".to_string())\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/endomorphism_example.rs",
    "content": "#[test]\nfn endomorphism_example() {\n    let uppercase = |x: &str| x.to_uppercase();\n    let decrement = |x: i32| x - 1;\n\n    assert_eq!(uppercase(\"abc\"), \"ABC\".to_string());\n    assert_eq!(decrement(1), 0);\n}\n"
  },
  {
    "path": "fp-examples/src/foldable_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::empty::Empty;\n    use fp_core::foldable::*;\n\n    /*\n    // Check out foldable.rs in fp-core\n    pub trait Foldable<A, B>: HKT<A, B> {\n        fn reduce<F>(b: B, ba: F) -> <Self as HKT<A, B>>::Target\n        where\n            F: FnOnce(B, A) -> (B, B);\n    }\n    */\n\n    #[test]\n    fn foldable_example() {\n        let k = vec![1, 2, 3];\n        let result = k.reduce(0, |i, acc| i + acc);\n        assert_eq!(result, 6);\n    }\n\n    #[test]\n    fn fold_map_example() {\n        let k = vec![Some(1_i64), Some(2_i64), Some(3_i64), None];\n        let result = fold_map(k, |&opt| opt.unwrap_or_default());\n        assert_eq!(result, 6);\n    }\n\n    // Additional comprehensive foldable tests\n    #[test]\n    fn test_vec_reduce() {\n        let vec = vec![1, 2, 3, 4, 5];\n        let result = vec.reduce(0, |acc, x| acc + x);\n        assert_eq!(result, 15);\n    }\n\n    #[test]\n    fn test_vec_reduce_empty() {\n        let vec: Vec<i32> = vec![];\n        let result = vec.reduce(0, |acc, x| acc + x);\n        assert_eq!(result, 0);\n    }\n\n    #[test]\n    fn test_vec_reduce_multiply() {\n        let vec = vec![1, 2, 3, 4];\n        let result = vec.reduce(1, |acc, x| acc * x);\n        assert_eq!(result, 24);\n    }\n\n    #[test]\n    fn test_vec_reduce_string_concat() {\n        let vec = vec![\"hello\", \" \", \"world\"];\n        let result = vec.reduce(String::new(), |acc, x| acc + x);\n        assert_eq!(result, \"hello world\");\n    }\n\n    #[test]\n    fn test_vec_reduce_max() {\n        let vec = vec![5, 2, 8, 1, 9, 3];\n        let result = vec.reduce(i32::MIN, |acc, x| acc.max(*x));\n        assert_eq!(result, 9);\n    }\n\n    #[test]\n    fn test_fold_map_sum() {\n        let vec = vec![1, 2, 3, 4, 5];\n        let result: i32 = fold_map(vec, |x| *x);\n        assert_eq!(result, 15);\n    }\n\n    #[test]\n    fn test_fold_map_string_concat() {\n        let vec = vec![1, 2, 3];\n        let result: String = fold_map(vec, |x| x.to_string());\n        assert_eq!(result, \"123\");\n    }\n\n    #[test]\n    fn test_fold_map_with_transformation() {\n        let vec = vec![1, 2, 3];\n        let result: i32 = fold_map(vec, |x| x * 2);\n        assert_eq!(result, 12); // 2 + 4 + 6\n    }\n\n    #[test]\n    fn test_fold_map_empty_vec() {\n        let vec: Vec<i32> = vec![];\n        let result: i32 = fold_map(vec, |x| *x);\n        assert_eq!(result, i32::empty());\n    }\n\n    #[test]\n    fn test_reduce_filter_operation() {\n        let vec = vec![1, 2, 3, 4, 5, 6];\n        let evens = vec.reduce(Vec::new(), |mut acc, x| {\n            if x % 2 == 0 {\n                acc.push(*x);\n            }\n            acc\n        });\n        assert_eq!(evens, vec![2, 4, 6]);\n    }\n\n    #[test]\n    fn test_reduce_partition() {\n        #[derive(Debug, PartialEq)]\n        struct Partitioned {\n            evens: Vec<i32>,\n            odds: Vec<i32>,\n        }\n\n        let vec = vec![1, 2, 3, 4, 5];\n        let result = vec.reduce(\n            Partitioned {\n                evens: vec![],\n                odds: vec![],\n            },\n            |mut acc, x| {\n                if x % 2 == 0 {\n                    acc.evens.push(*x);\n                } else {\n                    acc.odds.push(*x);\n                }\n                acc\n            },\n        );\n\n        assert_eq!(result.evens, vec![2, 4]);\n        assert_eq!(result.odds, vec![1, 3, 5]);\n    }\n\n    #[test]\n    fn test_reduce_count() {\n        let vec = vec![1, 2, 3, 4, 5];\n        let count = vec.reduce(0, |acc, _| acc + 1);\n        assert_eq!(count, 5);\n    }\n\n    #[test]\n    fn test_vec_reduce_right() {\n        let vec = vec![1, 2, 3, 4];\n        let result = vec.reduce_right(0, |x, acc| x + acc);\n        assert_eq!(result, 10);\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/function_composition_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::compose::*;\n\n    #[test]\n    fn function_composition() {\n        let add = |x: i32| x + 2;\n        let multiply = |x: i32| x * 2;\n        let divide = |x: i32| x / 2;\n\n        let intermediate = compose!(add, multiply, divide);\n\n        let subtract = |x: i32| x - 1;\n\n        let finally = compose!(intermediate, subtract);\n\n        let expected = 11;\n        let result = finally(10);\n\n        assert_eq!(result, expected);\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/functor_example.rs",
    "content": "// impl<A, B, T> HKT<A, B> for T\n// where\n//     T: Sized + Iterator<Item = A>,\n//     U: Sized + Iterator<Item = B>,\n// {\n//     type URI = Self;\n//     type Target = U;\n// }\n//\n// impl<A, B, T> Functor<A, B> for T\n// where\n//     T: Iterator<Item = A>,\n// {\n//     fn fmap<F>(self, f: F) -> Self::Target\n//     where\n//         F: FnOnce(A) -> B,\n//         A: Sized,\n//         B: Sized,\n//     {\n//         self.map(f)\n//     }\n// }\n\n#[cfg(test)]\nmod example {\n    use fp_core::functor::Functor;\n\n    #[test]\n    fn test_functor() {\n        let z = Option::fmap(Some(1), |x| x + 1).fmap(|x| x + 1);\n        assert_eq!(z, Some(3));\n\n        // let v = vec![3, 4];\n        // assert_eq!(vec![5, 6], v.iter().fmap(|x| x + 1).fmap(|x| x + 1));\n    }\n\n    #[test]\n    fn test_functor_for_result() {\n        let z = Result::<_, ()>::fmap(Ok(1), |x| x + 1).fmap(|x| x + 1);\n        assert_eq!(z, Ok(3));\n    }\n\n    // Additional comprehensive functor tests\n    #[test]\n    fn test_option_functor_some() {\n        let x = Some(5);\n        let result = x.fmap(|n| n * 2);\n        assert_eq!(result, Some(10));\n    }\n\n    #[test]\n    fn test_option_functor_none() {\n        let x: Option<i32> = None;\n        let result = x.fmap(|n| n * 2);\n        assert_eq!(result, None);\n    }\n\n    #[test]\n    fn test_option_functor_chain() {\n        let x = Some(5);\n        let result = x.fmap(|n| n * 2).fmap(|n| n + 1);\n        assert_eq!(result, Some(11));\n    }\n\n    #[test]\n    fn test_option_functor_type_change() {\n        let x = Some(5);\n        let result = x.fmap(|n| n.to_string());\n        assert_eq!(result, Some(\"5\".to_string()));\n    }\n\n    #[test]\n    fn test_result_functor_ok() {\n        let x: Result<i32, String> = Ok(10);\n        let result = x.fmap(|n| n * 3);\n        assert_eq!(result, Ok(30));\n    }\n\n    #[test]\n    fn test_result_functor_err() {\n        let x: Result<i32, String> = Err(\"error\".to_string());\n        let result = x.fmap(|n| n * 3);\n        assert_eq!(result, Err(\"error\".to_string()));\n    }\n\n    #[test]\n    fn test_result_functor_chain() {\n        let x: Result<i32, String> = Ok(5);\n        let result = x.fmap(|n| n * 2).fmap(|n| n + 10);\n        assert_eq!(result, Ok(20));\n    }\n\n    #[test]\n    fn test_result_functor_preserves_error() {\n        let x: Result<i32, String> = Err(\"initial error\".to_string());\n        let result = x.fmap(|n| n * 2).fmap(|n| n + 10);\n        assert_eq!(result, Err(\"initial error\".to_string()));\n    }\n\n    #[test]\n    fn test_result_functor_type_change() {\n        let x: Result<i32, String> = Ok(42);\n        let result = x.fmap(|n| format!(\"Number: {}\", n));\n        assert_eq!(result, Ok(\"Number: 42\".to_string()));\n    }\n\n    #[test]\n    fn test_functor_with_closure() {\n        let multiplier = 3;\n        let x = Some(5);\n        let result = x.fmap(|n| n * multiplier);\n        assert_eq!(result, Some(15));\n    }\n\n    // Functor Laws\n    #[test]\n    fn test_functor_identity_law_option() {\n        // Identity law: fmap(id) == id\n        let x = Some(42);\n        let result = x.fmap(|n| n);\n        assert_eq!(result, Some(42));\n    }\n\n    #[test]\n    fn test_functor_identity_law_result() {\n        let x: Result<i32, String> = Ok(42);\n        let result = x.fmap(|n| n);\n        assert_eq!(result, Ok(42));\n    }\n\n    #[test]\n    fn test_functor_composition_law_option() {\n        // Composition law: fmap(g . f) == fmap(f).fmap(g)\n        let f = |x: i32| x + 1;\n        let g = |x: i32| x * 2;\n\n        let x1 = Some(5);\n        let x2 = Some(5);\n\n        let composed = x1.fmap(|x| g(f(x)));\n        let chained = x2.fmap(f).fmap(g);\n\n        assert_eq!(composed, chained);\n    }\n\n    #[test]\n    fn test_functor_composition_law_result() {\n        let f = |x: i32| x + 1;\n        let g = |x: i32| x * 2;\n\n        let x1: Result<i32, String> = Ok(5);\n        let x2: Result<i32, String> = Ok(5);\n\n        let composed = x1.fmap(|x| g(f(x)));\n        let chained = x2.fmap(f).fmap(g);\n\n        assert_eq!(composed, chained);\n    }\n}\n\n/*\n// Below is an old implementation\n#[derive(Debug, PartialEq, Eq)]\npub enum Maybe<T> {\n    Nothing,\n    Just(T),\n}\n\n#[test]\nfn functor_example_1() {\n    let v: Vec<i32> = vec![1, 2, 3].into_iter().map(| x | x + 1).collect();\n\n    assert_eq!(v, vec![2, 3, 4]);\n}\n\npub trait Functor<'a, A, B, F>\n    where\n        A: 'a,\n        F: Fn(&'a A) -> B {\n    type Output;\n    fn fmap(&'a self, f: F) -> Self::Output;\n}\n\nimpl<'a, A, B, F> Functor<'a, A, B, F> for Maybe<A>\n    where\n        A: 'a,\n        F: Fn(&'a A) -> B {\n\n    type Output = Maybe<B>;\n    fn fmap(&'a self, f: F) -> Maybe<B> {\n        match *self {\n            Maybe::Just(ref x) => Maybe::Just(f(x)),\n            Maybe::Nothing => Maybe::Nothing,\n        }\n    }\n}\n\n#[test]\nfn functor_example_2() {\n    let just = Maybe::Just(7);\n    let nothing = Maybe::fmap(&Maybe::Nothing, |x| x + 1);\n    let other = Maybe::fmap(&just, |x| x + 1);\n    assert_eq!(nothing, Maybe::Nothing);\n    assert_eq!(other, Maybe::Just(8));\n}\n*/\n"
  },
  {
    "path": "fp-examples/src/hof_example.rs",
    "content": "#[test]\nfn hof() {\n    let filter = |predicate: fn(&i32) -> bool, xs: Vec<i32>| {\n        // A good Reddit post on how Filter works https://www.reddit.com/r/rust/comments/3bmua6/can_someone_help_me_understand_stditerfilter/\n        xs.into_iter().filter(predicate).collect::<Vec<i32>>()\n    };\n\n    let is_even = |x: &i32| x % 2 == 0;\n\n    let result = filter(is_even, vec![1, 2, 3, 4, 5, 6]);\n\n    assert_eq!(result, vec![2, 4, 6]);\n}\n"
  },
  {
    "path": "fp-examples/src/homomorphism_example.rs",
    "content": "#[test]\nfn homomorphism_example() {\n    // Check out the README for a psuedo code\n    assert!(true);\n}\n"
  },
  {
    "path": "fp-examples/src/idempotent_example.rs",
    "content": "#[test]\nfn idempotent_sort() {\n    let sort = |x: Vec<i32>| -> Vec<i32> {\n        let mut x = x;\n        x.sort();\n        x\n    };\n\n    let x = vec![2, 1];\n    let sorted_x = sort(sort(x.clone()));\n    let expected = vec![1, 2];\n    assert_eq!(sorted_x, expected);\n}\n\n#[test]\nfn idempotent_abs() {\n    let abs = |x: i32| -> i32 { x.abs() };\n\n    let x: i32 = 10;\n    let result = abs(abs(x));\n    assert_eq!(result, x);\n}\n"
  },
  {
    "path": "fp-examples/src/isomorphism_example.rs",
    "content": "#[derive(PartialEq, Debug)]\n#[allow(dead_code)]\nstruct Coords {\n    x: i32,\n    y: i32,\n}\n\n#[test]\nfn isomorphism_example() {\n    let pair_to_coords = |pair: (i32, i32)| Coords {\n        x: pair.0,\n        y: pair.1,\n    };\n    let coords_to_pair = |coords: Coords| (coords.x, coords.y);\n    assert_eq!(pair_to_coords((1, 2)), Coords { x: 1, y: 2 },);\n    assert_eq!(coords_to_pair(Coords { x: 1, y: 2 }), (1, 2),);\n}\n"
  },
  {
    "path": "fp-examples/src/lambda_example.rs",
    "content": "#[test]\nfn lambdas() {\n    fn increment(i: i32) -> i32 {\n        i + 1\n    }\n    let closure_annotated = |i: i32| i + 1;\n    let closure_inferred = |i| i + 1;\n\n    let inc = increment(3);\n    let ca = closure_annotated(3);\n    let ci = closure_inferred(3);\n\n    assert_eq!(inc, 4);\n    assert_eq!(ca, 4);\n    assert_eq!(ci, 4);\n}\n"
  },
  {
    "path": "fp-examples/src/lens_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::lens::Lens;\n\n    #[derive(Debug, PartialEq, Clone)]\n    struct Person {\n        name: String,\n    }\n\n    #[derive(Debug)]\n    struct PersonNameLens;\n\n    impl Lens<Person, String> for PersonNameLens {\n        fn get(s: &Person) -> Option<&String> {\n            Some(&s.name)\n        }\n\n        #[allow(dead_code)]\n        fn set(a: String, _s: &Person) -> Person {\n            Person { name: a }\n        }\n    }\n\n    struct FirstLens;\n\n    impl<A> Lens<Vec<A>, A> for FirstLens {\n        fn get(s: &Vec<A>) -> Option<&A> {\n            s.first()\n        }\n\n        #[allow(dead_code)]\n        fn set(_a: A, _s: &Vec<A>) -> Vec<A> {\n            unimplemented!()\n        }\n    }\n\n    #[test]\n    fn lens_example() {\n        let e1 = Person {\n            name: \"Jason\".to_string(),\n        };\n        let name = PersonNameLens::get(&e1);\n        let e2 = PersonNameLens::set(\"John\".to_string(), &e1);\n        let expected = Person {\n            name: \"John\".to_string(),\n        };\n        let e3 = PersonNameLens::over(&e1, &|x: Option<&String>| match x {\n            Some(y) => y.to_uppercase(),\n            None => panic!(\"T_T\"),\n        });\n        let rando = vec![1, 2];\n        let e4 = FirstLens::get(&rando);\n\n        assert_eq!(*name.unwrap(), e1.name);\n        assert_eq!(e2, expected);\n        assert_eq!(\n            e3,\n            Person {\n                name: \"JASON\".to_string()\n            }\n        );\n        assert_eq!(*e4.unwrap(), 1);\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/main.rs",
    "content": "#[allow(unused_imports)]\n#[macro_use]\nextern crate partial_application;\n#[allow(unused_imports)]\n#[macro_use]\nextern crate fp_core;\n\npub mod adt_example;\nmod anamorphism_example;\nmod applicative_example;\nmod arity_example;\nmod catamorphism_example;\nmod closure_example;\nmod comonad_example;\nmod continuation_example;\nmod contracts_example;\nmod currying_example;\nmod empty_example;\nmod endomorphism_example;\nmod foldable_example;\nmod function_composition_example;\nmod functor_example;\nmod hof_example;\nmod idempotent_example;\nmod isomorphism_example;\nmod lambda_example;\nmod lens_example;\nmod monad_example;\nmod monoid_example;\nmod option_example;\nmod partial_application_example;\nmod pointed_functor_example;\nmod predicate_example;\nmod purity_example;\nmod referential_transparency_example;\nmod semigroup_example;\nmod setoid_example;\nmod side_effects_example;\nmod type_signature_example;\n\nfn main() {\n    println!(\"Welcome to fp-core!\");\n}\n"
  },
  {
    "path": "fp-examples/src/monad_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::chain::*;\n    use fp_core::functor::Functor;\n    use fp_core::pure::*;\n\n    #[test]\n    fn monad_example() {\n        let x = Option::of(1).chain(|x| Some(x + 1));\n        assert_eq!(x, Some(2));\n    }\n\n    #[test]\n    fn monad_example_on_result() {\n        let x = Result::<_, ()>::of(1).chain(|x| Ok(x + 1));\n        assert_eq!(x, Ok(2));\n    }\n\n    // Additional comprehensive monad tests\n    #[test]\n    fn test_option_chain_some() {\n        let x = Some(5);\n        let result = x.chain(|n| Some(n * 2));\n        assert_eq!(result, Some(10));\n    }\n\n    #[test]\n    fn test_option_chain_none() {\n        let x: Option<i32> = None;\n        let result = x.chain(|n| Some(n * 2));\n        assert_eq!(result, None);\n    }\n\n    #[test]\n    fn test_option_chain_returns_none() {\n        let x = Some(5);\n        let result: Option<i32> = x.chain(|_| None);\n        assert_eq!(result, None);\n    }\n\n    #[test]\n    fn test_option_chain_multiple() {\n        let x = Some(5);\n        let result = x\n            .chain(|n| Some(n * 2))\n            .chain(|n| Some(n + 1))\n            .chain(|n| Some(n.to_string()));\n        assert_eq!(result, Some(\"11\".to_string()));\n    }\n\n    #[test]\n    fn test_result_chain_ok() {\n        let x: Result<i32, String> = Ok(10);\n        let result = x.chain(|n| Ok(n * 2));\n        assert_eq!(result, Ok(20));\n    }\n\n    #[test]\n    fn test_result_chain_err() {\n        let x: Result<i32, String> = Err(\"error\".to_string());\n        let result = x.chain(|n: i32| Ok(n * 2));\n        assert_eq!(result, Err(\"error\".to_string()));\n    }\n\n    #[test]\n    fn test_result_chain_multiple() {\n        let x: Result<i32, String> = Ok(5);\n        let result = x\n            .chain(|n| Ok(n * 2))\n            .chain(|n| Ok(n + 10))\n            .chain(|n| Ok(n.to_string()));\n        assert_eq!(result, Ok(\"20\".to_string()));\n    }\n\n    // Monad Laws\n    #[test]\n    fn test_monad_left_identity_option() {\n        // Left identity: pure(a).chain(f) == f(a)\n        let a = 5;\n        let f = |x: i32| Some(x * 2);\n\n        let left = Some(a).chain(f);\n        let right = f(a);\n\n        assert_eq!(left, right);\n    }\n\n    #[test]\n    fn test_monad_right_identity_option() {\n        // Right identity: m.chain(pure) == m\n        let m = Some(5);\n        let result = m.chain(Some);\n        assert_eq!(result, m);\n    }\n\n    #[test]\n    fn test_monad_associativity_option() {\n        // Associativity: m.chain(f).chain(g) == m.chain(|x| f(x).chain(g))\n        let m = Some(5);\n        let f = |x: i32| Some(x * 2);\n        let g = |x: i32| Some(x + 1);\n\n        let left = m.chain(f).chain(g);\n        let right = Some(5).chain(|x| f(x).chain(g));\n\n        assert_eq!(left, right);\n    }\n\n    #[test]\n    fn test_chain_validation_pattern() {\n        // Practical validation use case\n        fn validate_positive(n: i32) -> Result<i32, String> {\n            if n > 0 {\n                Ok(n)\n            } else {\n                Err(\"Must be positive\".to_string())\n            }\n        }\n\n        fn validate_even(n: i32) -> Result<i32, String> {\n            if n % 2 == 0 {\n                Ok(n)\n            } else {\n                Err(\"Must be even\".to_string())\n            }\n        }\n\n        let valid: Result<i32, String> = Ok(10);\n        let result = valid.chain(validate_positive).chain(validate_even);\n        assert_eq!(result, Ok(10));\n\n        let invalid: Result<i32, String> = Ok(5);\n        let result = invalid.chain(validate_positive).chain(validate_even);\n        assert_eq!(result, Err(\"Must be even\".to_string()));\n    }\n\n    #[test]\n    fn test_chain_with_functor_option() {\n        // Combining chain and fmap\n        let x = Some(5);\n        let result = x.fmap(|n| n * 2).chain(|n| Some(n + 10));\n        assert_eq!(result, Some(20));\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/monoid_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::compose::*;\n    use fp_core::empty::Empty;\n    use fp_core::identity::*;\n    use fp_core::semigroup::Semigroup;\n\n    fn foo(a: i32) -> i32 {\n        a + 20\n    }\n\n    #[test]\n    fn monoid_example() {\n        let z = 1 + 1;\n        let x = 1 + (2 + 3) == (1 + 2) + 3;\n        let y = [vec![1, 2, 3], vec![4, 5, 6]].concat();\n        let u = [vec![1, 2], vec![]].concat();\n        let i = compose!(foo, identity)(1) == compose!(identity, foo)(1);\n        assert_eq!(z, 2);\n        assert!(x);\n        assert_eq!(y, vec![1, 2, 3, 4, 5, 6]);\n        assert_eq!(u, vec![1, 2]);\n        assert!(i);\n    }\n\n    // Additional comprehensive monoid tests\n    #[test]\n    fn test_semigroup_i32_combine() {\n        let a = 5;\n        let b = 10;\n        let result = a.combine(b);\n        assert_eq!(result, 15);\n    }\n\n    #[test]\n    fn test_semigroup_string_combine() {\n        let a = \"hello\".to_string();\n        let b = \" world\".to_string();\n        let result = a.combine(b);\n        assert_eq!(result, \"hello world\");\n    }\n\n    #[test]\n    fn test_semigroup_vec_combine() {\n        let a = vec![1, 2, 3];\n        let b = vec![4, 5, 6];\n        let result = a.combine(b);\n        assert_eq!(result, vec![1, 2, 3, 4, 5, 6]);\n    }\n\n    #[test]\n    fn test_empty_i32() {\n        let result = i32::empty();\n        assert_eq!(result, 0);\n    }\n\n    #[test]\n    fn test_empty_string() {\n        let result = String::empty();\n        assert_eq!(result, \"\");\n    }\n\n    #[test]\n    fn test_empty_vec() {\n        let result: Vec<i32> = Vec::empty();\n        assert_eq!(result, vec![]);\n    }\n\n    // Monoid Laws\n    #[test]\n    fn test_monoid_left_identity_i32() {\n        // Left identity: empty().combine(x) == x\n        let x = 42;\n        let result = i32::empty().combine(x);\n        assert_eq!(result, x);\n    }\n\n    #[test]\n    fn test_monoid_right_identity_i32() {\n        // Right identity: x.combine(empty()) == x\n        let x = 42;\n        let result = x.combine(i32::empty());\n        assert_eq!(result, x);\n    }\n\n    #[test]\n    fn test_monoid_left_identity_string() {\n        let x = \"hello\".to_string();\n        let result = String::empty().combine(x.clone());\n        assert_eq!(result, x);\n    }\n\n    #[test]\n    fn test_monoid_right_identity_string() {\n        let x = \"hello\".to_string();\n        let result = x.clone().combine(String::empty());\n        assert_eq!(result, x);\n    }\n\n    #[test]\n    fn test_semigroup_associativity_i32() {\n        // Associativity: (a.combine(b)).combine(c) == a.combine(b.combine(c))\n        let a = 5;\n        let b = 10;\n        let c = 15;\n\n        let left = a.combine(b).combine(c);\n        let right = a.combine(b.combine(c));\n\n        assert_eq!(left, right);\n    }\n\n    #[test]\n    fn test_semigroup_associativity_string() {\n        let a = \"hello\".to_string();\n        let b = \" \".to_string();\n        let c = \"world\".to_string();\n\n        let left = a.clone().combine(b.clone()).combine(c.clone());\n        let right = a.combine(b.combine(c));\n\n        assert_eq!(left, right);\n    }\n\n    #[test]\n    fn test_monoid_multiple_combines() {\n        let values = vec![1, 2, 3, 4, 5];\n        let result = values\n            .into_iter()\n            .fold(i32::empty(), |acc, x| acc.combine(x));\n        assert_eq!(result, 15);\n    }\n\n    #[test]\n    fn test_practical_monoid_sum() {\n        // Practical example: summing a list\n        fn sum(numbers: Vec<i32>) -> i32 {\n            numbers\n                .into_iter()\n                .fold(i32::empty(), |acc, x| acc.combine(x))\n        }\n\n        assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);\n        assert_eq!(sum(vec![]), 0);\n    }\n\n    #[test]\n    fn test_practical_monoid_concat() {\n        // Practical example: concatenating strings\n        fn concat(strings: Vec<String>) -> String {\n            strings\n                .into_iter()\n                .fold(String::empty(), |acc, x| acc.combine(x))\n        }\n\n        assert_eq!(\n            concat(vec![\n                \"hello\".to_string(),\n                \" \".to_string(),\n                \"world\".to_string()\n            ]),\n            \"hello world\"\n        );\n        assert_eq!(concat(vec![]), \"\");\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/option_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use std::collections::HashMap;\n\n    #[test]\n    fn option_example() {\n        let mut cart = HashMap::new();\n        let mut item = HashMap::new();\n        item.insert(\"price\".to_string(), 12);\n        cart.insert(\"item\".to_string(), item);\n\n        fn get_item(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&HashMap<String, i32>> {\n            cart.get(\"item\")\n        }\n\n        fn get_price(item: &HashMap<String, i32>) -> Option<&i32> {\n            item.get(\"price\")\n        }\n\n        fn get_nested_price(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&i32> {\n            get_item(cart).and_then(get_price)\n        }\n\n        let price = get_nested_price(&cart);\n\n        match price {\n            Some(v) => assert_eq!(v, &12),\n            None => panic!(\"T_T\"),\n        }\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/partial_application_example.rs",
    "content": "#[test]\nfn partial_application() {\n    fn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 {\n        (a + b * b + c.pow(3) + d.pow(4)) * mul - off\n    }\n\n    let bar = partial!(foo(_, _, 10, 42, 10, 10));\n\n    assert_eq!(foo(15, 15, 10, 42, 10, 10), bar(15, 15));\n}\n"
  },
  {
    "path": "fp-examples/src/pointed_functor_example.rs",
    "content": "#[derive(Debug, PartialEq, Eq)]\npub enum Maybe<T> {\n    #[allow(dead_code)]\n    Nothing,\n    Just(T),\n}\n\nimpl<T> Maybe<T> {\n    #[allow(dead_code)]\n    pub fn of(x: T) -> Self {\n        Maybe::Just(x)\n    }\n}\n\n#[test]\nfn pointed_functor_example() {\n    let pointed_functor = Maybe::of(1);\n\n    assert_eq!(pointed_functor, Maybe::Just(1));\n}\n"
  },
  {
    "path": "fp-examples/src/predicate_example.rs",
    "content": "#[test]\nfn predicate_example() {\n    let predicate = |a: &i32| *a > 2;\n\n    let result = (vec![1, 2, 3, 4])\n        .into_iter()\n        .filter(predicate)\n        .collect::<Vec<i32>>();\n\n    assert_eq!(result, vec![3, 4]);\n}\n"
  },
  {
    "path": "fp-examples/src/purity_example.rs",
    "content": "#[test]\nfn purity() {\n    let greet = |name: &str| format!(\"Hi! {}\", name);\n\n    assert_eq!(\"Hi! Jason\", greet(\"Jason\"));\n}\n\n#[test]\nfn impure() {\n    let name = \"Jason\";\n\n    let greet = || -> String { format!(\"Hi! {}\", name) };\n\n    assert_eq!(\"Hi! Jason\", greet());\n}\n\n#[test]\nfn impure2() {\n    let mut greeting: String = \"\".to_string();\n\n    let mut greet = |name: &str| {\n        greeting = format!(\"Hi! {}\", name);\n    };\n\n    greet(\"Jason\");\n\n    assert_eq!(\"Hi! Jason\", greeting);\n}\n"
  },
  {
    "path": "fp-examples/src/referential_transparency_example.rs",
    "content": "#[test]\nfn referential_transparency() {\n    let greet = || \"Hello World!\";\n    let msg = greet();\n\n    assert_eq!(msg, \"Hello World!\");\n}\n"
  },
  {
    "path": "fp-examples/src/semigroup_example.rs",
    "content": "/*\n// Note that below are just example code. We no longer use manual semigroup implementation.\n// Instead we favour Add, Mul, Sub and etc from std.\nuse fp_core::semigroup::*;\n\n#[test]\nfn semigroup_test() {\n    let a = vec![1, 2];\n    let b = vec![3, 4];\n    let c = vec![5, 6];\n    assert_eq!(vec![1, 2].combine(&vec![3, 4]), vec![1, 2, 3, 4],);\n    assert_eq!(a.combine(&b).combine(&c), a.combine(&b.combine(&c)),);\n}\n*/\n"
  },
  {
    "path": "fp-examples/src/setoid_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use fp_core::setoid::*;\n\n    #[test]\n    fn setoid_example() {\n        assert!(vec![1, 2].equals(&vec![1, 2]));\n        assert!(Setoid::equals(&\"test\", &\"test\"));\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/side_effects_example.rs",
    "content": "#[cfg(test)]\nmod example {\n    use std::time::SystemTime;\n\n    #[test]\n    fn side_effects() {\n        let now = SystemTime::now();\n        println!(\"{:?}\", now);\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/type_signature_example.rs",
    "content": "#[test]\nfn type_signature_example() {\n    // add :: i32 -> i32 -> i32\n    #[allow(dead_code)]\n    fn add(x: i32) -> impl Fn(i32) -> i32 {\n        move |y| x + y\n    }\n\n    // increment :: i32 -> i32\n    #[allow(dead_code)]\n    fn increment(x: i32) -> i32 {\n        x + 1\n    }\n\n    // call :: (a -> b) -> a -> b\n    #[allow(dead_code)]\n    fn call<A, B>(f: &dyn Fn(A) -> B) -> impl Fn(A) -> B + '_ {\n        move |x| f(x)\n    }\n\n    // This time with an explicit lifetime\n    #[allow(dead_code)]\n    fn call2<'a, A, B>(f: &'a dyn Fn(A) -> B) -> impl Fn(A) -> B + 'a {\n        move |x| f(x)\n    }\n\n    // map :: (a -> b) -> [a] -> [b]\n    #[allow(dead_code)]\n    fn map<A, B>(f: &dyn Fn(A) -> B) -> impl Fn(A) -> B + '_ {\n        move |x| f(x)\n    }\n}\n"
  },
  {
    "path": "fp-examples/src/value.rs",
    "content": "#[test]\nfn value_example() {\n    let a = 5;\n    let b = vec![1, 2, 3];\n    let c = \"test\";\n}"
  },
  {
    "path": "rust-toolchain.toml",
    "content": "[toolchain]\nchannel = \"stable\"\n"
  }
]