Skip to content

Commit

Permalink
List: merge NonEmpty APIs into base ones (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Nov 29, 2023
1 parent f8d2750 commit 023b512
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 129 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-cows-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

List: merge NonEmpty APIs into base ones
4 changes: 2 additions & 2 deletions docs/modules/Chunk.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Added in v2.0.0
## prependAll
Concatenates two chunks, combining their elements.
Prepends the specified prefix chunk to the beginning of the specified chunk.
If either chunk is non-empty, the result is also a non-empty chunk.
**Signature**
Expand Down Expand Up @@ -1024,7 +1024,7 @@ Added in v2.0.0

## flatMap

Returns a chunk with the elements mapped by the specified function.
Applies a function to each element in a chunk and returns a new chunk containing the concatenated mapped elements.

**Signature**

Expand Down
84 changes: 39 additions & 45 deletions docs/modules/List.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ Added in v2.0.0
- [concatenating](#concatenating)
- [append](#append)
- [appendAll](#appendall)
- [appendAllNonEmpty](#appendallnonempty)
- [prepend](#prepend)
- [prependAll](#prependall)
- [prependAllNonEmpty](#prependallnonempty)
- [prependAllReversed](#prependallreversed)
- [constructors](#constructors)
- [cons](#cons)
Expand Down Expand Up @@ -76,7 +74,6 @@ Added in v2.0.0
- [isNil](#isnil)
- [sequencing](#sequencing)
- [flatMap](#flatmap)
- [flatMapNonEmpty](#flatmapnonempty)
- [symbol](#symbol)
- [TypeId](#typeid)
- [TypeId (type alias)](#typeid-type-alias)
Expand All @@ -88,6 +85,7 @@ Added in v2.0.0
- [List (namespace)](#list-namespace)
- [Infer (type alias)](#infer-type-alias)
- [With (type alias)](#with-type-alias)
- [With2 (type alias)](#with2-type-alias)

---

Expand Down Expand Up @@ -251,30 +249,26 @@ Added in v2.0.0
## appendAll
Concatentates the specified lists together.
Concatenates two lists, combining their elements.
If either list is non-empty, the result is also a non-empty list.
**Signature**
```ts
export declare const appendAll: {
<B>(that: List<B>): <A>(self: List<A>) => List<B | A>
<S extends List<any>, T extends List<any>>(that: T): (self: S) => List.With2<S, T, List.Infer<S> | List.Infer<T>>
<A, B>(self: List<A>, that: Cons<B>): Cons<A | B>
<A, B>(self: Cons<A>, that: List<B>): Cons<A | B>
<A, B>(self: List<A>, that: List<B>): List<A | B>
}
```
Added in v2.0.0
## appendAllNonEmpty
**Signature**
**Example**
```ts
export declare const appendAllNonEmpty: {
<B>(that: Cons<B>): <A>(self: List<A>) => Cons<B | A>
<B>(that: List<B>): <A>(self: Cons<A>) => Cons<B | A>
<A, B>(self: List<A>, that: Cons<B>): Cons<A | B>
<A, B>(self: Cons<A>, that: List<B>): Cons<A | B>
}
import * as List from "effect/List"

assert.deepStrictEqual(List.make(1, 2).pipe(List.appendAll(List.make("a", "b")), List.toArray), [1, 2, "a", "b"])
```

Added in v2.0.0
Expand All @@ -297,29 +291,25 @@ Added in v2.0.0
## prependAll
Prepends the specified prefix list to the beginning of the specified list.
If either list is non-empty, the result is also a non-empty list.
**Signature**
```ts
export declare const prependAll: {
<B>(prefix: List<B>): <A>(self: List<A>) => List<B | A>
<A, B>(self: List<A>, prefix: List<B>): List<A | B>
<S extends List<any>, T extends List<any>>(that: T): (self: S) => List.With2<S, T, List.Infer<S> | List.Infer<T>>
<A, B>(self: List<A>, that: Cons<B>): Cons<A | B>
<A, B>(self: Cons<A>, that: List<B>): Cons<A | B>
<A, B>(self: List<A>, that: List<B>): List<A | B>
}
```
Added in v2.0.0
## prependAllNonEmpty
**Signature**
**Example**
```ts
export declare const prependAllNonEmpty: {
<B>(that: Cons<B>): <A>(self: List<A>) => Cons<B | A>
<B>(that: List<B>): <A>(self: Cons<A>) => Cons<B | A>
<A, B>(self: List<A>, that: Cons<B>): Cons<A | B>
<A, B>(self: Cons<A>, that: List<B>): Cons<A | B>
}
import * as List from "effect/List"

assert.deepStrictEqual(List.make(1, 2).pipe(List.prependAll(List.make("a", "b")), List.toArray), ["a", "b", 1, 2])
```

Added in v2.0.0
Expand Down Expand Up @@ -709,27 +699,17 @@ Added in v2.0.0
## flatMap
Flat maps a list using the specified function.
Applies a function to each element in a list and returns a new list containing the concatenated mapped elements.
**Signature**
```ts
export declare const flatMap: {
<A, B>(f: (a: A) => List<B>): (self: List<A>) => List<B>
<A, B>(self: List<A>, f: (a: A) => List<B>): List<B>
}
```
Added in v2.0.0
## flatMapNonEmpty
**Signature**
```ts
export declare const flatMapNonEmpty: {
<A, B>(f: (a: A) => Cons<B>): (self: Cons<A>) => Cons<B>
<A, B>(self: Cons<A>, f: (a: A) => Cons<B>): Cons<B>
<S extends List<any>, T extends List<any>>(
f: (a: List.Infer<S>, i: number) => T
): (self: S) => List.With2<S, T, List.Infer<T>>
<A, B>(self: Cons<A>, f: (a: A, i: number) => Cons<B>): Cons<B>
<A, B>(self: List<A>, f: (a: A, i: number) => List<B>): List<B>
}
```
Expand Down Expand Up @@ -820,3 +800,17 @@ export type With<T extends List<any>, A> = T extends Cons<any> ? Cons<A> : List<
```
Added in v2.0.0
### With2 (type alias)
**Signature**
```ts
export type With2<S extends List<any>, T extends List<any>, A> = S extends Cons<any>
? Cons<A>
: T extends Cons<any>
? Cons<A>
: List<A>
```
Added in v2.0.0
107 changes: 74 additions & 33 deletions dtslint/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as List from "effect/List"
import * as Predicate from "effect/Predicate"

declare const numbers: List.List<number>
declare const strings: List.List<string>
declare const nonEmptyNumbers: List.Cons<number>
declare const nonEmptyStrings: List.Cons<string>
declare const numbersOrStrings: List.List<number | string>
declare const nonEmptyNumbersOrStrings: List.Cons<number | string>

declare const predicateNumbersOrStrings: Predicate.Predicate<number | string>

Expand Down Expand Up @@ -84,22 +85,6 @@ List.append(numbersOrStrings, true)
// $ExpectType Cons<string | number | boolean>
List.append(true)(numbersOrStrings)

// -------------------------------------------------------------------------------------
// appendAllNonEmpty
// -------------------------------------------------------------------------------------

// $ExpectType Cons<string | number>
List.appendAllNonEmpty(numbersOrStrings, nonEmptyNumbersOrStrings)

// $ExpectType Cons<string | number>
List.appendAllNonEmpty(numbersOrStrings)(nonEmptyNumbersOrStrings)

// $ExpectType Cons<string | number>
List.appendAllNonEmpty(nonEmptyNumbersOrStrings, numbersOrStrings)

// $ExpectType Cons<string | number>
List.appendAllNonEmpty(nonEmptyNumbersOrStrings)(numbersOrStrings)

// -------------------------------------------------------------------------------------
// prepend
// -------------------------------------------------------------------------------------
Expand All @@ -110,22 +95,6 @@ List.prepend(numbersOrStrings, true)
// $ExpectType Cons<string | number | boolean>
List.prepend(true)(numbersOrStrings)

// -------------------------------------------------------------------------------------
// prependAllNonEmpty
// -------------------------------------------------------------------------------------

// $ExpectType Cons<string | number>
List.prependAllNonEmpty(numbersOrStrings, nonEmptyNumbersOrStrings)

// $ExpectType Cons<string | number>
List.prependAllNonEmpty(numbersOrStrings)(nonEmptyNumbersOrStrings)

// $ExpectType Cons<string | number>
List.prependAllNonEmpty(nonEmptyNumbersOrStrings, numbersOrStrings)

// $ExpectType Cons<string | number>
List.prependAllNonEmpty(nonEmptyNumbersOrStrings)(numbersOrStrings)

// -------------------------------------------------------------------------------------
// map
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -201,3 +170,75 @@ List.findFirst(numbersOrStrings, Predicate.isNumber)

// $ExpectType Option<number>
pipe(numbersOrStrings, List.findFirst(Predicate.isNumber))

// -------------------------------------------------------------------------------------
// appendAll
// -------------------------------------------------------------------------------------

// $ExpectType List<string | number>
List.appendAll(strings, numbers)

// $ExpectType List<string | number>
pipe(strings, List.appendAll(numbers))

// $ExpectType Cons<string | number>
List.appendAll(nonEmptyStrings, numbers)

// $ExpectType Cons<string | number>
pipe(nonEmptyStrings, List.appendAll(numbers))

// $ExpectType Cons<string | number>
List.appendAll(strings, nonEmptyNumbers)

// $ExpectType Cons<string | number>
pipe(strings, List.appendAll(nonEmptyNumbers))

// $ExpectType Cons<string | number>
List.appendAll(nonEmptyStrings, nonEmptyNumbers)

// $ExpectType Cons<string | number>
pipe(nonEmptyStrings, List.appendAll(nonEmptyNumbers))

// -------------------------------------------------------------------------------------
// prependAll
// -------------------------------------------------------------------------------------

// $ExpectType List<string | number>
List.prependAll(strings, numbers)

// $ExpectType List<string | number>
pipe(strings, List.prependAll(numbers))

// $ExpectType Cons<string | number>
List.prependAll(nonEmptyStrings, numbers)

// $ExpectType Cons<string | number>
pipe(nonEmptyStrings, List.prependAll(numbers))

// $ExpectType Cons<string | number>
List.prependAll(strings, nonEmptyNumbers)

// $ExpectType Cons<string | number>
pipe(strings, List.prependAll(nonEmptyNumbers))

// $ExpectType Cons<string | number>
List.prependAll(nonEmptyStrings, nonEmptyNumbers)

// $ExpectType Cons<string | number>
pipe(nonEmptyStrings, List.prependAll(nonEmptyNumbers))

// -------------------------------------------------------------------------------------
// flatMap
// -------------------------------------------------------------------------------------

// $ExpectType List<number>
List.flatMap(strings, (_) => List.empty<number>())

// $ExpectType List<number>
List.flatMap(strings, (s) => List.of(s.length))

// $ExpectType List<number>
List.flatMap(nonEmptyStrings, (_s) => List.empty<number>())

// $ExpectType Cons<number>
List.flatMap(nonEmptyStrings, (s) => List.of(s.length))
4 changes: 2 additions & 2 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export const dropWhile: {
})

/**
* Concatenates two chunks, combining their elements.
* Prepends the specified prefix chunk to the beginning of the specified chunk.
* If either chunk is non-empty, the result is also a non-empty chunk.
*
* @example
Expand Down Expand Up @@ -680,7 +680,7 @@ export const filterMapWhile: {
export const compact = <A>(self: Chunk<Option<A>>): Chunk<A> => filterMap(self, identity)

/**
* Returns a chunk with the elements mapped by the specified function.
* Applies a function to each element in a chunk and returns a new chunk containing the concatenated mapped elements.
*
* @since 2.0.0
* @category sequencing
Expand Down
Loading

0 comments on commit 023b512

Please sign in to comment.