Skip to content

Commit

Permalink
optimize intercalate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 22, 2022
1 parent 6efe63c commit 2e44bbf
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 166 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,8 @@ high state of flux, you're at risk of it changing without notice.

- **New Feature**
- add `IOOption` module (@gcanti)
- `Array`
- add `intercalate`, #1678 (@thewilkybarkid)
- `Bounded`
- add `clamp` (@gcanti)
- add `reverse` (@gcanti)
Expand All @@ -39,6 +41,8 @@ high state of flux, you're at risk of it changing without notice.
- add `chainFirstEitherKW` (@gcanti)
- add `orElseFirstIOK`, #1655 (@thewilkybarkid)
- add `bracketW`, #1627 (@thomasvargiu)
- `NonEmptyArray`
- add `intercalate`, #1678 (@thewilkybarkid)
- `Option`
- add `chainFirstEitherK` (@gcanti)
- `Ordering`
Expand All @@ -60,6 +64,10 @@ high state of flux, you're at risk of it changing without notice.
- add `chainNullableK` (@gcanti)
- add `fromNullable` (@gcanti)
- add `fromNullableK` (@gcanti)
- `ReadonlyArray`
- add `intercalate`, #1678 (@thewilkybarkid)
- `ReadonlyNonEmptyArray`
- add `intercalate`, #1678 (@thewilkybarkid)
- `ReadonlyRecord`
- add `toEntries`, #1552 (@bravely)
- add `fromEntries`, #1552 (@bravely)
Expand Down
45 changes: 22 additions & 23 deletions docs/modules/Array.ts.md
Expand Up @@ -87,7 +87,6 @@ Added in v2.0.0
- [flatten](#flatten)
- [fromEitherK](#fromeitherk)
- [fromOptionK](#fromoptionk)
- [intercalate](#intercalate)
- [intersection](#intersection)
- [intersperse](#intersperse)
- [lefts](#lefts)
Expand Down Expand Up @@ -198,6 +197,7 @@ Added in v2.0.0
- [findIndex](#findindex)
- [findLastIndex](#findlastindex)
- [insertAt](#insertat)
- [intercalate](#intercalate)
- [isOutOfBound](#isoutofbound)
- [lookup](#lookup)
- [modifyAt](#modifyat)
Expand Down Expand Up @@ -1437,28 +1437,6 @@ export declare const fromOptionK: <A extends readonly unknown[], B>(f: (...a: A)

Added in v2.11.0

## intercalate

Creates a new `Array` placing an element in between members of the input `Array`, then folds the results using the
provided `Monoid`.

**Signature**

```ts
export declare const intercalate: <A>(M: Monoid<A>) => (sep: A) => (as: A[]) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/Array'
assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.11.9

## intersection

Creates an array of unique values that are included in all given arrays using a `Eq` for equality
Expand Down Expand Up @@ -3454,6 +3432,27 @@ assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4]))

Added in v2.0.0

## intercalate

Places an element in between members of an `Array`, then folds the results using the provided `Monoid`.

**Signature**

```ts
export declare const intercalate: <A>(M: Monoid<A>) => (middle: A) => (as: A[]) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/Array'
assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.12.0

## isOutOfBound

Test whether an array contains a particular index
Expand Down
10 changes: 5 additions & 5 deletions docs/modules/Foldable.ts.md
Expand Up @@ -223,17 +223,17 @@ Fold a data structure, accumulating values in some `Monoid`, combining adjacent
export declare function intercalate<M, F extends URIS3>(
M: Monoid<M>,
F: Foldable3<F>
): <R, E>(sep: M, fm: Kind3<F, R, E, M>) => M
): <R, E>(middle: M, fm: Kind3<F, R, E, M>) => M
export declare function intercalate<M, F extends URIS2>(
M: Monoid<M>,
F: Foldable2<F>
): <E>(sep: M, fm: Kind2<F, E, M>) => M
): <E>(middle: M, fm: Kind2<F, E, M>) => M
export declare function intercalate<M, F extends URIS2, E>(
M: Monoid<M>,
F: Foldable2C<F, E>
): (sep: M, fm: Kind2<F, E, M>) => M
export declare function intercalate<M, F extends URIS>(M: Monoid<M>, F: Foldable1<F>): (sep: M, fm: Kind<F, M>) => M
export declare function intercalate<M, F>(M: Monoid<M>, F: Foldable<F>): (sep: M, fm: HKT<F, M>) => M
): (middle: M, fm: Kind2<F, E, M>) => M
export declare function intercalate<M, F extends URIS>(M: Monoid<M>, F: Foldable1<F>): (middle: M, fm: Kind<F, M>) => M
export declare function intercalate<M, F>(M: Monoid<M>, F: Foldable<F>): (middle: M, fm: HKT<F, M>) => M
```

**Example**
Expand Down
44 changes: 22 additions & 22 deletions docs/modules/NonEmptyArray.ts.md
Expand Up @@ -63,7 +63,6 @@ Added in v2.0.0
- [group](#group)
- [groupBy](#groupby)
- [insertAt](#insertat)
- [intercalate](#intercalate)
- [intersperse](#intersperse)
- [modifyAt](#modifyat)
- [prependAll](#prependall)
Expand Down Expand Up @@ -130,6 +129,7 @@ Added in v2.0.0
- [extract](#extract)
- [head](#head)
- [init](#init)
- [intercalate](#intercalate)
- [last](#last)
- [max](#max)
- [min](#min)
Expand Down Expand Up @@ -532,27 +532,6 @@ export declare const insertAt: <A>(i: number, a: A) => (as: A[]) => Option<NonEm

Added in v2.0.0

## intercalate

**Note**. The constraint is relaxed: a `Semigroup` instead of a `Monoid`.

**Signature**

```ts
export declare const intercalate: <A>(S: Se.Semigroup<A>) => (sep: A) => (as: NonEmptyArray<A>) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/NonEmptyArray'
assert.deepStrictEqual(intercalate(S.Semigroup)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.11.9

## intersperse

Places an element in between members of an array
Expand Down Expand Up @@ -1403,6 +1382,27 @@ assert.deepStrictEqual(init([1]), [])

Added in v2.2.0

## intercalate

Places an element in between members of a `NonEmptyArray`, then folds the results using the provided `Semigroup`.

**Signature**

```ts
export declare const intercalate: <A>(S: Se.Semigroup<A>) => (middle: A) => (as: NonEmptyArray<A>) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/NonEmptyArray'
assert.deepStrictEqual(intercalate(S.Semigroup)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.12.0

## last

**Signature**
Expand Down
46 changes: 23 additions & 23 deletions docs/modules/ReadonlyArray.ts.md
Expand Up @@ -82,7 +82,6 @@ Added in v2.5.0
- [flatten](#flatten)
- [fromEitherK](#fromeitherk)
- [fromOptionK](#fromoptionk)
- [intercalate](#intercalate)
- [intersection](#intersection)
- [intersperse](#intersperse)
- [lefts](#lefts)
Expand Down Expand Up @@ -195,6 +194,7 @@ Added in v2.5.0
- [head](#head)
- [init](#init)
- [insertAt](#insertat)
- [intercalate](#intercalate)
- [isOutOfBound](#isoutofbound)
- [last](#last)
- [lookup](#lookup)
Expand Down Expand Up @@ -971,27 +971,6 @@ export declare const fromOptionK: <A extends readonly unknown[], B>(

Added in v2.11.0

## intercalate

Places an element in between members of an array`, then folds the results using the provided `Monoid`.
**Signature**
```ts
export declare const intercalate: <A>(M: Monoid<A>) => (sep: A) => (as: readonly A[]) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/Array'

assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.11.9

## intersection

Creates an array of unique values that are included in all given arrays using a `Eq` for equality
Expand Down Expand Up @@ -1923,7 +1902,7 @@ Added in v2.7.0
**Signature**

```ts
export declare const Foldable: F.Foldable1<'ReadonlyArray'>
export declare const Foldable: Foldable1<'ReadonlyArray'>
```

Added in v2.7.0
Expand Down Expand Up @@ -2762,6 +2741,27 @@ assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4]))

Added in v2.5.0

## intercalate

Places an element in between members of a `ReadonlyArray`, then folds the results using the provided `Monoid`.

**Signature**

```ts
export declare const intercalate: <A>(M: Monoid<A>) => (middle: A) => (as: readonly A[]) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/Array'
assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.12.0

## isOutOfBound

Test whether an array contains a particular index
Expand Down
44 changes: 22 additions & 22 deletions docs/modules/ReadonlyNonEmptyArray.ts.md
Expand Up @@ -68,7 +68,6 @@ Added in v2.5.0
- [getUnionSemigroup](#getunionsemigroup)
- [group](#group)
- [groupBy](#groupby)
- [intercalate](#intercalate)
- [intersperse](#intersperse)
- [modifyAt](#modifyat)
- [prependAll](#prependall)
Expand Down Expand Up @@ -136,6 +135,7 @@ Added in v2.5.0
- [concatAll](#concatall)
- [head](#head)
- [init](#init)
- [intercalate](#intercalate)
- [last](#last)
- [max](#max)
- [min](#min)
Expand Down Expand Up @@ -608,27 +608,6 @@ assert.deepStrictEqual(groupBy((s: string) => String(s.length))(['a', 'b', 'ab']

Added in v2.5.0

## intercalate

**Note**. The constraint is relaxed: a `Semigroup` instead of a `Monoid`.

**Signature**

```ts
export declare const intercalate: <A>(S: Se.Semigroup<A>) => (sep: A) => (as: ReadonlyNonEmptyArray<A>) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/ReadonlyNonEmptyArray'
assert.deepStrictEqual(intercalate(S.Semigroup)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.11.9

## intersperse

Places an element in between members of a `ReadonlyNonEmptyArray`.
Expand Down Expand Up @@ -1509,6 +1488,27 @@ assert.deepStrictEqual(init([1]), [])

Added in v2.5.0

## intercalate

Places an element in between members of a `ReadonlyNonEmptyArray`, then folds the results using the provided `Semigroup`.

**Signature**

```ts
export declare const intercalate: <A>(S: Se.Semigroup<A>) => (middle: A) => (as: ReadonlyNonEmptyArray<A>) => A
```

**Example**

```ts
import * as S from 'fp-ts/string'
import { intercalate } from 'fp-ts/ReadonlyNonEmptyArray'
assert.deepStrictEqual(intercalate(S.Semigroup)('-')(['a', 'b', 'c']), 'a-b-c')
```

Added in v2.12.0

## last

**Signature**
Expand Down
28 changes: 13 additions & 15 deletions src/Array.ts
Expand Up @@ -1124,21 +1124,6 @@ export const intersperse = <A>(middle: A): ((as: Array<A>) => Array<A>) => {
return (as) => (isNonEmpty(as) ? f(as) : copy(as))
}

/**
* Creates a new `Array` placing an element in between members of the input `Array`, then folds the results using the
* provided `Monoid`.
*
* @example
* import * as S from 'fp-ts/string'
* import { intercalate } from 'fp-ts/Array'
*
* assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
*
* @category combinators
* @since 2.11.9
*/
export const intercalate: <A>(M: Monoid<A>) => (sep: A) => (as: Array<A>) => A = RA.intercalate

/**
* Creates a new `Array` rotating the input `Array` by `n` steps.
*
Expand Down Expand Up @@ -2903,6 +2888,19 @@ export const some = <A>(predicate: Predicate<A>) => (as: Array<A>): as is NonEmp
*/
export const exists = some

/**
* Places an element in between members of an `Array`, then folds the results using the provided `Monoid`.
*
* @example
* import * as S from 'fp-ts/string'
* import { intercalate } from 'fp-ts/Array'
*
* assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
*
* @since 2.12.0
*/
export const intercalate: <A>(M: Monoid<A>) => (middle: A) => (as: Array<A>) => A = RA.intercalate

// -------------------------------------------------------------------------------------
// do notation
// -------------------------------------------------------------------------------------
Expand Down

0 comments on commit 2e44bbf

Please sign in to comment.