Welcome to SCATS, a comprehensive TypeScript library that brings the powerful functional programming paradigms of Scala to JavaScript and TypeScript. This library features immutable collections, monads, pattern matching, and much more. With SCATS, you can leverage advanced programming techniques to create clean, maintainable, and efficient code.
SCATS offers a range of features designed to enhance your programming experience:
- Immutable Collections: Work with collections that cannot be modified after creation, ensuring data integrity.
- Monads: Utilize monadic structures to handle side effects and asynchronous programming.
- Pattern Matching: Simplify conditional logic with powerful pattern matching capabilities.
- Option and Either Types: Manage optional values and error handling in a clean and expressive way.
- Lazy Evaluation: Improve performance by delaying computation until necessary.
- Type Classes: Implement polymorphism and code reuse through type classes.
To install SCATS, use npm or yarn. Run one of the following commands in your terminal:
npm install scats
or
yarn add scats
Once installed, you can import SCATS in your TypeScript files:
import { ... } from 'scats';
SCATS is designed to be straightforward and intuitive. You can start using it right away with minimal setup. Here’s a simple example to get you started:
import { List } from 'scats';
const myList = List.of(1, 2, 3);
const doubled = myList.map(x => x * 2);
console.log(doubled.toArray()); // Output: [2, 4, 6]
For more detailed examples and use cases, check out the Examples section.
Immutable collections are a core feature of SCATS. They allow you to create data structures that do not change after they are created. This helps prevent bugs related to unintended side effects.
import { List } from 'scats';
const originalList = List.of(1, 2, 3);
const newList = originalList.append(4);
console.log(originalList.toArray()); // Output: [1, 2, 3]
console.log(newList.toArray()); // Output: [1, 2, 3, 4]
Monads are a design pattern that helps manage side effects and asynchronous programming. SCATS provides several monadic types, including Option
, Either
, and Try
.
import { Option } from 'scats';
const maybeValue = Option.of(null);
const result = maybeValue.map(value => value * 2).getOrElse(0);
console.log(result); // Output: 0
Pattern matching allows you to destructure data types and perform actions based on their shape. This can simplify complex conditional logic.
import { match } from 'scats';
const value = { type: 'success', data: 'Hello, World!' };
match(value)
.with({ type: 'success', data: (data) => console.log(data) })
.otherwise(() => console.log('Error'));
Option
and Either
types help manage optional values and errors in a functional way. They provide a clean alternative to null checks and exceptions.
import { Either } from 'scats';
const divide = (a: number, b: number): Either<string, number> => {
if (b === 0) {
return Either.left('Division by zero');
}
return Either.right(a / b);
};
const result = divide(10, 0);
result
.map(value => console.log(value))
.mapLeft(error => console.error(error));
Lazy evaluation allows you to defer computation until the result is needed. This can improve performance, especially with large data sets.
import { LazyList } from 'scats';
const lazyList = LazyList.from([1, 2, 3, 4, 5]);
const filtered = lazyList.filter(x => x % 2 === 0);
console.log(filtered.toArray()); // Output: [2, 4]
Type classes enable polymorphism and code reuse. They allow you to define generic behavior that can be implemented for different types.
import { typeClass } from 'scats';
const show = typeClass({
String: {
show: (value: string) => `"${value}"`,
},
Number: {
show: (value: number) => value.toString(),
},
});
console.log(show.String.show('Hello')); // Output: "Hello"
console.log(show.Number.show(42)); // Output: 42
To explore more examples, check the Examples section in the repository. You can find various use cases demonstrating how to utilize SCATS effectively.
We welcome contributions to SCATS! If you would like to help improve the library, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them.
- Push your branch to your fork.
- Create a pull request.
Please ensure that your code adheres to the existing style and includes appropriate tests.
SCATS is licensed under the MIT License. See the LICENSE file for more details.
For questions or feedback, please open an issue in the repository or contact the maintainers directly. You can also check the Releases section for updates and new features.
Thank you for using SCATS! We hope you enjoy exploring functional programming in TypeScript.