Skip to content

Commit

Permalink
feat: Add Map and Record modules (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina committed Feb 12, 2024
1 parent e23ab6c commit d09f97a
Show file tree
Hide file tree
Showing 8 changed files with 2,820 additions and 9 deletions.
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * as Identity from "./src/identity.ts";
export * as Kleisli from "./src/kleisli.ts";
export * as Lazy from "./src/lazy.ts";
export * as List from "./src/list.ts";
export * as Map from "./src/map.ts";
export * as Number from "./src/number.ts";
export * as Optical from "./src/optical.ts";
export * as Option from "./src/option.ts";
Expand All @@ -27,6 +28,7 @@ export * as Promise from "./src/promise.ts";
export * as MonadPromise from "./src/promise/monad.ts";
export * as Reader from "./src/reader.ts";
export * as MonadReader from "./src/reader/monad.ts";
export * as Record from "./src/record.ts";
export * as Result from "./src/result.ts";
export * as Reverse from "./src/reverse.ts";
export * as Seq from "./src/seq.ts";
Expand Down
16 changes: 15 additions & 1 deletion src/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEquals } from "../deps.ts";
import { doT } from "./cat.ts";
import { monad, range, toArray } from "./list.ts";
import { none, some } from "./option.ts";
import { fromIterable, monad, range, toArray } from "./list.ts";

Deno.test("with CatT", () => {
// Find patterns where `x + y + z == 5` for all natural number `x`, `y`, and `z`.
Expand Down Expand Up @@ -34,3 +35,16 @@ Deno.test("with CatT", () => {
[5, 0, 0],
]);
});

Deno.test("fromIterable", () => {
let list = fromIterable([3, 1, 4]);
assertEquals(list.current(), some(3));
list = list.rest();
assertEquals(list.current(), some(1));
list = list.rest();
assertEquals(list.current(), some(4));
list = list.rest();
assertEquals(list.current(), none());
list = list.rest();
assertEquals(list.current(), none());
});
21 changes: 17 additions & 4 deletions src/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cat } from "./cat.ts";
import type { Get1, Hkt1 } from "./hkt.ts";
import * as Option from "./option.ts";
import { isNone } from "./option.ts";
import { andThen, type Ordering } from "./ordering.ts";
import type { Tuple } from "./tuple.ts";
import { type Applicative, liftA2 } from "./type-class/applicative.ts";
Expand Down Expand Up @@ -28,15 +29,18 @@ export interface List<T> {

export const partialEquality = <T>(equalityT: PartialEq<T>) => {
const self = (l: List<T>, r: List<T>): boolean =>
Option.partialEq(equalityT).eq(l.current(), r.current()) &&
self(l.rest(), r.rest());
(isNone(l.current()) && isNone(r.current())) ||
(Option.partialEq(equalityT).eq(l.current(), r.current()) &&
self(l.rest(), r.rest()));

return self;
};
export const partialEq = fromPartialEquality(partialEquality);
export const equality = <T>(equalityT: Eq<T>) => {
const self = (l: List<T>, r: List<T>): boolean =>
Option.eq(equalityT).eq(l.current(), r.current()) &&
self(l.rest(), r.rest());
(isNone(l.current()) && isNone(r.current())) ||
(Option.eq(equalityT).eq(l.current(), r.current()) &&
self(l.rest(), r.rest()));
return self;
};
export const eq = fromEquality(equality);
Expand Down Expand Up @@ -204,6 +208,15 @@ export const singletonWith = <T>(value: () => T): List<T> => ({
*/
export const singleton = <T>(value: T): List<T> => singletonWith(() => value);

/**
* Creates a new list with a finite iterable object.
*
* @param iterable - The finite iterable object such as `Array` or `Map`.
* @returns The list of items from the iterable.
*/
export const fromIterable = <T>(iterable: Iterable<T>): List<T> =>
fromArray([...iterable]);

/**
* Concatenates two lists.
*
Expand Down

0 comments on commit d09f97a

Please sign in to comment.