master d020d6d607d5 cached
64 files
90.9 KB
28.0k tokens
175 symbols
1 requests
Download .txt
Repository: JasonShin/functional-programming-jargon.rs
Branch: master
Commit: d020d6d607d5
Files: 64
Total size: 90.9 KB

Directory structure:
gitextract_jfkkq38d/

├── .github/
│   └── workflows/
│       └── rust.yml
├── .gitignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── Cargo.toml
├── LICENSE
├── README.md
├── fp-core/
│   ├── Cargo.toml
│   └── src/
│       ├── applicative.rs
│       ├── apply.rs
│       ├── chain.rs
│       ├── comonad.rs
│       ├── compose.rs
│       ├── empty.rs
│       ├── extend.rs
│       ├── extract.rs
│       ├── foldable.rs
│       ├── functor.rs
│       ├── hkt.rs
│       ├── identity.rs
│       ├── lens.rs
│       ├── lib.rs
│       ├── monad.rs
│       ├── monoid.rs
│       ├── pure.rs
│       ├── semigroup.rs
│       └── setoid.rs
├── fp-examples/
│   ├── Cargo.toml
│   └── src/
│       ├── adt_example.rs
│       ├── anamorphism_example.rs
│       ├── applicative_example.rs
│       ├── arity_example.rs
│       ├── catamorphism_example.rs
│       ├── closure_example.rs
│       ├── comonad_example.rs
│       ├── continuation_example.rs
│       ├── contracts_example.rs
│       ├── currying_example.rs
│       ├── empty_example.rs
│       ├── endomorphism_example.rs
│       ├── foldable_example.rs
│       ├── function_composition_example.rs
│       ├── functor_example.rs
│       ├── hof_example.rs
│       ├── homomorphism_example.rs
│       ├── idempotent_example.rs
│       ├── isomorphism_example.rs
│       ├── lambda_example.rs
│       ├── lens_example.rs
│       ├── main.rs
│       ├── monad_example.rs
│       ├── monoid_example.rs
│       ├── option_example.rs
│       ├── partial_application_example.rs
│       ├── pointed_functor_example.rs
│       ├── predicate_example.rs
│       ├── purity_example.rs
│       ├── referential_transparency_example.rs
│       ├── semigroup_example.rs
│       ├── setoid_example.rs
│       ├── side_effects_example.rs
│       ├── type_signature_example.rs
│       └── value.rs
└── rust-toolchain.toml

================================================
FILE CONTENTS
================================================

================================================
FILE: .github/workflows/rust.yml
================================================
name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

env:
  RUST_BACKTRACE: 1
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Format Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Check formatting
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy (Lint)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings
        # -D warnings makes lint warnings fail CI (fixes Issue #34)

  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Run tests
        run: cargo test --all-features --workspace
      - name: Run doc tests
        run: cargo test --doc --workspace


================================================
FILE: .gitignore
================================================
/target
**/*.rs.bk

================================================
FILE: CODE_OF_CONDUCT.md
================================================
# Citizen Code of Conduct

## 1. Purpose

A 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).

This code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior.

We invite all those who participate in PureRust to help us create safe and positive experiences for everyone.

## 2. Open [Source/Culture/Tech] Citizenship

A 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.

Communities 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.

If 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.

## 3. Expected Behavior

The following behaviors are expected and requested of all community members:

 * Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community.
 * Exercise consideration and respect in your speech and actions.
 * Attempt collaboration before conflict.
 * Refrain from demeaning, discriminatory, or harassing behavior and speech.
 * 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.
 * Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations.

## 4. Unacceptable Behavior

The following behaviors are considered harassment and are unacceptable within our community:

 * Violence, threats of violence or violent language directed against another person.
 * Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language.
 * Posting or displaying sexually explicit or violent material.
 * Posting or threatening to post other people's personally identifying information ("doxing").
 * Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability.
 * Inappropriate photography or recording.
 * Inappropriate physical contact. You should have someone's consent before touching them.
 * Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances.
 * Deliberate intimidation, stalking or following (online or in person).
 * Advocating for, or encouraging, any of the above behavior.
 * Sustained disruption of community events, including talks and presentations.

## 5. Weapons Policy

No 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.

## 6. Consequences of Unacceptable Behavior

Unacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated.

Anyone asked to stop unacceptable behavior is expected to comply immediately.

If 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).

## 7. Reporting Guidelines

If 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.



Additionally, 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.

## 8. Addressing Grievances

If 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. 



## 9. Scope

We 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.

This 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.

## 10. Contact info

visualbbasic@gmail.com

## 11. License and attribution

The 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/). 

Portions 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).

_Revision 2.3. Posted 6 March 2017._

_Revision 2.2. Posted 4 February 2016._

_Revision 2.1. Posted 23 June 2014._

_Revision 2.0, adopted by the [Stumptown Syndicate](http://stumptownsyndicate.org) board on 10 January 2013. Posted 17 March 2013._


================================================
FILE: CONTRIBUTING.md
================================================
# Contributing to functional-programming-jargon.rs

All sort of contributions are welcome and there are no complicated rules with it.
We appreciate:

- New features
- Bug fixes
- Suggestions
- Ideas

## Issues

Feel free to submit issues, ideas, suggestions and enhancement requests.

## Contributing

Please refer to each project's style guidelines and guidelines for submitting patches and additions.
In general, we follow the "fork-and-pull" Git workflow.

1.  **Fork** the repo on GitHub
2.  **Clone** the project to your own machine
3.  **Commit** changes to your own branch
4.  **Push** your work back up to your fork
5.  Submit a **Pull request** so that we can review your changes

NOTE: Be sure to merge the latest from "upstream" before making a pull request!

## Development Environments

| type | version                |
| ---- | ---------------------- |
| OS   | Linux, Windows and Mac |

You will need to install Rust to work on this project. Installation instruction can be found at https://www.rust-lang.org/tools/install.

### Requirements

- **Rust stable** (1.70.0 or later recommended)
- **No nightly features required** - this project uses only stable Rust features

The project automatically uses the correct Rust version via `rust-toolchain.toml`.

### Running Tests

```bash
# Run all tests
cargo test --workspace

# Run tests for a specific package
cargo test -p fp-core

# Run with output
cargo test -- --nocapture
```

### Code Quality Checks

```bash
# Format code
cargo fmt --all

# Check formatting
cargo fmt --all -- --check

# Run clippy lints
cargo clippy --all-targets --all-features

# Fix clippy warnings automatically (where possible)
cargo clippy --all-targets --all-features --fix
```

## Copyright and Licensing

 is an open source project licensed under the MIT license.

functional-programming-jargon.rs does not require you to assign the copyright of your contributions, you retain the copyright.
functional-programming-jargon.rs does require that you make your contributions available under the MIT license in order to be
included in the main repo.

If appropriate, include the MIT license summary at the top of each file along with the copyright info.
If you are adding a new file that you wrote, include your name in the copyright notice in the license
summary at the top of the file.

## License Summary

You can copy and paste the MIT license summary from below.

```
MIT License

Copyright (c) 2026 Jason Shin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
```


================================================
FILE: Cargo.toml
================================================
[workspace]

members = [
  "fp-examples",
  "fp-core"
]


================================================
FILE: LICENSE
================================================
Copyright (c) 2026 Jason Shin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: README.md
================================================
<p align="center"><img src="https://i.imgur.com/pqQLDFz.png" width="30%" /></p>

[![Build Status](https://travis-ci.com/JasonShin/fp-core.rs.svg?branch=master)](https://travis-ci.com/JasonShin/fp-core.rs)

The project is a library for functional programming in Rust.

* [fp-core.rs](#fp-corers)
    * [installation](#installation)
* [functional programming jargon in rust](#functional-programming-jargon-in-rust)

# fp-core.rs

A library for functional programming in Rust.

It contains purely functional data structures to supplement the functional programming needs alongside
with the Rust Standard Library.

## Installation 

Add below line to your Cargo.toml

```rust
fp-core = "0.1.9"
```

If you have [Cargo Edit](https://github.com/killercup/cargo-edit) you may simply

```bash
$ cargo add fp-core
```

# functional programming jargon in rust

Functional programming (FP) provides many advantages, and its popularity has been increasing as a result.
However, each programming paradigm comes with its own unique jargon and FP is no exception. By providing a glossary,
we hope to make learning FP easier.

Where applicable, this document uses terms defined in the [Fantasy Land spec](https://github.com/fantasyland/fantasy-land)
and Rust programming language to give code examples.

The content of this section was drawn from [Functional Programming Jargon in Javascript](https://github.com/hemanth/functional-programming-jargon) and
we sincerely appreciate them for providing the initial baseline.

__Table of Contents__
<!-- RM(noparent,notop) -->

* [Arity](#arity)
* [Higher-Order Functions (HOF)](#higher-order-functions-hof)
* [Closure](#closure)
* [Partial Application](#partial-application)
* [Currying](#currying)
* [Auto Currying](#auto-currying)
* [Referential Transparency](#referential-transparency)
* [Lambda](#lambda)
* [Lambda Calculus](#lambda-calculus)
* [Purity](#purity)
* [Side effects](#side-effects)
* [Idempotent](#idempotent)
* [Function Composition](#function-composition)
* [Continuation](#continuation)
* [Point-Free Style](#point-free-style)
* [Predicate](#predicate)
* [Contracts](#contracts)
* [Category](#category)
* [Value](#value)
* [Constant](#constant)
* [Variance](#variance)
* [Higher Kinded Type](#higher-kinded-type-hkt)
* [Functor](#functor)
* [Pointed Functor](#pointed-functor)
* [Lifting](#lifting)
* [Equational Reasoning](#equational-reasoning)
* [Monoid](#monoid)
* [Monad](#monad)
* [Comonad](#comonad)
* [Applicative](#applicative)
* [Morphism](#morphism)
  * [Endomorphism](#endomorphism)
  * [Isomorphism](#isomorphism)
  * [Homomorphism](#homomorphism)
  * [Catamorphism](#catamorphism)
  * [Hylomorphism](#hylomorphism)
  * [Anamorphism](#anamorphism)
* [Setoid](#setoid)
* [Ord](#ord)
* [Semigroup](#semigroup)
* [Foldable](#foldable)
* [Lens](#lens)
* [Type Signature](#type-signature)
* [Algebraic data type](#algebraic-data-type)
  * [Sum Type](#sum-type)
  * [Product Type](#product-type)
* [Option](#option)
* [Functional Programming References](#functional-programming-references)
* [Function Programming development in Rust Language](#functional-programming-development-in-rust-language)
* [Inspiration](#inspiration)

## Arity

The number of arguments a function takes. From words like unary, binary, ternary, etc.
This word has the distinction of being composed of two suffixes, "-ary" and "-ity."
Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two.
Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin.
Likewise, a function that takes a variable number of arguments is called "variadic,"
whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding (see below).

```rust
let sum = |a: i32, b: i32| { a + b }; // The arity of sum is 2
```

## Higher-Order Functions (HOF)

A function which takes a function as an argument and/or returns a function.

```rust
let filter = | predicate: fn(&i32) -> bool, xs: Vec<i32> | {
    xs.into_iter().filter(predicate).collect::<Vec<i32>>()
};
```

```rust
let is_even = |x: &i32| { x % 2 == 0 };
```

```rust
filter(is_even, vec![1, 2, 3, 4, 5, 6]);
```

## Closure

A closure is a scope which retains variables available to a function when it's created. This is important for
[partial application](#partial-application) to work.

```rust
let add_to = |x: i32| move |y: i32| x + y;
```

We 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.

```rust
let add_to_five = add_to(5);
```

In 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`
and get back the desired number.

```rust
add_to_five(3); // => 8
```

Closures are commonly used in event handlers so that they still have access to variables defined in their parents when they
are eventually called.

__Further reading__
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)

## Partial Application

Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.

To achieve this easily, we will be using a [partial application crate](https://crates.io/crates/partial_application)

```rust
#[macro_use]
extern crate partial_application;

fn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 {
    (a + b*b + c.pow(3) + d.pow(4)) * mul - off
}

let bar = partial!( foo(_, _, 10, 42, 10, 10) );

assert_eq!(
    foo(15, 15, 10, 42, 10, 10),
    bar(15, 15)
); // passes
```

Partial application helps create simpler functions from more complex ones by baking in data when you have it.
Curried functions are automatically partially applied.

__Further reading__
* [Partial Application in Haskell](https://wiki.haskell.org/Partial_application)


## Currying

The process of converting a function that takes multiple arguments into a function that takes them one at a time.

Each time the function is called it only accepts one argument and returns a function that takes one argument until all arguments are passed.


```rust
fn add(x: i32) -> impl Fn(i32)-> i32 {
    move |y| x + y
}

let add5 = add(5);
add5(10); // 15
```

__Further reading__
* [Currying in Rust](https://hashnode.com/post/currying-in-rust-cjpfb0i2z00cm56s2aideuo4z)

## Auto Currying

Transforming a function that takes multiple arguments into one that if given less than its
correct number of arguments returns a function that takes the rest. When the function gets the correct number of
arguments it is then evaluated.

Although Auto Currying is not possible in Rust right now, there is a debate on this issue on the Rust forum:
https://internals.rust-lang.org/t/auto-currying-in-rust/149/22

## Referential Transparency

An expression that can be replaced with its value without changing the behavior of the program is said to be referentially transparent.

Say we have function greet:

```rust
let greet = || "Hello World!";
```

Any invocation of `greet()` can be replaced with `Hello World!` hence greet is referentially transparent.


## Lambda

An anonymous function that can be treated like a value.

```rust
fn  increment(i: i32) -> i32 { i + 1 }

let closure_annotated = |i: i32| { i + 1 };
let closure_inferred = |i| i + 1;
```

Lambdas are often passed as arguments to Higher-Order functions.
You can assign a lambda to a variable, as shown above.

## Lambda Calculus

A branch of mathematics that uses functions to create a [universal model of computation](https://en.wikipedia.org/wiki/Lambda_calculus).

This is in contrast to a [Turing machine](https://www.youtube.com/watch?v=dNRDvLACg5Q), an equivalent model.

Lambda calculus has three key components: variables, abstraction, and application. A variable is just some
symbol, say `x`. An abstraction is sort of a function: it binds variables into "formulae". Applications
are function calls. This is meaningless without examples.

The identity function (`|x| x` in rust) looks like `\ x. x` in most literature (`\` is a Lambda where Latex
or Unicode make it available). It is an abstraction. If `1` were a value we could use, `(\ x. x) 1` would
be an application (and evaluating it gives you `1`).

But there's more...

__Computation in Pure Lambda Calculus__

Let's invent booleans. `\ x y. x` can be true and `\ x y. y` can be false.
If so, `\ b1 b2. b1(b2,(\x y. y))` is `and`. Let's evaluate it to show how:

| `b1` | `b2` | Their `and` |
| --- | --- | --- |
| `\ x y. x` | `\x y. x` | `\x y. x` |
| `\ x y. x` | `\x y. y` | `\x y. y` |
| `\ x y. y` | `\x y. y` | `\x y. y` |
| `\ x y. y` | `\x y. x` | `\x y. y` |

I'll leave `or` as an exercise. Furthermore, `if` can now be implemented: `\c t e. c(t, e)` where `c` is the condition, `t`
the consequent (`then`) and `e` the else clause.

[SICP leaves numbers as an exercise.](https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-14.html#%_idx_1474)
They define 0 as `\f . \x. x` and adding one as `\n. \f. \x. f(n(f)(x))`.
That isn't even ASCII art, so let's add: `0 + 1`:

```
(\n. \f. \x. f(n(f)(x)))(\f. \x. x) = \f. \x. f((\x'. x')(x)) = \f. \x. f(x)
```

Basically, the number of `f`s in the expression is the number. I'll leave figuring out larger numbers as a exercise.
With 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))`
should add two numbers. Let's make 4:

```
(\n m. \f. \x. n(f)(m(f)(x)))(\f. x. f(f(x)), \f. \x. f(f(x)))
  = \f. \x. (\f'. \x'. f'(f'(x')))(f)((\f'. \x'. f'(f'(x')))(f)(x))
  = \f. \x. (\x'. f(f(x')))(f(f(x')))
  = \f. \x. f(f(f(f(x))))
```

Multiplication is harder and there's better
[exposition on Wikipedia](https://en.wikipedia.org/wiki/Church_encoding#Calculation_with_Church_numerals).
Another good reference is [on stackoverflow](https://stackoverflow.com/questions/3077908/church-numeral-for-addition).

## Purity

A function is pure if the return value is only determined by its input values, and does not produce side effects.

```rust
let greet = |name: &str| { format!("Hi! {}", name) };

greet("Jason"); // Hi! Jason
```

As opposed to each of the following:

```rust
let name = "Jason";

let greet = || -> String {
    format!("Hi! {}", name)
};

greet(); // String = "Hi! Jason"
```

The above example's output is based on data stored outside of the function...

```rust
let mut greeting: String = "".to_string();

let mut greet = |name: &str| {
    greeting = format!("Hi! {}", name);
};

greet("Jason");

assert_eq!("Hi! Jason", greeting); // Passes
```

... and this one modifies state outside of the function.

## Side effects

A function or expression is said to have a side effect if apart from returning a value,
it interacts with (reads from or writes to) external mutable state.

```rust
use std::time::SystemTime;

let now = SystemTime::now();
```

```rust
println!("IO is a side effect!");
// IO is a side effect!
```

## Idempotent

A function is idempotent if reapplying it to its result does not produce a different result.

```rust
// Custom immutable sort method
let sort = |x: Vec<i32>| -> Vec<i32> {
    let mut x = x;
    x.sort();
    x
};
```

Then we can use the sort method like

```rust
let x = vec![2 ,1];
let sorted_x = sort(sort(x.clone()));
let expected = vec![1, 2];
assert_eq!(sorted_x, expected); // passes
```

```rust
let abs = | x: i32 | -> i32 {
    x.abs()
};

let x: i32 = 10;
let result = abs(abs(x));
assert_eq!(result, x); // passes
```

## Function Composition

The act of putting two functions together to form a third function where the output of one function is the input of the other.
Below is an example of compose function is Rust.

```rust
macro_rules! compose {
    ( $last:expr ) => { $last };
    ( $head:expr, $($tail:expr), +) => {
        compose_two($head, compose!($($tail),+))
    };
}

fn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C
where
    F: Fn(A) -> B,
    G: Fn(B) -> C,
{
    move |x| g(f(x))
}
```

Then we can use it like

```rust
let add = | x: i32 | x + 2;
let multiply = | x: i32 | x * 2;
let divide = | x: i32 | x / 2;

let intermediate = compose!(add, multiply, divide);

let subtract = | x: i32 | x - 1;

let finally = compose!(intermediate, subtract);

let expected = 11;
let result = finally(10);
assert_eq!(result, expected); // passes
```

## Continuation

At any given point in a program, the part of the code that's yet to be executed is known as a continuation.

```rust
let print_as_string = |num: i32| println!("Given {}", num);

let add_one_and_continue = |num: i32, cc: fn(i32)| {
    let result = num + 1;
    cc(result)
};

add_one_and_continue(1, print_as_string); // Given 2
```

Continuations are often seen in asynchronous programming when the program needs to wait to receive data before it can continue.
 The response is often passed off to the rest of the program, which is the continuation, once it's been received.

## Point-Free style

Writing functions where the definition does not explicitly identify the arguments used.
This style usually requires currying or other Higher-Order functions. A.K.A Tacit programming.

## Predicate

A predicate is a function that returns true or false for a given value.
A common use of a predicate is as the callback for array filter.

```rust
let predicate = | a: &i32 | a.clone() > 2;

let result = (vec![1, 2, 3, 4]).into_iter().filter(predicate).collect::<Vec<i32>>();

assert_eq!(result, vec![3, 4]); // passes
```

## Contracts

A contract specifies the obligations and guarantees of the behavior from a function or expression at runtime.
This acts as a set of rules that are expected from the input and output of a function or expression,
and errors are generally reported whenever a contract is violated.

```rust
let contract = | x: &i32 | -> bool {
    x > &10
};

let add_one = | x: &i32 | -> Result<i32, String> {
    if contract(x) {
        return Ok(x + 1);
    }
    Err("Cannot add one".to_string())
};
```

Then you can use `add_one` like

```rust
let expected = 12;
match add_one(&11) {
    Ok(x) => assert_eq!(x, expected),
    _ => panic!("Failed!")
}
```

## Category

A category in category theory is a collection of objects and morphisms between them. In programming, typically types
act as the objects and functions as morphisms.

To be a valid category 3 rules must be met:

1. There must be an identity morphism that maps an object to itself.
    Where `a` is an object in some category,
    there must be a function from `a -> a`.
2. Morphisms must compose.
    Where `a`, `b`, and `c` are objects in some category,
    and `f` is a morphism from `a -> b`, and `g` is a morphism from `b -> c`;
    `g(f(x))` must be equivalent to `(g • f)(x)`.
3. Composition must be associative
    `f • (g • h)` is the same as `(f • g) • h`

Since these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.
In particular, many see this as another foundation of mathematics (so, everything would be a category from this view of math).
Various definitions in this guide are related to category theory since this approach applies elegantly to functional programming.

__Examples of categories__

When one specifies a category, the objects and morphisms are essential. Additionally, showing that the rules are met is nice
though usually left to the reader as an exercise.

* Any type and pure functions on the type. (Note, we require purity since side-effects affect associativity (the third rule).)

These examples come up in mathematics:

* 1: the category with 1 object and its identity morphism.
* Monoidal categories: [monoids are defined later](#monoids) but any monoid is a category with 1 object and many morphisms
  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
  category.)

__Further reading__

* [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)
* [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.

## Value

Anything that can be assigned to a variable.

```rust
let a = 5;
let b = vec![1, 2, 3];
let c = "test";
```

## Constant

A variable that cannot be reassigned once defined.

```rust
let a = 5;
a = 3; // error!
```

Constants are [referentially transparent](#referential-transparency).
That is, they can be replaced with the values that they represent without affecting the result.

## Variance

Variance in functional programming refers to subtyping between more complex types related to subtyping between
their components.

Unlike 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)
or [functional programming language like Scala or Haskell](https://medium.com/@wiemzin/variances-in-scala-9c7d17af9dc4)

Variance in Rust is used during the type checking against type and lifetime parameters. Here are examples:
- By default, all lifetimes are co-variant except for `'static` because it outlives all others
- `'static` is always contra-variant to others regardless of where it appears or used
- It is `in-variant` if you use `Cell<T>` or `UnsafeCell<T>` in `PhatomData`

**Further Reading**

- https://github.com/rust-lang/rustc-guide/blob/master/src/variance.md
- https://nearprotocol.com/blog/understanding-rust-lifetimes/

## Higher Kinded Type (HKT)

Rust does not support Higher Kinded Types [yet](https://github.com/rust-lang/rfcs/issues/324). First of all, HKT is a
type with a "hole" in it, so you can declare a type signature such as `trait Functor<F<A>>`.

Although 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)

An implementation example of above theory in Rust would look like below:

```rust
pub trait HKT<A, B> {
    type URI;
    type Target;
}

// Lifted Option
impl<A, B> HKT<A, B> for Option<A> {
    type URI = Self;
    type Target = Option<B>;
}
```

Higher Kinded Type is crucial for functional programming in general.

**Further Reading**

- https://gist.github.com/CMCDragonkai/a5638f50c87d49f815b8
- https://www.youtube.com/watch?v=ERM0mBPNLHc

## Functor

An object that implements a map function which,
while running over each value in the object to produce a new functor of the same type, adheres to two rules:

__Preserves identity__

```
object.map(x => x) ≍ object
```

__Composable__

```
object.map(compose(f, g)) ≍ object.map(g).map(f)
```

(`f`, `g` are arbitrary functions)

For example, below can be considered as a functor-like operation

```rust
let v: Vec<i32> = vec![1, 2, 3].into_iter().map(| x | x + 1).collect();

assert_eq!(v, vec![2, 3, 4]); // passes while mapping the original vector and returns a new vector
```

While leveraging the [HKT implementation](#higher-kinded-type-hkt), You can define a trait that represents Functor like below

```rust
pub trait Functor<A, B>: HKT<A, B> {
    fn fmap<F>(self, f: F) -> <Self as HKT<A, B>>::Target
        where F: FnOnce(A) -> B;
}
```

Then use it against a type such as [Option](#https://doc.rust-lang.org/std/option/index.html) like

```rust
impl<A, B> Functor<A, B> for Option<A> {
    fn fmap<F>(self, f: F) -> Self::Target
        where
            F: FnOnce(A) -> B
    {
        self.map(f)
    }
}

// This conflicts with the above, so isn't tested
// below and is commented out. TODO: add a careful
// implementation that makes sure Optional and this
// don't conflict.
impl<A, B, T> HKT<A, B> for T
where
    T: Sized + Iterator<Item = A>,
    U: Sized + Iterator<Item = B>,
{
    type URI = Self;
    type Target = U;
}

impl<A, B, T> Functor<A, B> for T
where
    T: Iterator<Item = A>,
{
    fn fmap<F>(self, f: F) -> Self::Target
    where
        F: FnOnce(A) -> B,
        A: Sized,
        B: Sized,
    {
        self.map(f)
    }
}

#[test]
fn test_functor() {
    let z = Option::fmap(Some(1), |x| x + 1).fmap(|x| x + 1); // Return Option<B>
    assert_eq!(z, Some(3)); // passes
}
```

__The Underlying Math__

The confusing fact is that functors are morphisms in the category of categories. Really, this means that
a functor from category `C` into `D` preserves properties of the category, so that the data is somewhat
preserved.

Technically, every category has a functor into the simplest (non-empty) category (1): since the category `1` just
has one object and one function, map all the objects and functions in whatever category you start from into the
thing in `1`. So, data isn't quite preserved in a "nice" sense. Such functors are called forgetful sometimes as
they drop structure.

However, less forgetful examples provide more insight and empower useful statements about types.
Unfortunately, these are rather heavy-handed in the mathematics they evoke.


## Pointed Functor

An object with an of function that puts any single value into it.

```rust
#[derive(Debug, PartialEq, Eq)]
enum Maybe<T> {
    Nothing,
    Just(T),
}


impl<T> Maybe<T> {
    fn of(x: T) -> Self {
        Maybe::Just(x)
    }
}
```

Then use it like

```rust
let pointed_functor = Maybe::of(1);

assert_eq!(pointed_functor, Maybe::Just(1));
```

## Lifting

Lifting in functional programming typically means to lift a function into a context (a Functor or Monad).
For example, give a function `a -> b` and lift it into a `List` then the signature would
look like `List[a] -> List[b]`.

**Further Reading**
- https://wiki.haskell.org/Lifting
- https://stackoverflow.com/questions/43482772/difference-between-lifting-and-higher-order-functions
- https://stackoverflow.com/questions/2395697/what-is-lifting-in-haskell/2395956

## Equational Reasoning

When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts.

## Monoid

An set with a binary function that "combines" pairs from that set into another element of the set.

One simple monoid is the addition of numbers:

```rust
1 + 1
// i32: 2
```

In this case numbers are the set and `+` is the function.

An "identity" value must also exist that when combined with a value doesn't change it.

The identity value for addition is `0`.

```rust
1 + 0
// i32: 1
```

It's also required that the grouping of operations will not affect the result (associativity):

```rust
1 + (2 + 3) == (1 + 2) + 3
// bool: true
```

Array concatenation also forms a monoid:

```rust
[vec![1, 2, 3], vec![4, 5, 6]].concat();
// Vec<i32>: vec![1, 2, 3, 4, 5, 6]
```

The identity value is empty array `[]`

```rust
[vec![1, 2], vec![]].concat();
// Vec<i32>: vec![1, 2]
```

If identity and compose functions are provided, functions themselves form a monoid:

```rust
fn identity<A>(a: A) -> A {
    a
}
```

`foo` is any function that takes one argument.

```rust
compose(foo, identity) ≍ compose(identity, foo) ≍ foo
```

We can express Monoid as a Rust trait and the type signature would look like below

```rust
use crate::applicative_example::Applicative;

trait Empty<A> {
    fn empty() -> A;
}

trait Monoid<A, F, B>: Empty<A> + Applicative<A, F, B>
where
    F: FnOnce(A) -> B,
{
}
```

According to Fantasy Land Specification, Monoid should implement `Empty` and `Applicative`.

## Monad

A [Monad](https://github.com/fantasyland/fantasy-land#monad) is a trait that implements `Applicative` and `Chain` specifications. `chain` is
like `map` except it un-nests the resulting nested object.

First, `Chain` type can be implemented like below:

```rust
pub trait Chain<A, B>: HKT<A, B> {
    fn chain<F>(self, f: F) -> <Self as HKT<A, B>>::Target
        where F: FnOnce(A) -> <Self as HKT<A, B>>::Target;
}

impl<A, B> Chain<A, B> for Option<A> {
    fn chain<F>(self, f: F) -> Self::Target
        where F: FnOnce(A) -> <Self as HKT<A, B>>::Target {
        self.and_then(f)
    }
}
```

Then `Monad` itself can simply derive `Chain` and `Applicative`

```rust
pub trait Monad<A, F, B>: Chain<A, B> + Applicative<A, F, B>
    where F: FnOnce(A) -> B {}

impl<A, F, B> Monad<A, F, B> for Option<A>
    where F: FnOnce(A) -> B {}

#[test]
fn monad_example() {
    let x = Option::of(Some(1)).chain(|x| Some(x + 1));
    assert_eq!(x, Some(2)); // passes
}
```

`pure` is also known as `return` in other functional languages. `flat_map` is also known as `bind` in other languages.

Importantly, it is worth noting that monads are a rather advanced topic in category theory. In fact, they are called
triples by some as they involve adjoint functors and their unit -- both of which are rare to see in functional programming.
The meme is to think of a monad as a burrito with "pure" being the act of taking a tortilla (the empty burrito) and
adding ingredients using "chain".

The purely mathematical presentation of monads does not look anything like this, but there is an equivalence.

## Comonad

An object that has `extract` and `extend` functions.

```rust
trait Extend<A, B>: Functor<A, B> + Sized {
    fn extend<W>(self, f: W) -> <Self as HKT<A, B>>::Target
    where
        W: FnOnce(Self) -> B;
}

trait Extract<A> {
    fn extract(self) -> A;
}

trait Comonad<A, B>: Extend<A, B> + Extract<A> {}
```

Then we can implement these types for Option

```rust
impl<A, B> Extend<A, B> for Option<A> {
    fn extend<W>(self, f: W) -> Self::Target
    where
        W: FnOnce(Self) -> B,
    {
        self.map(|x| f(Some(x)))
    }
}

impl<A> Extract<A> for Option<A> {
    fn extract(self) -> A {
        self.unwrap() // is there a better way to achieve this?
    }
}
```

Extract takes a value out of a comonad.

```rust
Some(1).extract(); // 1
```

Extend runs a function on the Comonad.

```rust
Some(1).extend(|co| co.extract() + 1); // Some(2)
```

This can be thought of as the reverse of a monad. In fact, this is called
the "dual" in category theory. (Basically, if you know what `A` is, a `coA`
is everything in `A`'s definition with the arrows reversed.)

## Applicative

An applicative functor is an object with an `ap` function. `ap` applies a function in the object to a value in another
object 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
this, we will introduce another [higher kinded type](#higher-kinded-type-hkt), called `HKT3` that is capable of doing this.

For this example, we will use Option datatype.

```rust
trait HKT3<A, B, C> {
    type Target2;
}

impl<A, B, C> HKT3<A, B, C> for Option<A> {
    type Target2 = Option<B>;
}
```

Since Applicative implements Apply for `ap`  and `Pure` for `of` according to [Fantasy Land specification](https://github.com/fantasyland/fantasy-land#applicative)
we must implement the types like below:

```rust

// Apply
trait Apply<A, F, B> : Functor<A, B> + HKT3<A, F, B>
    where F: FnOnce(A) -> B,
{
    fn ap(self, f: <Self as HKT3<A, F, B>>::Target2) -> <Self as HKT<A, B>>::Target;
}

impl<A, F, B> Apply<A, F, B> for Option<A>
    where F: FnOnce(A) -> B,
{
    fn ap(self, f: Self::Target2) -> Self::Target {
        self.and_then(|v| f.map(|z| z(v)))
    }
}

// Pure
trait Pure<A>: HKT<A, A> {
    fn of(self) -> <Self as HKT<A, A>>::Target;
}

impl<A> Pure<A> for Option<A> {
    fn of(self) -> Self::Target {
        self
    }
}

// Applicative
trait Applicative<A, F, B> : Apply<A, F, B> + Pure<A>
    where F: FnOnce(A) -> B,
{
} // Simply derives Apply and Pure

impl<A, F, B> Applicative<A, F, B> for Option<A>
    where F: FnOnce(A) -> B,
{
}
```

Then we can use Option Applicative like this:

```rust
let x = Option::of(Some(1)).ap(Some(|x| x + 1));
assert_eq!(x, Some(2));
```

## Morphism

A function that preserves the structure of its domain.
See [the category definition](#category) from more.

The first few (endomorphism, homomorphism, and isomorphism) are easier to
understand than the rest. The rest require the notion of an F-algebra.
The simpler Haskell declarations are listed in [the wikipedia on paramorphisms](https://en.wikipedia.org/wiki/Paramorphism)
but the notions have yet to be extended to more general category theory.
Briefly, the view of F-algebras is to take the set-theoretic definition of algebraic
objects and redefined them on a purely category theoretic footing: to move the ideas
away from sets containing elements to collections of objects with morphisms.
However, some of the ideas here have yet to be generalised into this movement.

### Endomorphism

A function where the input type is same as the output.

```rust
// uppercase :: &str -> String
let uppercase = |x: &str| x.to_uppercase();

// decrement :: i32 -> i32
let decrement = |x: i32| x - 1;
```

### Isomorphism

A pair of transformations between 2 types of objects that is invertible.

For example, 2D coordinates could be stored as a i32 vector [2,3] or a struct {x: 2, y: 3}.

```rust
#[derive(PartialEq, Debug)]
struct Coords {
    x: i32,
    y: i32,
}

let pair_to_coords = | pair: (i32, i32) | Coords { x: pair.0, y: pair.1 };
let coords_to_pair = | coords: Coords | (coords.x, coords.y);
assert_eq!(
    pair_to_coords((1, 2)),
    Coords { x: 1, y: 2 },
); // passes
assert_eq!(
    coords_to_pair(Coords { x: 1, y: 2 }),
    (1, 2),
); // passes
```

Isomorphisms are critical in making structures identical. Since we know that the struct above is
identical to a pair, all the functions that exist on the pair can exist on the struct. If `f`
is the isomorphism and `g` and endomorphism on the codomain: `f^{-1} g f` would extend `g`
to apply on the domain.

### Homomorphism

A homomorphism is just a structure preserving map. It is the older term of morphism.
In fact, a functor is just a homomorphism between categories as it preserves the original category's structure under the mapping.

```rust
assert_eq!(A::of(f).ap(A::of(x)), A::of(f(x))); // passes
assert_eq!(
    Either::of(|x: &str| x.to_uppercase(x)).ap(Either::of("oreos")),
    Either::of("oreos".to_uppercase),
); // passes
```

### Catamorphism

A `reduceRight` function that applies a function against an accumulator and each value of the array (from right-to-left)
to reduce it to a single value.

```rust
let sum = |xs: Vec<i32>| xs.iter().fold(0, |mut sum, &val| { sum += val; sum });

assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);
```

### Anamorphism

An `unfold` function. An `unfold` is the opposite of `fold` (`reduce`). It generates a list from a single value.

```rust
let count_down = unfold((8_u32, 1_u32), |state| {
    let (ref mut x1, ref mut x2) = *state;

    if *x1 == 0 {
        return None;
    }

    let next = *x1 - *x2;
    let ret = *x1;
    *x1 = next;

    Some(ret)
});

assert_eq!(
    count_down.collect::<Vec<u32>>(),
    vec![8, 7, 6, 5, 4, 3, 2, 1],
);
```

### Hylomorphism

The combination of anamorphism and catamorphism.

### Apomorphism

It's the opposite of paramorphism, just as anamorphism is the opposite of catamorphism.
Whereas with paramorphism, you combine with access to the accumulator and what has been accumulated,
apomorphism lets you unfold with the potential to return early.

## Setoid

This is a set with an equivalence relation.

An object that has an `equals` function which can be used to compare other objects of the same type.

It must obey following rules to be `Setoid`

1. `a.equals(a) == true` (reflexivity)
2. `a.equals(b) == b.equals(a)` (symmetry)
3. `a.equals(b)` and `b.equals(c)` then `a.equals(c)` (transitivity)

Make a Vector a setoid:

Note that I am treating `Self` / `self` like `a`.

```rust
trait Setoid {
    fn equals(&self, other: &Self) -> bool;
}

impl Setoid for Vec<i32> {
    fn equals(&self, other: &Self) -> bool {
        self.len() == other.len()
    }
}

assert_eq!(vec![1, 2].equals(&vec![1, 2]), true); // passes
```

In Rust standard library, it already provides [Eq](https://doc.rust-lang.org/std/cmp/trait.Eq.html), which
resembles Setoid that was discussed in this section. Also [Eq](https://doc.rust-lang.org/std/cmp/trait.Eq.html)
has `equals` implementations that covers a range of data structures that already exist in Rust.

## Ord

An object or value that implements Ord specification, also implements [Setoid](#setoid) specification.

The Ord object or value must satisfy below rules for all `a`, `b` or `c`:

1. totality: `a <= b` or `b <= a`
2. antisymmetric: `a <= b` and `b <= a`, then `a == b`
3. transivity: `a <= b` and `b <= c`, then `a <= c`

Rust documentation for Ord can be found here [Ord](https://doc.rust-lang.org/std/cmp/trait.Ord.html)

## Semigroup

An object that has a `combine` function that combines it with another object of the same type.

It must obey following rules to be `Semigroup`

1. `a.add(b).add(c)` is equivalent to `a.add(b.add(c))` (associativity)

```rust
use std::ops::Add;

pub trait Semigroup<M>: Add<M> {
}

assert_eq!(
    vec![1, 2].add(&vec![3, 4]),
    vec![1, 2, 3, 4],
); // passes

assert_eq!(
    a.add(&b).add(&c),
    a.add(&b.add(&c)),
); // passes
```

## Foldable

An object that has a `foldr/l` function that can transform that object into some other type.

`fold_right` is equivalent to Fantasy Land Foldable's `reduce`, which goes like:

`fantasy-land/reduce :: Foldable f => f a ~> ((b, a) -> b, b) -> b`

```rust
use fp_core::foldable::*;

let k = vec![1, 2, 3];
let result = k.reduce(0, |i, acc| i + acc);
assert_eq!(result, 6);
```

If you were to implement `Foldable` manually, the trait of it would look like below

```rust
use crate::hkt::HKT;
use crate::monoid::Monoid;

pub trait Foldable<A, B>: HKT<A, B> {
    fn reduce<F>(b: B, ba: F) -> <Self as HKT<A, B>>::Target
    where
        F: FnOnce(B, A) -> (B, B);

    fn fold_map<M, N, F>(m: M, fa: F) -> M
    where
        M: Monoid<N>,
        F: FnOnce(<Self as HKT<A, B>>::URI) -> M;

    fn reduce_right<F>(b: B, f: F) -> <Self as HKT<A, B>>::Target
    where
        F: FnOnce(A, B) -> (B, B);
}
```

## Lens

A lens is a type that pairs a getter and a non-mutating setter for some other data structure.

```rust
trait Lens<S, A> {
    fn over(s: &S, f: &Fn(Option<&A>) -> A) -> S {
        let result: A = f(Self::get(s));
        Self::set(result, &s)
    }
    fn get(s: &S) -> Option<&A>;
    fn set(a: A, s: &S) -> S;
}

#[derive(Debug, PartialEq, Clone)]
struct Person {
    name: String,
}

#[derive(Debug)]
struct PersonNameLens;

impl Lens<Person, String> for PersonNameLens {
    fn get(s: &Person) -> Option<&String> {
       Some(&s.name)
    }

    fn set(a: String, s: &Person) -> Person {
        Person {
            name: a,
        }
    }
}
```

Having the pair of get and set for a given data structure enables a few key features.

```rust
let e1 = Person {
    name: "Jason".to_string(),
};
let name = PersonNameLens::get(&e1);
let e2 = PersonNameLens::set("John".to_string(), &e1);
let expected = Person {
    name: "John".to_string()
};
let e3 = PersonNameLens::over(&e1, &|x: Option<&String>| {
    match x {
        Some(y) => y.to_uppercase(),
        None => panic!("T_T") // lol...
    }
});

assert_eq!(*name.unwrap(), e1.name); // passes
assert_eq!(e2, expected); // passes
assert_eq!(e3, Person { name: "JASON".to_string() }); // passes
```

Lenses are also composable. This allows easy immutable updates to deeply nested data.

```rust
struct FirstLens;

impl<A> Lens<Vec<A>, A> for FirstLens {
  fn get(s: &Vec<A>) -> Option<&A> {
     s.first()
  }

  fn set(a: A, s: &Vec<A>) -> Vec<A> {
      unimplemented!() // Nothing to set in FirstLens
  }
}

let people = vec![Person { name: "Jason" }, Person { name: "John" }];
Lens::over(composeL!(FirstLens, NameLens), &|x: Option<&String>| {
  match x {
      Some(y) => y.to_uppercase(),
      None => panic!("T_T")
  }
}, people); // vec![Person { name: "JASON" }, Person { name: "John" }];
```

**Further Reading**

- [A Little Lens Starter](https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial)
- [Monocle Scala](https://scalac.io/scala-optics-lenses-with-monocle/)

## Type Signature

Every function in Rust will indicate the types of their arguments and return values.

```rust
// add :: i32 -> i32 -> i32
fn add(x: i32) -> impl Fn(i32)-> i32 {
    move |y| x + y
}

// increment :: i32 -> i32
fn increment(x: i32) -> i32 {
    x + 1
}
```

If a function accepts another function as an argument it is wrapped in parentheses.

```rust
// call :: (a -> b) -> a -> b
fn call<A, B>(f: &Fn(A) -> B) -> impl Fn(A) -> B + '_ {
    move |x| f(x)
}
```

The letters `a`, `b`, `c`, `d` are used to signify that the argument can be of any type.
The following version of map takes `a` function that transforms `a` value of some type `a` into another type `b`,
an array of values of type `a`, and returns an array of values of type `b`.

```rust
// map :: (a -> b) -> [a] -> [b]
fn map<A, B>(f: &Fn(A) -> B) -> impl Fn(A) -> B + '_ {
    move |x| f(x)
}
```

**Further Reading**

- [Mostly Adequate Guide](https://drboolean.gitbooks.io/mostly-adequate-guide-old/content/ch7.html#tales-from-the-cryptic)
- [What is Hindley-Milner?](https://stackoverflow.com/questions/399312/what-is-hindley-milner/399392#399392)

## Algebraic data type

A composite type made from putting other types together. Two common classes of algebraic types are [sum](#sum-type) and [product](#product-type).

### Sum Type

A Sum type is the combination of two types together into another one.
It is called sum because the number of possible values in the result type is the sum of the input types.

Rust has `enum` that literally represent `sum` in ADT.

```rust
enum WeakLogicValues {
   True(bool),
   False(bool),
   HalfTrue(bool),
}
// WeakLogicValues = bool + otherbool + anotherbool
```

### Product Type

A product type combines types together in a way you're probably more familiar with:

```rust
struct Point {
    x: i32,
    y: i32,
}
// Point = i32 x i32
```

It's called a product because the total possible values of the data structure is the product of the different values.
Many languages have a tuple type which is the simplest formulation of a product type.

See also [Set Theory](https://en.wikipedia.org/wiki/Set_theory)

**Further Reading**

- [ADT in 4 different languages](https://blog.softwaremill.com/algebraic-data-types-in-four-languages-858788043d4e)
- [What are Sum Product and Pi Types](https://manishearth.github.io/blog/2017/03/04/what-are-sum-product-and-pi-types/)

## Option

Option is a [sum type](#sum-type) with two cases often called Some and None.

Option is useful for composing functions that might not return a value.

```rust
let mut cart = HashMap::new();
let mut item = HashMap::new();
item.insert(
    "price".to_string(),
    12
);
cart.insert(
    "item".to_string(),
    item,
);

fn get_item(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&HashMap<String, i32>> {
    cart.get("item")
}

fn get_price(item: &HashMap<String, i32>) -> Option<&i32> {
    item.get("price")
}

```

Use [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

```rust
fn get_nested_price(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&i32> {
    return get_item(cart).and_then(get_price);
}

let price = get_nested_price(&cart);

match price {
    Some(v) => assert_eq!(v, &12),
    None => panic!("T_T"),
}
```

`Option` is also known as `Maybe`. `Some` is sometimes called `Just`. `None` is sometimes called `Nothing`.

## Functional Programming references

- [Scala with Cats](https://underscore.io/books/scala-with-cats/)
- [Haskell Programming](http://haskellbook.com/)
- [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)
- [Category Theory Notes](http://cheng.staff.shef.ac.uk/catnotes/categorynotes-cheng.pdf)

## Functional Programming development in Rust Language

- [Higher Kinded Polymorphism RFC](https://github.com/rust-lang/rfcs/issues/324)
- [Currying in Rust](https://internals.rust-lang.org/t/currying-in-rust/10326)
- [Auto-Currying in Rust](https://internals.rust-lang.org/t/auto-currying-in-rust/149)

## Inspiration

As a community, we have chosen our value as "learn by teaching".
We want to share our knowledge with the world while we are learning.



================================================
FILE: fp-core/Cargo.toml
================================================
[package]
name = "fp-core"
version = "0.1.9"
authors = ["Jason Shin <visualbbasic@gmail.com>", "Heman Gandhi <hemangandhi@gmail.com>"]
edition = "2018"
description = "A library for functional programming in Rust"
license = "MIT"
keywords = [
  "fp",
  "haskell",
  "applicative",
  "functor",
  "monad",
]
readme = "../README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.8.0"


================================================
FILE: fp-core/src/applicative.rs
================================================
use crate::apply::Apply;
use crate::pure::Pure;

pub trait Applicative<A, B>: Apply<B> + Pure<A> {}

impl<A, B> Applicative<A, B> for Option<A> {}

impl<A, B, E> Applicative<A, B> for Result<A, E> {}


================================================
FILE: fp-core/src/apply.rs
================================================
use crate::functor::Functor;
use crate::hkt::HKT;

type Applicator<B, S> = <S as HKT<Box<dyn Fn(<S as HKT<B>>::Current) -> B>>>::Target;

pub trait Apply<B>: Functor<B> + HKT<Box<dyn Fn(<Self as HKT<B>>::Current) -> B>> {
    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target;
}

impl<A, B> Apply<B> for Option<A> {
    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {
        self.and_then(|v| f.map(|z| z(v)))
    }
}

impl<A, B, E> Apply<B> for Result<A, E> {
    fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {
        self.and_then(|v| f.map(|z| z(v)))
    }
}


================================================
FILE: fp-core/src/chain.rs
================================================
use crate::hkt::HKT;

pub trait Chain<B>: HKT<B> {
    fn chain<F>(self, f: F) -> <Self as HKT<B>>::Target
    where
        F: FnOnce(<Self as HKT<B>>::Current) -> <Self as HKT<B>>::Target;
}

impl<A, B> Chain<B> for Option<A> {
    fn chain<F>(self, f: F) -> Self::Target
    where
        F: FnOnce(A) -> <Self as HKT<B>>::Target,
    {
        self.and_then(f)
    }
}

impl<A, B, E> Chain<B> for Result<A, E> {
    fn chain<F>(self, f: F) -> Self::Target
    where
        F: FnOnce(A) -> <Self as HKT<B>>::Target,
    {
        self.and_then(f)
    }
}


================================================
FILE: fp-core/src/comonad.rs
================================================
use crate::extend::Extend;
use crate::extract::Extract;

pub trait Comonad<A, B>: Extend<B> + Extract<A> {}


================================================
FILE: fp-core/src/compose.rs
================================================
#[macro_export]
macro_rules! compose {
    ( $last:expr ) => { $last };
    ( $head:expr, $($tail:expr), +) => {
        compose_two($head, compose!($($tail),+))
    };
}

pub fn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C
where
    F: Fn(A) -> B,
    G: Fn(B) -> C,
{
    move |x| g(f(x))
}


================================================
FILE: fp-core/src/empty.rs
================================================
pub trait Empty {
    fn empty() -> Self;
}

macro_rules! numeric_empty_impl {
    ($($t:ty)*) => ($(
        impl Empty for $t {
            fn empty() -> Self {
                0
            }
        }
    )*)
}

numeric_empty_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

macro_rules! floating_numeric_empty_impl {
    ($($t:ty)*) => ($(
        impl Empty for $t {
            fn empty() -> Self {
                0.0
            }
        }
    )*)
}

floating_numeric_empty_impl! { f32 f64 }

impl<T> Empty for Vec<T> {
    fn empty() -> Vec<T> {
        vec![]
    }
}

impl Empty for String {
    fn empty() -> String {
        "".to_string()
    }
}


================================================
FILE: fp-core/src/extend.rs
================================================
use crate::functor::Functor;
use crate::hkt::HKT;

pub trait Extend<B>: Functor<B> + Sized {
    fn extend<W>(self, f: W) -> <Self as HKT<B>>::Target
    where
        W: FnOnce(Self) -> B;
}

impl<A, B> Extend<B> for Option<A> {
    fn extend<W>(self, f: W) -> Self::Target
    where
        W: FnOnce(Self) -> B,
    {
        self.map(|x| f(Some(x)))
    }
}

impl<A, B, E> Extend<B> for Result<A, E> {
    fn extend<W>(self, f: W) -> Self::Target
    where
        W: FnOnce(Self) -> B,
    {
        self.map(|x| f(Ok(x)))
    }
}


================================================
FILE: fp-core/src/extract.rs
================================================
pub trait Extract<A> {
    fn extract(self) -> A;
}

impl<A> Extract<A> for Option<A> {
    fn extract(self) -> A {
        self.unwrap() // is there a better way to achieve this?
    }
}

impl<A, E> Extract<A> for Result<A, E>
where
    E: std::fmt::Debug,
{
    fn extract(self) -> A {
        self.unwrap() // is there a better way to achieve this?
    }
}


================================================
FILE: fp-core/src/foldable.rs
================================================
use crate::hkt::HKT;
use crate::monoid::Monoid;

// Cheating: all HKT instances exist for any B,
// so HKT<B> here isn't about Self<B> having any meaning,
// it's about folding on some Self<A> -- the HKT lets us
// have an A to speak of.
pub trait Foldable<B>: HKT<B> + Sized {
    fn reduce<F>(self, b: B, ba: F) -> B
    where
        F: Fn(B, &<Self as HKT<B>>::Current) -> B;

    fn reduce_right<F>(self, b: B, f: F) -> B
    where
        F: Fn(&<Self as HKT<B>>::Current, B) -> B;
}

// Biggest hardship with trying to put this into the above:
// we cannot have B constrained to be a Monoid, so having
// a default implementation becomes impossible. That said,
// having this as a separate function might make more sense
// (in particular, it might be easier to implement Foldable for
// rust containers as above and not have to worry about our Monoid
// until "later" -- when this function is handy).
pub fn fold_map<M, C, F>(container: C, mapper: F) -> M
where
    M: Monoid,
    C: Foldable<M>,
    F: Fn(&<C as HKT<M>>::Current) -> M,
{
    container.reduce(M::empty(), |acc, curr| acc.combine(mapper(curr)))
}

impl<A, B> Foldable<B> for Vec<A> {
    fn reduce<F>(self, b: B, fa: F) -> B
    where
        F: Fn(B, &A) -> B,
    {
        self.iter().fold(b, fa)
    }

    // TODO: make sure this is correct.
    fn reduce_right<F>(self, b: B, fa: F) -> B
    where
        F: Fn(&A, B) -> B,
    {
        self.iter().rev().fold(b, |x, y| fa(y, x))
    }
}


================================================
FILE: fp-core/src/functor.rs
================================================
use crate::hkt::HKT;

pub trait Functor<B>: HKT<B> {
    fn fmap<F>(self, f: F) -> Self::Target
    where
        F: FnOnce(Self::Current) -> B;
}

impl<A, B> Functor<B> for Option<A> {
    fn fmap<F>(self, f: F) -> Self::Target
    where
        // A is Self::Current
        F: FnOnce(A) -> B,
    {
        self.map(f)
    }
}

impl<A, B, E> Functor<B> for Result<A, E> {
    fn fmap<F>(self, f: F) -> Self::Target
    where
        // A is Self::Current
        F: FnOnce(A) -> B,
    {
        self.map(f)
    }
}


================================================
FILE: fp-core/src/hkt.rs
================================================
use std::collections::HashMap;

// TODO: use a declarative macro (see https://github.com/rust-lang/rust/issues/39412) to make this
// one macro that is invoked repeatedly.

pub trait HKT<U> {
    type Current;
    type Target;
}

macro_rules! derive_hkt {
    ($t: ident) => {
        impl<T, U> HKT<U> for $t<T> {
            type Current = T;
            type Target = $t<U>;
        }
    };
}

derive_hkt!(Option);
derive_hkt!(Vec);

impl<T, U, E> HKT<U> for Result<T, E> {
    type Current = T;
    type Target = Result<U, E>;
}

pub trait HKT3<U1, U2> {
    type Current1;
    type Current2;
    type Target;
}

macro_rules! derive_hkt3 {
    ($t:ident) => {
        impl<T1, T2, U1, U2> HKT3<U1, U2> for $t<T1, T2> {
            // The currently contained types
            type Current1 = T1;
            type Current2 = T2;
            // How the U's get filled in.
            type Target = $t<U1, U2>;
        }
    };
}

derive_hkt3!(HashMap);


================================================
FILE: fp-core/src/identity.rs
================================================
pub fn identity<A>(a: A) -> A {
    a
}


================================================
FILE: fp-core/src/lens.rs
================================================
pub trait Lens<S, A> {
    fn over(s: &S, f: &dyn Fn(Option<&A>) -> A) -> S {
        let result: A = f(Self::get(s));
        Self::set(result, s)
    }
    fn get(s: &S) -> Option<&A>;
    fn set(a: A, s: &S) -> S;
}


================================================
FILE: fp-core/src/lib.rs
================================================
pub mod applicative;
pub mod apply;
pub mod chain;
pub mod comonad;
pub mod compose;
pub mod empty;
pub mod extend;
pub mod extract;
pub mod foldable;
pub mod functor;
pub mod hkt;
pub mod identity;
pub mod lens;
pub mod monad;
pub mod monoid;
pub mod pure;
pub mod semigroup;
pub mod setoid;


================================================
FILE: fp-core/src/monad.rs
================================================
use crate::applicative::Applicative;
use crate::chain::Chain;

pub trait Monad<A, B>: Chain<B> + Applicative<A, B> {}

impl<A, B> Monad<A, B> for Option<A> {}

impl<A, B, E> Monad<A, B> for Result<A, E> {}


================================================
FILE: fp-core/src/monoid.rs
================================================
use crate::empty::Empty;
use crate::semigroup::Semigroup;

pub trait Monoid: Empty + Semigroup {}

impl Monoid for i32 {}
impl Monoid for i64 {}
impl<T: Clone> Monoid for Vec<T> {}
impl Monoid for String {}


================================================
FILE: fp-core/src/pure.rs
================================================
use crate::hkt::HKT;

pub trait Pure<A>: HKT<A> {
    fn of(c: Self::Current) -> Self::Target;
}

impl<A> Pure<A> for Option<A> {
    fn of(a: A) -> Self::Target {
        Some(a)
    }
}

impl<A, E> Pure<A> for Result<A, E> {
    fn of(a: A) -> Self::Target {
        Ok(a)
    }
}


================================================
FILE: fp-core/src/semigroup.rs
================================================
pub trait Semigroup {
    fn combine(self, other: Self) -> Self;
}

macro_rules! semigroup_numeric_impl {
    ($($t:ty)*) => ($(
        impl Semigroup for $t {
            fn combine(self, other: Self) -> Self {
                self + other
            }
        }
    )*)
}

semigroup_numeric_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }

impl<T: Clone> Semigroup for Vec<T> {
    fn combine(self, other: Self) -> Self {
        let mut concat = self.to_vec();
        concat.extend_from_slice(&other);
        concat
    }
}

impl Semigroup for String {
    fn combine(self, other: Self) -> Self {
        format!("{}{}", self, other)
    }
}


================================================
FILE: fp-core/src/setoid.rs
================================================
pub trait Setoid {
    fn equals(&self, other: &Self) -> bool;
}

impl Setoid for Vec<i32> {
    fn equals(&self, other: &Self) -> bool {
        self.len() == other.len()
    }
}

impl Setoid for &str {
    fn equals(&self, other: &Self) -> bool {
        self.eq(other)
    }
}


================================================
FILE: fp-examples/Cargo.toml
================================================
[package]
name = "fp-examples"
version = "0.1.0"
authors = ["Jason Shin <visualbbasic@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
partial_application = "0.1.0"
itertools = "0.8.0"
fp-core = { path = "../fp-core" }


================================================
FILE: fp-examples/src/adt_example.rs
================================================
#[test]
fn adt_example() {
    #[allow(dead_code)]
    enum WeakLogicValues {
        True(bool),
        False(bool),
        HalfTrue(bool),
    }
    // WeakLogicValues = bool + otherbool + anotherbool

    #[allow(dead_code)]
    struct Point {
        x: i32,
        y: i32,
    }
}


================================================
FILE: fp-examples/src/anamorphism_example.rs
================================================
#[cfg(test)]
mod example {
    use itertools::unfold;

    #[test]
    fn anamorphism_example() {
        let count_down = unfold((8_u32, 1_u32), |state| {
            let (ref mut x1, ref mut x2) = *state;

            if *x1 == 0 {
                return None;
            }

            let next = *x1 - *x2;
            let ret = *x1;
            *x1 = next;

            Some(ret)
        });

        assert_eq!(
            count_down.collect::<Vec<u32>>(),
            vec![8, 7, 6, 5, 4, 3, 2, 1],
        );
    }
}


================================================
FILE: fp-examples/src/applicative_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::apply::*;
    use fp_core::pure::*;

    #[test]
    fn applicative_example() {
        let x = Option::of(1).ap(Some(Box::new(|x| x + 1)));
        assert_eq!(x, Some(2));
    }

    #[test]
    fn applicative_example_on_result() {
        let x = Result::<_, ()>::of(1).ap(Ok(Box::new(|x| x + 1)));
        assert_eq!(x, Ok(2));
    }

    // Additional comprehensive applicative tests
    #[test]
    fn test_option_pure() {
        let result = Option::of(5);
        assert_eq!(result, Some(5));
    }

    #[test]
    fn test_result_pure() {
        let result: Result<i32, String> = Result::of(5);
        assert_eq!(result, Ok(5));
    }

    #[test]
    fn test_option_apply() {
        let value = Some(5);
        let func = Some(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);
        let result = value.ap(func);
        assert_eq!(result, Some(10));
    }

    #[test]
    fn test_option_apply_none_value() {
        let value: Option<i32> = None;
        let func = Some(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);
        let result = value.ap(func);
        assert_eq!(result, None);
    }

    #[test]
    fn test_option_apply_none_func() {
        let value = Some(5);
        let func: Option<Box<dyn Fn(i32) -> i32>> = None;
        let result = value.ap(func);
        assert_eq!(result, None);
    }

    #[test]
    fn test_result_apply_ok() {
        let value: Result<i32, String> = Ok(5);
        let func: Result<Box<dyn Fn(i32) -> i32>, String> =
            Ok(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);
        let result = value.ap(func);
        assert_eq!(result, Ok(10));
    }

    #[test]
    fn test_result_apply_err_value() {
        let value: Result<i32, String> = Err("value error".to_string());
        let func: Result<Box<dyn Fn(i32) -> i32>, String> =
            Ok(Box::new(|x: i32| x * 2) as Box<dyn Fn(i32) -> i32>);
        let result = value.ap(func);
        assert_eq!(result, Err("value error".to_string()));
    }

    #[test]
    fn test_result_apply_err_func() {
        let value: Result<i32, String> = Ok(5);
        let func: Result<Box<dyn Fn(i32) -> i32>, String> = Err("func error".to_string());
        let result = value.ap(func);
        assert_eq!(result, Err("func error".to_string()));
    }

    // Applicative Laws
    #[test]
    fn test_applicative_identity_law_option() {
        // Identity: pure(id).ap(v) == v
        let v = Some(42);
        let id_func = Some(Box::new(|x: i32| x) as Box<dyn Fn(i32) -> i32>);
        let result = v.ap(id_func);
        assert_eq!(result, Some(42));
    }

    #[test]
    fn test_applicative_homomorphism_option() {
        // Homomorphism: pure(f).ap(pure(x)) == pure(f(x))
        let x = 5;
        let f = |n: i32| n * 2;

        let left = Option::of(x).ap(Some(Box::new(f) as Box<dyn Fn(i32) -> i32>));
        let right = Option::of(f(x));

        assert_eq!(left, right);
    }

    #[test]
    fn test_apply_with_type_change() {
        let value = Some(42);
        let func = Some(Box::new(|x: i32| x.to_string()) as Box<dyn Fn(i32) -> String>);
        let result = value.ap(func);
        assert_eq!(result, Some("42".to_string()));
    }
}


================================================
FILE: fp-examples/src/arity_example.rs
================================================
#[test]
fn arity() {
    let sum = |a: i32, b: i32| a + b;
    let result = sum(1, 2);
    assert_eq!(result, 3);
}


================================================
FILE: fp-examples/src/catamorphism_example.rs
================================================
#[test]
fn catamorphism_example() {
    let sum = |xs: Vec<i32>| {
        xs.iter().fold(0, |mut sum, &val| {
            sum += val;
            sum
        })
    };

    assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);
}


================================================
FILE: fp-examples/src/closure_example.rs
================================================
#[test]
fn closure() {
    let add_to = |x: i32| move |y: i32| x + y;

    let add_to_five = add_to(5);

    assert_eq!(add_to_five(3), 8);
}


================================================
FILE: fp-examples/src/comonad_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::extend::*;
    use fp_core::extract::*;

    #[test]
    fn comonad_test() {
        let z = Some(1).extend(|x| x.extract() + 1);
        assert_eq!(z, Some(2));
    }
}


================================================
FILE: fp-examples/src/continuation_example.rs
================================================
#[test]
fn continuation() {
    let print_as_string = |num: i32| println!("Given {}", num);

    let add_one_and_continue = |num: i32, cc: fn(i32)| {
        let result = num + 1;
        cc(result)
    };

    add_one_and_continue(1, print_as_string);
}


================================================
FILE: fp-examples/src/contracts_example.rs
================================================
#[test]
fn contracts_example() {
    let contract = |x: &i32| -> bool { x > &10 };

    let add_one = |x: &i32| -> Result<i32, String> {
        if contract(x) {
            return Ok(x + 1);
        }
        Err("Cannot add one".to_string())
    };

    let expected = 12;
    match add_one(&11) {
        Ok(x) => assert_eq!(x, expected),
        _ => panic!("Failed!"),
    }
}


================================================
FILE: fp-examples/src/currying_example.rs
================================================
#[test]
fn currying() {
    fn add(x: i32) -> impl Fn(i32) -> i32 {
        move |y| x + y
    }

    let add5 = add(5);
    let result = add5(10);
    assert_eq!(result, 15);
}


================================================
FILE: fp-examples/src/empty_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::empty::*;

    #[test]
    fn empty_example_vec() {
        let empty_vec = Vec::<i32>::empty();
        assert_eq!(empty_vec, vec![])
    }

    #[test]
    fn empty_example_string() {
        let empty_str = String::empty();
        assert_eq!(empty_str, "".to_string())
    }
}


================================================
FILE: fp-examples/src/endomorphism_example.rs
================================================
#[test]
fn endomorphism_example() {
    let uppercase = |x: &str| x.to_uppercase();
    let decrement = |x: i32| x - 1;

    assert_eq!(uppercase("abc"), "ABC".to_string());
    assert_eq!(decrement(1), 0);
}


================================================
FILE: fp-examples/src/foldable_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::empty::Empty;
    use fp_core::foldable::*;

    /*
    // Check out foldable.rs in fp-core
    pub trait Foldable<A, B>: HKT<A, B> {
        fn reduce<F>(b: B, ba: F) -> <Self as HKT<A, B>>::Target
        where
            F: FnOnce(B, A) -> (B, B);
    }
    */

    #[test]
    fn foldable_example() {
        let k = vec![1, 2, 3];
        let result = k.reduce(0, |i, acc| i + acc);
        assert_eq!(result, 6);
    }

    #[test]
    fn fold_map_example() {
        let k = vec![Some(1_i64), Some(2_i64), Some(3_i64), None];
        let result = fold_map(k, |&opt| opt.unwrap_or_default());
        assert_eq!(result, 6);
    }

    // Additional comprehensive foldable tests
    #[test]
    fn test_vec_reduce() {
        let vec = vec![1, 2, 3, 4, 5];
        let result = vec.reduce(0, |acc, x| acc + x);
        assert_eq!(result, 15);
    }

    #[test]
    fn test_vec_reduce_empty() {
        let vec: Vec<i32> = vec![];
        let result = vec.reduce(0, |acc, x| acc + x);
        assert_eq!(result, 0);
    }

    #[test]
    fn test_vec_reduce_multiply() {
        let vec = vec![1, 2, 3, 4];
        let result = vec.reduce(1, |acc, x| acc * x);
        assert_eq!(result, 24);
    }

    #[test]
    fn test_vec_reduce_string_concat() {
        let vec = vec!["hello", " ", "world"];
        let result = vec.reduce(String::new(), |acc, x| acc + x);
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_vec_reduce_max() {
        let vec = vec![5, 2, 8, 1, 9, 3];
        let result = vec.reduce(i32::MIN, |acc, x| acc.max(*x));
        assert_eq!(result, 9);
    }

    #[test]
    fn test_fold_map_sum() {
        let vec = vec![1, 2, 3, 4, 5];
        let result: i32 = fold_map(vec, |x| *x);
        assert_eq!(result, 15);
    }

    #[test]
    fn test_fold_map_string_concat() {
        let vec = vec![1, 2, 3];
        let result: String = fold_map(vec, |x| x.to_string());
        assert_eq!(result, "123");
    }

    #[test]
    fn test_fold_map_with_transformation() {
        let vec = vec![1, 2, 3];
        let result: i32 = fold_map(vec, |x| x * 2);
        assert_eq!(result, 12); // 2 + 4 + 6
    }

    #[test]
    fn test_fold_map_empty_vec() {
        let vec: Vec<i32> = vec![];
        let result: i32 = fold_map(vec, |x| *x);
        assert_eq!(result, i32::empty());
    }

    #[test]
    fn test_reduce_filter_operation() {
        let vec = vec![1, 2, 3, 4, 5, 6];
        let evens = vec.reduce(Vec::new(), |mut acc, x| {
            if x % 2 == 0 {
                acc.push(*x);
            }
            acc
        });
        assert_eq!(evens, vec![2, 4, 6]);
    }

    #[test]
    fn test_reduce_partition() {
        #[derive(Debug, PartialEq)]
        struct Partitioned {
            evens: Vec<i32>,
            odds: Vec<i32>,
        }

        let vec = vec![1, 2, 3, 4, 5];
        let result = vec.reduce(
            Partitioned {
                evens: vec![],
                odds: vec![],
            },
            |mut acc, x| {
                if x % 2 == 0 {
                    acc.evens.push(*x);
                } else {
                    acc.odds.push(*x);
                }
                acc
            },
        );

        assert_eq!(result.evens, vec![2, 4]);
        assert_eq!(result.odds, vec![1, 3, 5]);
    }

    #[test]
    fn test_reduce_count() {
        let vec = vec![1, 2, 3, 4, 5];
        let count = vec.reduce(0, |acc, _| acc + 1);
        assert_eq!(count, 5);
    }

    #[test]
    fn test_vec_reduce_right() {
        let vec = vec![1, 2, 3, 4];
        let result = vec.reduce_right(0, |x, acc| x + acc);
        assert_eq!(result, 10);
    }
}


================================================
FILE: fp-examples/src/function_composition_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::compose::*;

    #[test]
    fn function_composition() {
        let add = |x: i32| x + 2;
        let multiply = |x: i32| x * 2;
        let divide = |x: i32| x / 2;

        let intermediate = compose!(add, multiply, divide);

        let subtract = |x: i32| x - 1;

        let finally = compose!(intermediate, subtract);

        let expected = 11;
        let result = finally(10);

        assert_eq!(result, expected);
    }
}


================================================
FILE: fp-examples/src/functor_example.rs
================================================
// impl<A, B, T> HKT<A, B> for T
// where
//     T: Sized + Iterator<Item = A>,
//     U: Sized + Iterator<Item = B>,
// {
//     type URI = Self;
//     type Target = U;
// }
//
// impl<A, B, T> Functor<A, B> for T
// where
//     T: Iterator<Item = A>,
// {
//     fn fmap<F>(self, f: F) -> Self::Target
//     where
//         F: FnOnce(A) -> B,
//         A: Sized,
//         B: Sized,
//     {
//         self.map(f)
//     }
// }

#[cfg(test)]
mod example {
    use fp_core::functor::Functor;

    #[test]
    fn test_functor() {
        let z = Option::fmap(Some(1), |x| x + 1).fmap(|x| x + 1);
        assert_eq!(z, Some(3));

        // let v = vec![3, 4];
        // assert_eq!(vec![5, 6], v.iter().fmap(|x| x + 1).fmap(|x| x + 1));
    }

    #[test]
    fn test_functor_for_result() {
        let z = Result::<_, ()>::fmap(Ok(1), |x| x + 1).fmap(|x| x + 1);
        assert_eq!(z, Ok(3));
    }

    // Additional comprehensive functor tests
    #[test]
    fn test_option_functor_some() {
        let x = Some(5);
        let result = x.fmap(|n| n * 2);
        assert_eq!(result, Some(10));
    }

    #[test]
    fn test_option_functor_none() {
        let x: Option<i32> = None;
        let result = x.fmap(|n| n * 2);
        assert_eq!(result, None);
    }

    #[test]
    fn test_option_functor_chain() {
        let x = Some(5);
        let result = x.fmap(|n| n * 2).fmap(|n| n + 1);
        assert_eq!(result, Some(11));
    }

    #[test]
    fn test_option_functor_type_change() {
        let x = Some(5);
        let result = x.fmap(|n| n.to_string());
        assert_eq!(result, Some("5".to_string()));
    }

    #[test]
    fn test_result_functor_ok() {
        let x: Result<i32, String> = Ok(10);
        let result = x.fmap(|n| n * 3);
        assert_eq!(result, Ok(30));
    }

    #[test]
    fn test_result_functor_err() {
        let x: Result<i32, String> = Err("error".to_string());
        let result = x.fmap(|n| n * 3);
        assert_eq!(result, Err("error".to_string()));
    }

    #[test]
    fn test_result_functor_chain() {
        let x: Result<i32, String> = Ok(5);
        let result = x.fmap(|n| n * 2).fmap(|n| n + 10);
        assert_eq!(result, Ok(20));
    }

    #[test]
    fn test_result_functor_preserves_error() {
        let x: Result<i32, String> = Err("initial error".to_string());
        let result = x.fmap(|n| n * 2).fmap(|n| n + 10);
        assert_eq!(result, Err("initial error".to_string()));
    }

    #[test]
    fn test_result_functor_type_change() {
        let x: Result<i32, String> = Ok(42);
        let result = x.fmap(|n| format!("Number: {}", n));
        assert_eq!(result, Ok("Number: 42".to_string()));
    }

    #[test]
    fn test_functor_with_closure() {
        let multiplier = 3;
        let x = Some(5);
        let result = x.fmap(|n| n * multiplier);
        assert_eq!(result, Some(15));
    }

    // Functor Laws
    #[test]
    fn test_functor_identity_law_option() {
        // Identity law: fmap(id) == id
        let x = Some(42);
        let result = x.fmap(|n| n);
        assert_eq!(result, Some(42));
    }

    #[test]
    fn test_functor_identity_law_result() {
        let x: Result<i32, String> = Ok(42);
        let result = x.fmap(|n| n);
        assert_eq!(result, Ok(42));
    }

    #[test]
    fn test_functor_composition_law_option() {
        // Composition law: fmap(g . f) == fmap(f).fmap(g)
        let f = |x: i32| x + 1;
        let g = |x: i32| x * 2;

        let x1 = Some(5);
        let x2 = Some(5);

        let composed = x1.fmap(|x| g(f(x)));
        let chained = x2.fmap(f).fmap(g);

        assert_eq!(composed, chained);
    }

    #[test]
    fn test_functor_composition_law_result() {
        let f = |x: i32| x + 1;
        let g = |x: i32| x * 2;

        let x1: Result<i32, String> = Ok(5);
        let x2: Result<i32, String> = Ok(5);

        let composed = x1.fmap(|x| g(f(x)));
        let chained = x2.fmap(f).fmap(g);

        assert_eq!(composed, chained);
    }
}

/*
// Below is an old implementation
#[derive(Debug, PartialEq, Eq)]
pub enum Maybe<T> {
    Nothing,
    Just(T),
}

#[test]
fn functor_example_1() {
    let v: Vec<i32> = vec![1, 2, 3].into_iter().map(| x | x + 1).collect();

    assert_eq!(v, vec![2, 3, 4]);
}

pub trait Functor<'a, A, B, F>
    where
        A: 'a,
        F: Fn(&'a A) -> B {
    type Output;
    fn fmap(&'a self, f: F) -> Self::Output;
}

impl<'a, A, B, F> Functor<'a, A, B, F> for Maybe<A>
    where
        A: 'a,
        F: Fn(&'a A) -> B {

    type Output = Maybe<B>;
    fn fmap(&'a self, f: F) -> Maybe<B> {
        match *self {
            Maybe::Just(ref x) => Maybe::Just(f(x)),
            Maybe::Nothing => Maybe::Nothing,
        }
    }
}

#[test]
fn functor_example_2() {
    let just = Maybe::Just(7);
    let nothing = Maybe::fmap(&Maybe::Nothing, |x| x + 1);
    let other = Maybe::fmap(&just, |x| x + 1);
    assert_eq!(nothing, Maybe::Nothing);
    assert_eq!(other, Maybe::Just(8));
}
*/


================================================
FILE: fp-examples/src/hof_example.rs
================================================
#[test]
fn hof() {
    let filter = |predicate: fn(&i32) -> bool, xs: Vec<i32>| {
        // A good Reddit post on how Filter works https://www.reddit.com/r/rust/comments/3bmua6/can_someone_help_me_understand_stditerfilter/
        xs.into_iter().filter(predicate).collect::<Vec<i32>>()
    };

    let is_even = |x: &i32| x % 2 == 0;

    let result = filter(is_even, vec![1, 2, 3, 4, 5, 6]);

    assert_eq!(result, vec![2, 4, 6]);
}


================================================
FILE: fp-examples/src/homomorphism_example.rs
================================================
#[test]
fn homomorphism_example() {
    // Check out the README for a psuedo code
    assert!(true);
}


================================================
FILE: fp-examples/src/idempotent_example.rs
================================================
#[test]
fn idempotent_sort() {
    let sort = |x: Vec<i32>| -> Vec<i32> {
        let mut x = x;
        x.sort();
        x
    };

    let x = vec![2, 1];
    let sorted_x = sort(sort(x.clone()));
    let expected = vec![1, 2];
    assert_eq!(sorted_x, expected);
}

#[test]
fn idempotent_abs() {
    let abs = |x: i32| -> i32 { x.abs() };

    let x: i32 = 10;
    let result = abs(abs(x));
    assert_eq!(result, x);
}


================================================
FILE: fp-examples/src/isomorphism_example.rs
================================================
#[derive(PartialEq, Debug)]
#[allow(dead_code)]
struct Coords {
    x: i32,
    y: i32,
}

#[test]
fn isomorphism_example() {
    let pair_to_coords = |pair: (i32, i32)| Coords {
        x: pair.0,
        y: pair.1,
    };
    let coords_to_pair = |coords: Coords| (coords.x, coords.y);
    assert_eq!(pair_to_coords((1, 2)), Coords { x: 1, y: 2 },);
    assert_eq!(coords_to_pair(Coords { x: 1, y: 2 }), (1, 2),);
}


================================================
FILE: fp-examples/src/lambda_example.rs
================================================
#[test]
fn lambdas() {
    fn increment(i: i32) -> i32 {
        i + 1
    }
    let closure_annotated = |i: i32| i + 1;
    let closure_inferred = |i| i + 1;

    let inc = increment(3);
    let ca = closure_annotated(3);
    let ci = closure_inferred(3);

    assert_eq!(inc, 4);
    assert_eq!(ca, 4);
    assert_eq!(ci, 4);
}


================================================
FILE: fp-examples/src/lens_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::lens::Lens;

    #[derive(Debug, PartialEq, Clone)]
    struct Person {
        name: String,
    }

    #[derive(Debug)]
    struct PersonNameLens;

    impl Lens<Person, String> for PersonNameLens {
        fn get(s: &Person) -> Option<&String> {
            Some(&s.name)
        }

        #[allow(dead_code)]
        fn set(a: String, _s: &Person) -> Person {
            Person { name: a }
        }
    }

    struct FirstLens;

    impl<A> Lens<Vec<A>, A> for FirstLens {
        fn get(s: &Vec<A>) -> Option<&A> {
            s.first()
        }

        #[allow(dead_code)]
        fn set(_a: A, _s: &Vec<A>) -> Vec<A> {
            unimplemented!()
        }
    }

    #[test]
    fn lens_example() {
        let e1 = Person {
            name: "Jason".to_string(),
        };
        let name = PersonNameLens::get(&e1);
        let e2 = PersonNameLens::set("John".to_string(), &e1);
        let expected = Person {
            name: "John".to_string(),
        };
        let e3 = PersonNameLens::over(&e1, &|x: Option<&String>| match x {
            Some(y) => y.to_uppercase(),
            None => panic!("T_T"),
        });
        let rando = vec![1, 2];
        let e4 = FirstLens::get(&rando);

        assert_eq!(*name.unwrap(), e1.name);
        assert_eq!(e2, expected);
        assert_eq!(
            e3,
            Person {
                name: "JASON".to_string()
            }
        );
        assert_eq!(*e4.unwrap(), 1);
    }
}


================================================
FILE: fp-examples/src/main.rs
================================================
#[allow(unused_imports)]
#[macro_use]
extern crate partial_application;
#[allow(unused_imports)]
#[macro_use]
extern crate fp_core;

pub mod adt_example;
mod anamorphism_example;
mod applicative_example;
mod arity_example;
mod catamorphism_example;
mod closure_example;
mod comonad_example;
mod continuation_example;
mod contracts_example;
mod currying_example;
mod empty_example;
mod endomorphism_example;
mod foldable_example;
mod function_composition_example;
mod functor_example;
mod hof_example;
mod idempotent_example;
mod isomorphism_example;
mod lambda_example;
mod lens_example;
mod monad_example;
mod monoid_example;
mod option_example;
mod partial_application_example;
mod pointed_functor_example;
mod predicate_example;
mod purity_example;
mod referential_transparency_example;
mod semigroup_example;
mod setoid_example;
mod side_effects_example;
mod type_signature_example;

fn main() {
    println!("Welcome to fp-core!");
}


================================================
FILE: fp-examples/src/monad_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::chain::*;
    use fp_core::functor::Functor;
    use fp_core::pure::*;

    #[test]
    fn monad_example() {
        let x = Option::of(1).chain(|x| Some(x + 1));
        assert_eq!(x, Some(2));
    }

    #[test]
    fn monad_example_on_result() {
        let x = Result::<_, ()>::of(1).chain(|x| Ok(x + 1));
        assert_eq!(x, Ok(2));
    }

    // Additional comprehensive monad tests
    #[test]
    fn test_option_chain_some() {
        let x = Some(5);
        let result = x.chain(|n| Some(n * 2));
        assert_eq!(result, Some(10));
    }

    #[test]
    fn test_option_chain_none() {
        let x: Option<i32> = None;
        let result = x.chain(|n| Some(n * 2));
        assert_eq!(result, None);
    }

    #[test]
    fn test_option_chain_returns_none() {
        let x = Some(5);
        let result: Option<i32> = x.chain(|_| None);
        assert_eq!(result, None);
    }

    #[test]
    fn test_option_chain_multiple() {
        let x = Some(5);
        let result = x
            .chain(|n| Some(n * 2))
            .chain(|n| Some(n + 1))
            .chain(|n| Some(n.to_string()));
        assert_eq!(result, Some("11".to_string()));
    }

    #[test]
    fn test_result_chain_ok() {
        let x: Result<i32, String> = Ok(10);
        let result = x.chain(|n| Ok(n * 2));
        assert_eq!(result, Ok(20));
    }

    #[test]
    fn test_result_chain_err() {
        let x: Result<i32, String> = Err("error".to_string());
        let result = x.chain(|n: i32| Ok(n * 2));
        assert_eq!(result, Err("error".to_string()));
    }

    #[test]
    fn test_result_chain_multiple() {
        let x: Result<i32, String> = Ok(5);
        let result = x
            .chain(|n| Ok(n * 2))
            .chain(|n| Ok(n + 10))
            .chain(|n| Ok(n.to_string()));
        assert_eq!(result, Ok("20".to_string()));
    }

    // Monad Laws
    #[test]
    fn test_monad_left_identity_option() {
        // Left identity: pure(a).chain(f) == f(a)
        let a = 5;
        let f = |x: i32| Some(x * 2);

        let left = Some(a).chain(f);
        let right = f(a);

        assert_eq!(left, right);
    }

    #[test]
    fn test_monad_right_identity_option() {
        // Right identity: m.chain(pure) == m
        let m = Some(5);
        let result = m.chain(Some);
        assert_eq!(result, m);
    }

    #[test]
    fn test_monad_associativity_option() {
        // Associativity: m.chain(f).chain(g) == m.chain(|x| f(x).chain(g))
        let m = Some(5);
        let f = |x: i32| Some(x * 2);
        let g = |x: i32| Some(x + 1);

        let left = m.chain(f).chain(g);
        let right = Some(5).chain(|x| f(x).chain(g));

        assert_eq!(left, right);
    }

    #[test]
    fn test_chain_validation_pattern() {
        // Practical validation use case
        fn validate_positive(n: i32) -> Result<i32, String> {
            if n > 0 {
                Ok(n)
            } else {
                Err("Must be positive".to_string())
            }
        }

        fn validate_even(n: i32) -> Result<i32, String> {
            if n % 2 == 0 {
                Ok(n)
            } else {
                Err("Must be even".to_string())
            }
        }

        let valid: Result<i32, String> = Ok(10);
        let result = valid.chain(validate_positive).chain(validate_even);
        assert_eq!(result, Ok(10));

        let invalid: Result<i32, String> = Ok(5);
        let result = invalid.chain(validate_positive).chain(validate_even);
        assert_eq!(result, Err("Must be even".to_string()));
    }

    #[test]
    fn test_chain_with_functor_option() {
        // Combining chain and fmap
        let x = Some(5);
        let result = x.fmap(|n| n * 2).chain(|n| Some(n + 10));
        assert_eq!(result, Some(20));
    }
}


================================================
FILE: fp-examples/src/monoid_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::compose::*;
    use fp_core::empty::Empty;
    use fp_core::identity::*;
    use fp_core::semigroup::Semigroup;

    fn foo(a: i32) -> i32 {
        a + 20
    }

    #[test]
    fn monoid_example() {
        let z = 1 + 1;
        let x = 1 + (2 + 3) == (1 + 2) + 3;
        let y = [vec![1, 2, 3], vec![4, 5, 6]].concat();
        let u = [vec![1, 2], vec![]].concat();
        let i = compose!(foo, identity)(1) == compose!(identity, foo)(1);
        assert_eq!(z, 2);
        assert!(x);
        assert_eq!(y, vec![1, 2, 3, 4, 5, 6]);
        assert_eq!(u, vec![1, 2]);
        assert!(i);
    }

    // Additional comprehensive monoid tests
    #[test]
    fn test_semigroup_i32_combine() {
        let a = 5;
        let b = 10;
        let result = a.combine(b);
        assert_eq!(result, 15);
    }

    #[test]
    fn test_semigroup_string_combine() {
        let a = "hello".to_string();
        let b = " world".to_string();
        let result = a.combine(b);
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_semigroup_vec_combine() {
        let a = vec![1, 2, 3];
        let b = vec![4, 5, 6];
        let result = a.combine(b);
        assert_eq!(result, vec![1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn test_empty_i32() {
        let result = i32::empty();
        assert_eq!(result, 0);
    }

    #[test]
    fn test_empty_string() {
        let result = String::empty();
        assert_eq!(result, "");
    }

    #[test]
    fn test_empty_vec() {
        let result: Vec<i32> = Vec::empty();
        assert_eq!(result, vec![]);
    }

    // Monoid Laws
    #[test]
    fn test_monoid_left_identity_i32() {
        // Left identity: empty().combine(x) == x
        let x = 42;
        let result = i32::empty().combine(x);
        assert_eq!(result, x);
    }

    #[test]
    fn test_monoid_right_identity_i32() {
        // Right identity: x.combine(empty()) == x
        let x = 42;
        let result = x.combine(i32::empty());
        assert_eq!(result, x);
    }

    #[test]
    fn test_monoid_left_identity_string() {
        let x = "hello".to_string();
        let result = String::empty().combine(x.clone());
        assert_eq!(result, x);
    }

    #[test]
    fn test_monoid_right_identity_string() {
        let x = "hello".to_string();
        let result = x.clone().combine(String::empty());
        assert_eq!(result, x);
    }

    #[test]
    fn test_semigroup_associativity_i32() {
        // Associativity: (a.combine(b)).combine(c) == a.combine(b.combine(c))
        let a = 5;
        let b = 10;
        let c = 15;

        let left = a.combine(b).combine(c);
        let right = a.combine(b.combine(c));

        assert_eq!(left, right);
    }

    #[test]
    fn test_semigroup_associativity_string() {
        let a = "hello".to_string();
        let b = " ".to_string();
        let c = "world".to_string();

        let left = a.clone().combine(b.clone()).combine(c.clone());
        let right = a.combine(b.combine(c));

        assert_eq!(left, right);
    }

    #[test]
    fn test_monoid_multiple_combines() {
        let values = vec![1, 2, 3, 4, 5];
        let result = values
            .into_iter()
            .fold(i32::empty(), |acc, x| acc.combine(x));
        assert_eq!(result, 15);
    }

    #[test]
    fn test_practical_monoid_sum() {
        // Practical example: summing a list
        fn sum(numbers: Vec<i32>) -> i32 {
            numbers
                .into_iter()
                .fold(i32::empty(), |acc, x| acc.combine(x))
        }

        assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);
        assert_eq!(sum(vec![]), 0);
    }

    #[test]
    fn test_practical_monoid_concat() {
        // Practical example: concatenating strings
        fn concat(strings: Vec<String>) -> String {
            strings
                .into_iter()
                .fold(String::empty(), |acc, x| acc.combine(x))
        }

        assert_eq!(
            concat(vec![
                "hello".to_string(),
                " ".to_string(),
                "world".to_string()
            ]),
            "hello world"
        );
        assert_eq!(concat(vec![]), "");
    }
}


================================================
FILE: fp-examples/src/option_example.rs
================================================
#[cfg(test)]
mod example {
    use std::collections::HashMap;

    #[test]
    fn option_example() {
        let mut cart = HashMap::new();
        let mut item = HashMap::new();
        item.insert("price".to_string(), 12);
        cart.insert("item".to_string(), item);

        fn get_item(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&HashMap<String, i32>> {
            cart.get("item")
        }

        fn get_price(item: &HashMap<String, i32>) -> Option<&i32> {
            item.get("price")
        }

        fn get_nested_price(cart: &HashMap<String, HashMap<String, i32>>) -> Option<&i32> {
            get_item(cart).and_then(get_price)
        }

        let price = get_nested_price(&cart);

        match price {
            Some(v) => assert_eq!(v, &12),
            None => panic!("T_T"),
        }
    }
}


================================================
FILE: fp-examples/src/partial_application_example.rs
================================================
#[test]
fn partial_application() {
    fn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 {
        (a + b * b + c.pow(3) + d.pow(4)) * mul - off
    }

    let bar = partial!(foo(_, _, 10, 42, 10, 10));

    assert_eq!(foo(15, 15, 10, 42, 10, 10), bar(15, 15));
}


================================================
FILE: fp-examples/src/pointed_functor_example.rs
================================================
#[derive(Debug, PartialEq, Eq)]
pub enum Maybe<T> {
    #[allow(dead_code)]
    Nothing,
    Just(T),
}

impl<T> Maybe<T> {
    #[allow(dead_code)]
    pub fn of(x: T) -> Self {
        Maybe::Just(x)
    }
}

#[test]
fn pointed_functor_example() {
    let pointed_functor = Maybe::of(1);

    assert_eq!(pointed_functor, Maybe::Just(1));
}


================================================
FILE: fp-examples/src/predicate_example.rs
================================================
#[test]
fn predicate_example() {
    let predicate = |a: &i32| *a > 2;

    let result = (vec![1, 2, 3, 4])
        .into_iter()
        .filter(predicate)
        .collect::<Vec<i32>>();

    assert_eq!(result, vec![3, 4]);
}


================================================
FILE: fp-examples/src/purity_example.rs
================================================
#[test]
fn purity() {
    let greet = |name: &str| format!("Hi! {}", name);

    assert_eq!("Hi! Jason", greet("Jason"));
}

#[test]
fn impure() {
    let name = "Jason";

    let greet = || -> String { format!("Hi! {}", name) };

    assert_eq!("Hi! Jason", greet());
}

#[test]
fn impure2() {
    let mut greeting: String = "".to_string();

    let mut greet = |name: &str| {
        greeting = format!("Hi! {}", name);
    };

    greet("Jason");

    assert_eq!("Hi! Jason", greeting);
}


================================================
FILE: fp-examples/src/referential_transparency_example.rs
================================================
#[test]
fn referential_transparency() {
    let greet = || "Hello World!";
    let msg = greet();

    assert_eq!(msg, "Hello World!");
}


================================================
FILE: fp-examples/src/semigroup_example.rs
================================================
/*
// Note that below are just example code. We no longer use manual semigroup implementation.
// Instead we favour Add, Mul, Sub and etc from std.
use fp_core::semigroup::*;

#[test]
fn semigroup_test() {
    let a = vec![1, 2];
    let b = vec![3, 4];
    let c = vec![5, 6];
    assert_eq!(vec![1, 2].combine(&vec![3, 4]), vec![1, 2, 3, 4],);
    assert_eq!(a.combine(&b).combine(&c), a.combine(&b.combine(&c)),);
}
*/


================================================
FILE: fp-examples/src/setoid_example.rs
================================================
#[cfg(test)]
mod example {
    use fp_core::setoid::*;

    #[test]
    fn setoid_example() {
        assert!(vec![1, 2].equals(&vec![1, 2]));
        assert!(Setoid::equals(&"test", &"test"));
    }
}


================================================
FILE: fp-examples/src/side_effects_example.rs
================================================
#[cfg(test)]
mod example {
    use std::time::SystemTime;

    #[test]
    fn side_effects() {
        let now = SystemTime::now();
        println!("{:?}", now);
    }
}


================================================
FILE: fp-examples/src/type_signature_example.rs
================================================
#[test]
fn type_signature_example() {
    // add :: i32 -> i32 -> i32
    #[allow(dead_code)]
    fn add(x: i32) -> impl Fn(i32) -> i32 {
        move |y| x + y
    }

    // increment :: i32 -> i32
    #[allow(dead_code)]
    fn increment(x: i32) -> i32 {
        x + 1
    }

    // call :: (a -> b) -> a -> b
    #[allow(dead_code)]
    fn call<A, B>(f: &dyn Fn(A) -> B) -> impl Fn(A) -> B + '_ {
        move |x| f(x)
    }

    // This time with an explicit lifetime
    #[allow(dead_code)]
    fn call2<'a, A, B>(f: &'a dyn Fn(A) -> B) -> impl Fn(A) -> B + 'a {
        move |x| f(x)
    }

    // map :: (a -> b) -> [a] -> [b]
    #[allow(dead_code)]
    fn map<A, B>(f: &dyn Fn(A) -> B) -> impl Fn(A) -> B + '_ {
        move |x| f(x)
    }
}


================================================
FILE: fp-examples/src/value.rs
================================================
#[test]
fn value_example() {
    let a = 5;
    let b = vec![1, 2, 3];
    let c = "test";
}

================================================
FILE: rust-toolchain.toml
================================================
[toolchain]
channel = "stable"
Download .txt
gitextract_jfkkq38d/

├── .github/
│   └── workflows/
│       └── rust.yml
├── .gitignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── Cargo.toml
├── LICENSE
├── README.md
├── fp-core/
│   ├── Cargo.toml
│   └── src/
│       ├── applicative.rs
│       ├── apply.rs
│       ├── chain.rs
│       ├── comonad.rs
│       ├── compose.rs
│       ├── empty.rs
│       ├── extend.rs
│       ├── extract.rs
│       ├── foldable.rs
│       ├── functor.rs
│       ├── hkt.rs
│       ├── identity.rs
│       ├── lens.rs
│       ├── lib.rs
│       ├── monad.rs
│       ├── monoid.rs
│       ├── pure.rs
│       ├── semigroup.rs
│       └── setoid.rs
├── fp-examples/
│   ├── Cargo.toml
│   └── src/
│       ├── adt_example.rs
│       ├── anamorphism_example.rs
│       ├── applicative_example.rs
│       ├── arity_example.rs
│       ├── catamorphism_example.rs
│       ├── closure_example.rs
│       ├── comonad_example.rs
│       ├── continuation_example.rs
│       ├── contracts_example.rs
│       ├── currying_example.rs
│       ├── empty_example.rs
│       ├── endomorphism_example.rs
│       ├── foldable_example.rs
│       ├── function_composition_example.rs
│       ├── functor_example.rs
│       ├── hof_example.rs
│       ├── homomorphism_example.rs
│       ├── idempotent_example.rs
│       ├── isomorphism_example.rs
│       ├── lambda_example.rs
│       ├── lens_example.rs
│       ├── main.rs
│       ├── monad_example.rs
│       ├── monoid_example.rs
│       ├── option_example.rs
│       ├── partial_application_example.rs
│       ├── pointed_functor_example.rs
│       ├── predicate_example.rs
│       ├── purity_example.rs
│       ├── referential_transparency_example.rs
│       ├── semigroup_example.rs
│       ├── setoid_example.rs
│       ├── side_effects_example.rs
│       ├── type_signature_example.rs
│       └── value.rs
└── rust-toolchain.toml
Download .txt
SYMBOL INDEX (175 symbols across 52 files)

FILE: fp-core/src/applicative.rs
  type Applicative (line 4) | pub trait Applicative<A, B>: Apply<B> + Pure<A> {}

FILE: fp-core/src/apply.rs
  type Applicator (line 4) | type Applicator<B, S> = <S as HKT<Box<dyn Fn(<S as HKT<B>>::Current) -> ...
  type Apply (line 6) | pub trait Apply<B>: Functor<B> + HKT<Box<dyn Fn(<Self as HKT<B>>::Curren...
    method ap (line 7) | fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target;
  function ap (line 11) | fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {
  function ap (line 17) | fn ap(self, f: Applicator<B, Self>) -> <Self as HKT<B>>::Target {

FILE: fp-core/src/chain.rs
  type Chain (line 3) | pub trait Chain<B>: HKT<B> {
    method chain (line 4) | fn chain<F>(self, f: F) -> <Self as HKT<B>>::Target
  function chain (line 10) | fn chain<F>(self, f: F) -> Self::Target
  function chain (line 19) | fn chain<F>(self, f: F) -> Self::Target

FILE: fp-core/src/comonad.rs
  type Comonad (line 4) | pub trait Comonad<A, B>: Extend<B> + Extract<A> {}

FILE: fp-core/src/compose.rs
  function compose_two (line 9) | pub fn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C

FILE: fp-core/src/empty.rs
  type Empty (line 1) | pub trait Empty {
    method empty (line 2) | fn empty() -> Self;
    method empty (line 30) | fn empty() -> Vec<T> {
    method empty (line 36) | fn empty() -> String {

FILE: fp-core/src/extend.rs
  type Extend (line 4) | pub trait Extend<B>: Functor<B> + Sized {
    method extend (line 5) | fn extend<W>(self, f: W) -> <Self as HKT<B>>::Target
  function extend (line 11) | fn extend<W>(self, f: W) -> Self::Target
  function extend (line 20) | fn extend<W>(self, f: W) -> Self::Target

FILE: fp-core/src/extract.rs
  type Extract (line 1) | pub trait Extract<A> {
    method extract (line 2) | fn extract(self) -> A;
  function extract (line 6) | fn extract(self) -> A {
  function extract (line 15) | fn extract(self) -> A {

FILE: fp-core/src/foldable.rs
  type Foldable (line 8) | pub trait Foldable<B>: HKT<B> + Sized {
    method reduce (line 9) | fn reduce<F>(self, b: B, ba: F) -> B
    method reduce_right (line 13) | fn reduce_right<F>(self, b: B, f: F) -> B
  function fold_map (line 25) | pub fn fold_map<M, C, F>(container: C, mapper: F) -> M
  function reduce (line 35) | fn reduce<F>(self, b: B, fa: F) -> B
  function reduce_right (line 43) | fn reduce_right<F>(self, b: B, fa: F) -> B

FILE: fp-core/src/functor.rs
  type Functor (line 3) | pub trait Functor<B>: HKT<B> {
    method fmap (line 4) | fn fmap<F>(self, f: F) -> Self::Target
  function fmap (line 10) | fn fmap<F>(self, f: F) -> Self::Target
  function fmap (line 20) | fn fmap<F>(self, f: F) -> Self::Target

FILE: fp-core/src/hkt.rs
  type HKT (line 6) | pub trait HKT<U> {
  type Current (line 24) | type Current = T;
  type Target (line 25) | type Target = Result<U, E>;
  type HKT3 (line 28) | pub trait HKT3<U1, U2> {

FILE: fp-core/src/identity.rs
  function identity (line 1) | pub fn identity<A>(a: A) -> A {

FILE: fp-core/src/lens.rs
  type Lens (line 1) | pub trait Lens<S, A> {
    method over (line 2) | fn over(s: &S, f: &dyn Fn(Option<&A>) -> A) -> S {
    method get (line 6) | fn get(s: &S) -> Option<&A>;
    method set (line 7) | fn set(a: A, s: &S) -> S;

FILE: fp-core/src/monad.rs
  type Monad (line 4) | pub trait Monad<A, B>: Chain<B> + Applicative<A, B> {}

FILE: fp-core/src/monoid.rs
  type Monoid (line 4) | pub trait Monoid: Empty + Semigroup {}

FILE: fp-core/src/pure.rs
  type Pure (line 3) | pub trait Pure<A>: HKT<A> {
    method of (line 4) | fn of(c: Self::Current) -> Self::Target;
  function of (line 8) | fn of(a: A) -> Self::Target {
  function of (line 14) | fn of(a: A) -> Self::Target {

FILE: fp-core/src/semigroup.rs
  type Semigroup (line 1) | pub trait Semigroup {
    method combine (line 2) | fn combine(self, other: Self) -> Self;
    method combine (line 18) | fn combine(self, other: Self) -> Self {
    method combine (line 26) | fn combine(self, other: Self) -> Self {

FILE: fp-core/src/setoid.rs
  type Setoid (line 1) | pub trait Setoid {
    method equals (line 2) | fn equals(&self, other: &Self) -> bool;
    method equals (line 6) | fn equals(&self, other: &Self) -> bool {
    method equals (line 12) | fn equals(&self, other: &Self) -> bool {

FILE: fp-examples/src/adt_example.rs
  function adt_example (line 2) | fn adt_example() {

FILE: fp-examples/src/anamorphism_example.rs
  function anamorphism_example (line 6) | fn anamorphism_example() {

FILE: fp-examples/src/applicative_example.rs
  function applicative_example (line 7) | fn applicative_example() {
  function applicative_example_on_result (line 13) | fn applicative_example_on_result() {
  function test_option_pure (line 20) | fn test_option_pure() {
  function test_result_pure (line 26) | fn test_result_pure() {
  function test_option_apply (line 32) | fn test_option_apply() {
  function test_option_apply_none_value (line 40) | fn test_option_apply_none_value() {
  function test_option_apply_none_func (line 48) | fn test_option_apply_none_func() {
  function test_result_apply_ok (line 56) | fn test_result_apply_ok() {
  function test_result_apply_err_value (line 65) | fn test_result_apply_err_value() {
  function test_result_apply_err_func (line 74) | fn test_result_apply_err_func() {
  function test_applicative_identity_law_option (line 83) | fn test_applicative_identity_law_option() {
  function test_applicative_homomorphism_option (line 92) | fn test_applicative_homomorphism_option() {
  function test_apply_with_type_change (line 104) | fn test_apply_with_type_change() {

FILE: fp-examples/src/arity_example.rs
  function arity (line 2) | fn arity() {

FILE: fp-examples/src/catamorphism_example.rs
  function catamorphism_example (line 2) | fn catamorphism_example() {

FILE: fp-examples/src/closure_example.rs
  function closure (line 2) | fn closure() {

FILE: fp-examples/src/comonad_example.rs
  function comonad_test (line 7) | fn comonad_test() {

FILE: fp-examples/src/continuation_example.rs
  function continuation (line 2) | fn continuation() {

FILE: fp-examples/src/contracts_example.rs
  function contracts_example (line 2) | fn contracts_example() {

FILE: fp-examples/src/currying_example.rs
  function currying (line 2) | fn currying() {

FILE: fp-examples/src/empty_example.rs
  function empty_example_vec (line 6) | fn empty_example_vec() {
  function empty_example_string (line 12) | fn empty_example_string() {

FILE: fp-examples/src/endomorphism_example.rs
  function endomorphism_example (line 2) | fn endomorphism_example() {

FILE: fp-examples/src/foldable_example.rs
  function foldable_example (line 16) | fn foldable_example() {
  function fold_map_example (line 23) | fn fold_map_example() {
  function test_vec_reduce (line 31) | fn test_vec_reduce() {
  function test_vec_reduce_empty (line 38) | fn test_vec_reduce_empty() {
  function test_vec_reduce_multiply (line 45) | fn test_vec_reduce_multiply() {
  function test_vec_reduce_string_concat (line 52) | fn test_vec_reduce_string_concat() {
  function test_vec_reduce_max (line 59) | fn test_vec_reduce_max() {
  function test_fold_map_sum (line 66) | fn test_fold_map_sum() {
  function test_fold_map_string_concat (line 73) | fn test_fold_map_string_concat() {
  function test_fold_map_with_transformation (line 80) | fn test_fold_map_with_transformation() {
  function test_fold_map_empty_vec (line 87) | fn test_fold_map_empty_vec() {
  function test_reduce_filter_operation (line 94) | fn test_reduce_filter_operation() {
  function test_reduce_partition (line 106) | fn test_reduce_partition() {
  function test_reduce_count (line 134) | fn test_reduce_count() {
  function test_vec_reduce_right (line 141) | fn test_vec_reduce_right() {

FILE: fp-examples/src/function_composition_example.rs
  function function_composition (line 6) | fn function_composition() {

FILE: fp-examples/src/functor_example.rs
  function test_functor (line 29) | fn test_functor() {
  function test_functor_for_result (line 38) | fn test_functor_for_result() {
  function test_option_functor_some (line 45) | fn test_option_functor_some() {
  function test_option_functor_none (line 52) | fn test_option_functor_none() {
  function test_option_functor_chain (line 59) | fn test_option_functor_chain() {
  function test_option_functor_type_change (line 66) | fn test_option_functor_type_change() {
  function test_result_functor_ok (line 73) | fn test_result_functor_ok() {
  function test_result_functor_err (line 80) | fn test_result_functor_err() {
  function test_result_functor_chain (line 87) | fn test_result_functor_chain() {
  function test_result_functor_preserves_error (line 94) | fn test_result_functor_preserves_error() {
  function test_result_functor_type_change (line 101) | fn test_result_functor_type_change() {
  function test_functor_with_closure (line 108) | fn test_functor_with_closure() {
  function test_functor_identity_law_option (line 117) | fn test_functor_identity_law_option() {
  function test_functor_identity_law_result (line 125) | fn test_functor_identity_law_result() {
  function test_functor_composition_law_option (line 132) | fn test_functor_composition_law_option() {
  function test_functor_composition_law_result (line 147) | fn test_functor_composition_law_result() {

FILE: fp-examples/src/hof_example.rs
  function hof (line 2) | fn hof() {

FILE: fp-examples/src/homomorphism_example.rs
  function homomorphism_example (line 2) | fn homomorphism_example() {

FILE: fp-examples/src/idempotent_example.rs
  function idempotent_sort (line 2) | fn idempotent_sort() {
  function idempotent_abs (line 16) | fn idempotent_abs() {

FILE: fp-examples/src/isomorphism_example.rs
  type Coords (line 3) | struct Coords {
  function isomorphism_example (line 9) | fn isomorphism_example() {

FILE: fp-examples/src/lambda_example.rs
  function lambdas (line 2) | fn lambdas() {

FILE: fp-examples/src/lens_example.rs
  type Person (line 6) | struct Person {
  type PersonNameLens (line 11) | struct PersonNameLens;
    method get (line 14) | fn get(s: &Person) -> Option<&String> {
    method set (line 19) | fn set(a: String, _s: &Person) -> Person {
  type FirstLens (line 24) | struct FirstLens;
    method get (line 27) | fn get(s: &Vec<A>) -> Option<&A> {
    method set (line 32) | fn set(_a: A, _s: &Vec<A>) -> Vec<A> {
  function lens_example (line 38) | fn lens_example() {

FILE: fp-examples/src/main.rs
  function main (line 41) | fn main() {

FILE: fp-examples/src/monad_example.rs
  function monad_example (line 8) | fn monad_example() {
  function monad_example_on_result (line 14) | fn monad_example_on_result() {
  function test_option_chain_some (line 21) | fn test_option_chain_some() {
  function test_option_chain_none (line 28) | fn test_option_chain_none() {
  function test_option_chain_returns_none (line 35) | fn test_option_chain_returns_none() {
  function test_option_chain_multiple (line 42) | fn test_option_chain_multiple() {
  function test_result_chain_ok (line 52) | fn test_result_chain_ok() {
  function test_result_chain_err (line 59) | fn test_result_chain_err() {
  function test_result_chain_multiple (line 66) | fn test_result_chain_multiple() {
  function test_monad_left_identity_option (line 77) | fn test_monad_left_identity_option() {
  function test_monad_right_identity_option (line 89) | fn test_monad_right_identity_option() {
  function test_monad_associativity_option (line 97) | fn test_monad_associativity_option() {
  function test_chain_validation_pattern (line 110) | fn test_chain_validation_pattern() {
  function test_chain_with_functor_option (line 138) | fn test_chain_with_functor_option() {

FILE: fp-examples/src/monoid_example.rs
  function foo (line 8) | fn foo(a: i32) -> i32 {
  function monoid_example (line 13) | fn monoid_example() {
  function test_semigroup_i32_combine (line 28) | fn test_semigroup_i32_combine() {
  function test_semigroup_string_combine (line 36) | fn test_semigroup_string_combine() {
  function test_semigroup_vec_combine (line 44) | fn test_semigroup_vec_combine() {
  function test_empty_i32 (line 52) | fn test_empty_i32() {
  function test_empty_string (line 58) | fn test_empty_string() {
  function test_empty_vec (line 64) | fn test_empty_vec() {
  function test_monoid_left_identity_i32 (line 71) | fn test_monoid_left_identity_i32() {
  function test_monoid_right_identity_i32 (line 79) | fn test_monoid_right_identity_i32() {
  function test_monoid_left_identity_string (line 87) | fn test_monoid_left_identity_string() {
  function test_monoid_right_identity_string (line 94) | fn test_monoid_right_identity_string() {
  function test_semigroup_associativity_i32 (line 101) | fn test_semigroup_associativity_i32() {
  function test_semigroup_associativity_string (line 114) | fn test_semigroup_associativity_string() {
  function test_monoid_multiple_combines (line 126) | fn test_monoid_multiple_combines() {
  function test_practical_monoid_sum (line 135) | fn test_practical_monoid_sum() {
  function test_practical_monoid_concat (line 148) | fn test_practical_monoid_concat() {

FILE: fp-examples/src/option_example.rs
  function option_example (line 6) | fn option_example() {

FILE: fp-examples/src/partial_application_example.rs
  function partial_application (line 2) | fn partial_application() {

FILE: fp-examples/src/pointed_functor_example.rs
  type Maybe (line 2) | pub enum Maybe<T> {
  function of (line 10) | pub fn of(x: T) -> Self {
  function pointed_functor_example (line 16) | fn pointed_functor_example() {

FILE: fp-examples/src/predicate_example.rs
  function predicate_example (line 2) | fn predicate_example() {

FILE: fp-examples/src/purity_example.rs
  function purity (line 2) | fn purity() {
  function impure (line 9) | fn impure() {
  function impure2 (line 18) | fn impure2() {

FILE: fp-examples/src/referential_transparency_example.rs
  function referential_transparency (line 2) | fn referential_transparency() {

FILE: fp-examples/src/setoid_example.rs
  function setoid_example (line 6) | fn setoid_example() {

FILE: fp-examples/src/side_effects_example.rs
  function side_effects (line 6) | fn side_effects() {

FILE: fp-examples/src/type_signature_example.rs
  function type_signature_example (line 2) | fn type_signature_example() {

FILE: fp-examples/src/value.rs
  function value_example (line 2) | fn value_example() {
Condensed preview — 64 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (101K chars).
[
  {
    "path": ".github/workflows/rust.yml",
    "chars": 1142,
    "preview": "name: CI\n\non:\n  push:\n    branches: [master]\n  pull_request:\n    branches: [master]\n\nenv:\n  RUST_BACKTRACE: 1\n  CARGO_TE"
  },
  {
    "path": ".gitignore",
    "chars": 18,
    "preview": "/target\n**/*.rs.bk"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "chars": 6311,
    "preview": "# Citizen Code of Conduct\n\n## 1. Purpose\n\nA primary goal of PureRust is to be inclusive to the largest number of contrib"
  },
  {
    "path": "CONTRIBUTING.md",
    "chars": 3026,
    "preview": "# Contributing to functional-programming-jargon.rs\n\nAll sort of contributions are welcome and there are no complicated r"
  },
  {
    "path": "Cargo.toml",
    "chars": 56,
    "preview": "[workspace]\n\nmembers = [\n  \"fp-examples\",\n  \"fp-core\"\n]\n"
  },
  {
    "path": "LICENSE",
    "chars": 1054,
    "preview": "Copyright (c) 2026 Jason Shin\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this soft"
  },
  {
    "path": "README.md",
    "chars": 40748,
    "preview": "<p align=\"center\"><img src=\"https://i.imgur.com/pqQLDFz.png\" width=\"30%\" /></p>\n\n[![Build Status](https://travis-ci.com/"
  },
  {
    "path": "fp-core/Cargo.toml",
    "chars": 464,
    "preview": "[package]\nname = \"fp-core\"\nversion = \"0.1.9\"\nauthors = [\"Jason Shin <visualbbasic@gmail.com>\", \"Heman Gandhi <hemangandh"
  },
  {
    "path": "fp-core/src/applicative.rs",
    "chars": 200,
    "preview": "use crate::apply::Apply;\nuse crate::pure::Pure;\n\npub trait Applicative<A, B>: Apply<B> + Pure<A> {}\n\nimpl<A, B> Applicat"
  },
  {
    "path": "fp-core/src/apply.rs",
    "chars": 615,
    "preview": "use crate::functor::Functor;\nuse crate::hkt::HKT;\n\ntype Applicator<B, S> = <S as HKT<Box<dyn Fn(<S as HKT<B>>::Current) "
  },
  {
    "path": "fp-core/src/chain.rs",
    "chars": 559,
    "preview": "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   "
  },
  {
    "path": "fp-core/src/comonad.rs",
    "chars": 108,
    "preview": "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",
    "chars": 306,
    "preview": "#[macro_export]\nmacro_rules! compose {\n    ( $last:expr ) => { $last };\n    ( $head:expr, $($tail:expr), +) => {\n       "
  },
  {
    "path": "fp-core/src/empty.rs",
    "chars": 677,
    "preview": "pub trait Empty {\n    fn empty() -> Self;\n}\n\nmacro_rules! numeric_empty_impl {\n    ($($t:ty)*) => ($(\n        impl Empty"
  },
  {
    "path": "fp-core/src/extend.rs",
    "chars": 536,
    "preview": "use crate::functor::Functor;\nuse crate::hkt::HKT;\n\npub trait Extend<B>: Functor<B> + Sized {\n    fn extend<W>(self, f: W"
  },
  {
    "path": "fp-core/src/extract.rs",
    "chars": 360,
    "preview": "pub trait Extract<A> {\n    fn extract(self) -> A;\n}\n\nimpl<A> Extract<A> for Option<A> {\n    fn extract(self) -> A {\n    "
  },
  {
    "path": "fp-core/src/foldable.rs",
    "chars": 1471,
    "preview": "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"
  },
  {
    "path": "fp-core/src/functor.rs",
    "chars": 519,
    "preview": "use crate::hkt::HKT;\n\npub trait Functor<B>: HKT<B> {\n    fn fmap<F>(self, f: F) -> Self::Target\n    where\n        F: FnO"
  },
  {
    "path": "fp-core/src/hkt.rs",
    "chars": 956,
    "preview": "use std::collections::HashMap;\n\n// TODO: use a declarative macro (see https://github.com/rust-lang/rust/issues/39412) to"
  },
  {
    "path": "fp-core/src/identity.rs",
    "chars": 40,
    "preview": "pub fn identity<A>(a: A) -> A {\n    a\n}\n"
  },
  {
    "path": "fp-core/src/lens.rs",
    "chars": 219,
    "preview": "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 "
  },
  {
    "path": "fp-core/src/lib.rs",
    "chars": 293,
    "preview": "pub mod applicative;\npub mod apply;\npub mod chain;\npub mod comonad;\npub mod compose;\npub mod empty;\npub mod extend;\npub "
  },
  {
    "path": "fp-core/src/monad.rs",
    "chars": 206,
    "preview": "use crate::applicative::Applicative;\nuse crate::chain::Chain;\n\npub trait Monad<A, B>: Chain<B> + Applicative<A, B> {}\n\ni"
  },
  {
    "path": "fp-core/src/monoid.rs",
    "chars": 207,
    "preview": "use crate::empty::Empty;\nuse crate::semigroup::Semigroup;\n\npub trait Monoid: Empty + Semigroup {}\n\nimpl Monoid for i32 {"
  },
  {
    "path": "fp-core/src/pure.rs",
    "chars": 283,
    "preview": "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 Op"
  },
  {
    "path": "fp-core/src/semigroup.rs",
    "chars": 671,
    "preview": "pub trait Semigroup {\n    fn combine(self, other: Self) -> Self;\n}\n\nmacro_rules! semigroup_numeric_impl {\n    ($($t:ty)*"
  },
  {
    "path": "fp-core/src/setoid.rs",
    "chars": 280,
    "preview": "pub trait Setoid {\n    fn equals(&self, other: &Self) -> bool;\n}\n\nimpl Setoid for Vec<i32> {\n    fn equals(&self, other:"
  },
  {
    "path": "fp-examples/Cargo.toml",
    "chars": 314,
    "preview": "[package]\nname = \"fp-examples\"\nversion = \"0.1.0\"\nauthors = [\"Jason Shin <visualbbasic@gmail.com>\"]\nedition = \"2018\"\n\n# S"
  },
  {
    "path": "fp-examples/src/adt_example.rs",
    "chars": 289,
    "preview": "#[test]\nfn adt_example() {\n    #[allow(dead_code)]\n    enum WeakLogicValues {\n        True(bool),\n        False(bool),\n "
  },
  {
    "path": "fp-examples/src/anamorphism_example.rs",
    "chars": 526,
    "preview": "#[cfg(test)]\nmod example {\n    use itertools::unfold;\n\n    #[test]\n    fn anamorphism_example() {\n        let count_down"
  },
  {
    "path": "fp-examples/src/applicative_example.rs",
    "chars": 3243,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::apply::*;\n    use fp_core::pure::*;\n\n    #[test]\n    fn applicative_example("
  },
  {
    "path": "fp-examples/src/arity_example.rs",
    "chars": 116,
    "preview": "#[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",
    "chars": 218,
    "preview": "#[test]\nfn catamorphism_example() {\n    let sum = |xs: Vec<i32>| {\n        xs.iter().fold(0, |mut sum, &val| {\n         "
  },
  {
    "path": "fp-examples/src/closure_example.rs",
    "chars": 142,
    "preview": "#[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!("
  },
  {
    "path": "fp-examples/src/comonad_example.rs",
    "chars": 214,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::extend::*;\n    use fp_core::extract::*;\n\n    #[test]\n    fn comonad_test() {"
  },
  {
    "path": "fp-examples/src/continuation_example.rs",
    "chars": 255,
    "preview": "#[test]\nfn continuation() {\n    let print_as_string = |num: i32| println!(\"Given {}\", num);\n\n    let add_one_and_continu"
  },
  {
    "path": "fp-examples/src/contracts_example.rs",
    "chars": 382,
    "preview": "#[test]\nfn contracts_example() {\n    let contract = |x: &i32| -> bool { x > &10 };\n\n    let add_one = |x: &i32| -> Resul"
  },
  {
    "path": "fp-examples/src/currying_example.rs",
    "chars": 178,
    "preview": "#[test]\nfn currying() {\n    fn add(x: i32) -> impl Fn(i32) -> i32 {\n        move |y| x + y\n    }\n\n    let add5 = add(5);"
  },
  {
    "path": "fp-examples/src/empty_example.rs",
    "chars": 325,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::empty::*;\n\n    #[test]\n    fn empty_example_vec() {\n        let empty_vec = "
  },
  {
    "path": "fp-examples/src/endomorphism_example.rs",
    "chars": 209,
    "preview": "#[test]\nfn endomorphism_example() {\n    let uppercase = |x: &str| x.to_uppercase();\n    let decrement = |x: i32| x - 1;\n"
  },
  {
    "path": "fp-examples/src/foldable_example.rs",
    "chars": 3719,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::empty::Empty;\n    use fp_core::foldable::*;\n\n    /*\n    // Check out foldabl"
  },
  {
    "path": "fp-examples/src/function_composition_example.rs",
    "chars": 478,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::compose::*;\n\n    #[test]\n    fn function_composition() {\n        let add = |"
  },
  {
    "path": "fp-examples/src/functor_example.rs",
    "chars": 5000,
    "preview": "// impl<A, B, T> HKT<A, B> for T\n// where\n//     T: Sized + Iterator<Item = A>,\n//     U: Sized + Iterator<Item = B>,\n//"
  },
  {
    "path": "fp-examples/src/hof_example.rs",
    "chars": 436,
    "preview": "#[test]\nfn hof() {\n    let filter = |predicate: fn(&i32) -> bool, xs: Vec<i32>| {\n        // A good Reddit post on how F"
  },
  {
    "path": "fp-examples/src/homomorphism_example.rs",
    "chars": 103,
    "preview": "#[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",
    "chars": 423,
    "preview": "#[test]\nfn idempotent_sort() {\n    let sort = |x: Vec<i32>| -> Vec<i32> {\n        let mut x = x;\n        x.sort();\n     "
  },
  {
    "path": "fp-examples/src/isomorphism_example.rs",
    "chars": 418,
    "preview": "#[derive(PartialEq, Debug)]\n#[allow(dead_code)]\nstruct Coords {\n    x: i32,\n    y: i32,\n}\n\n#[test]\nfn isomorphism_exampl"
  },
  {
    "path": "fp-examples/src/lambda_example.rs",
    "chars": 330,
    "preview": "#[test]\nfn lambdas() {\n    fn increment(i: i32) -> i32 {\n        i + 1\n    }\n    let closure_annotated = |i: i32| i + 1;"
  },
  {
    "path": "fp-examples/src/lens_example.rs",
    "chars": 1507,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::lens::Lens;\n\n    #[derive(Debug, PartialEq, Clone)]\n    struct Person {\n    "
  },
  {
    "path": "fp-examples/src/main.rs",
    "chars": 939,
    "preview": "#[allow(unused_imports)]\n#[macro_use]\nextern crate partial_application;\n#[allow(unused_imports)]\n#[macro_use]\nextern cra"
  },
  {
    "path": "fp-examples/src/monad_example.rs",
    "chars": 3831,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::chain::*;\n    use fp_core::functor::Functor;\n    use fp_core::pure::*;\n\n    "
  },
  {
    "path": "fp-examples/src/monoid_example.rs",
    "chars": 4213,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::compose::*;\n    use fp_core::empty::Empty;\n    use fp_core::identity::*;\n   "
  },
  {
    "path": "fp-examples/src/option_example.rs",
    "chars": 837,
    "preview": "#[cfg(test)]\nmod example {\n    use std::collections::HashMap;\n\n    #[test]\n    fn option_example() {\n        let mut car"
  },
  {
    "path": "fp-examples/src/partial_application_example.rs",
    "chars": 280,
    "preview": "#[test]\nfn partial_application() {\n    fn foo(a: i32, b: i32, c: i32, d: i32, mul: i32, off: i32) -> i32 {\n        (a + "
  },
  {
    "path": "fp-examples/src/pointed_functor_example.rs",
    "chars": 341,
    "preview": "#[derive(Debug, PartialEq, Eq)]\npub enum Maybe<T> {\n    #[allow(dead_code)]\n    Nothing,\n    Just(T),\n}\n\nimpl<T> Maybe<T"
  },
  {
    "path": "fp-examples/src/predicate_example.rs",
    "chars": 227,
    "preview": "#[test]\nfn predicate_example() {\n    let predicate = |a: &i32| *a > 2;\n\n    let result = (vec![1, 2, 3, 4])\n        .int"
  },
  {
    "path": "fp-examples/src/purity_example.rs",
    "chars": 492,
    "preview": "#[test]\nfn purity() {\n    let greet = |name: &str| format!(\"Hi! {}\", name);\n\n    assert_eq!(\"Hi! Jason\", greet(\"Jason\"))"
  },
  {
    "path": "fp-examples/src/referential_transparency_example.rs",
    "chars": 138,
    "preview": "#[test]\nfn referential_transparency() {\n    let greet = || \"Hello World!\";\n    let msg = greet();\n\n    assert_eq!(msg, \""
  },
  {
    "path": "fp-examples/src/semigroup_example.rs",
    "chars": 422,
    "preview": "/*\n// Note that below are just example code. We no longer use manual semigroup implementation.\n// Instead we favour Add,"
  },
  {
    "path": "fp-examples/src/setoid_example.rs",
    "chars": 202,
    "preview": "#[cfg(test)]\nmod example {\n    use fp_core::setoid::*;\n\n    #[test]\n    fn setoid_example() {\n        assert!(vec![1, 2]"
  },
  {
    "path": "fp-examples/src/side_effects_example.rs",
    "chars": 171,
    "preview": "#[cfg(test)]\nmod example {\n    use std::time::SystemTime;\n\n    #[test]\n    fn side_effects() {\n        let now = SystemT"
  },
  {
    "path": "fp-examples/src/type_signature_example.rs",
    "chars": 751,
    "preview": "#[test]\nfn type_signature_example() {\n    // add :: i32 -> i32 -> i32\n    #[allow(dead_code)]\n    fn add(x: i32) -> impl"
  },
  {
    "path": "fp-examples/src/value.rs",
    "chars": 92,
    "preview": "#[test]\nfn value_example() {\n    let a = 5;\n    let b = vec![1, 2, 3];\n    let c = \"test\";\n}"
  },
  {
    "path": "rust-toolchain.toml",
    "chars": 31,
    "preview": "[toolchain]\nchannel = \"stable\"\n"
  }
]

About this extraction

This page contains the full source code of the JasonShin/functional-programming-jargon.rs GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 64 files (90.9 KB), approximately 28.0k tokens, and a symbol index with 175 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!