Skip to content

Commit

Permalink
foldMap and foldMapWithIndex now require a Semigroup instead of…
Browse files Browse the repository at this point in the history
… a `Monoid`
  • Loading branch information
gcanti committed Jan 12, 2020
1 parent 6783dd7 commit afe153d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ high state of flux, you're at risk of it changing without notice.
# 2.4.1

- **Polish**
- add overloading to `NEA.group` managing non empty arrays, closes #831 (@gcanti)
- `NonEmptyArray`
- add overloading to `group` managing non empty arrays, closes #831 (@gcanti)
- `foldMap` and `foldMapWithIndex` now require a `Semigroup` instead of a `Monoid` (@gcanti)

# 2.4.0

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/NonEmptyArray.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Added in v2.0.0
**Signature**

```ts
;<M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: NonEmptyArray<A>) => M
;<S>(S: Semigroup<S>) => <A>(f: (a: A) => S) => (fa: NonEmptyArray<A>) => S
```

Added in v2.0.0
Expand All @@ -547,7 +547,7 @@ Added in v2.0.0
**Signature**

```ts
;<M>(M: Monoid<M>) => <A>(f: (i: number, a: A) => M) => (fa: NonEmptyArray<A>) => M
;<S>(S: Semigroup<S>) => <A>(f: (i: number, a: A) => S) => (fa: NonEmptyArray<A>) => S
```

Added in v2.0.0
Expand Down
8 changes: 6 additions & 2 deletions src/NonEmptyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ const {
duplicate,
extend,
flatten,
foldMap,
foldMapWithIndex,
map,
mapWithIndex,
reduce,
Expand All @@ -366,6 +364,12 @@ const {
reduceWithIndex
} = pipeable(nonEmptyArray)

const foldMapWithIndex = <S>(S: Semigroup<S>) => <A>(f: (i: number, a: A) => S) => (fa: NonEmptyArray<A>) =>
fa.slice(1).reduce((s, a, i) => S.concat(s, f(i + 1, a)), f(0, fa[0]))

const foldMap = <S>(S: Semigroup<S>) => <A>(f: (a: A) => S) => (fa: NonEmptyArray<A>) =>
fa.slice(1).reduce((s, a) => S.concat(s, f(a)), f(fa[0]))

export {
/**
* @since 2.0.0
Expand Down
23 changes: 19 additions & 4 deletions test/NonEmptyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { identity } from '../src/function'
import * as I from '../src/Identity'
import { fold, monoidString, monoidSum } from '../src/Monoid'
import {
concat,
cons,
copy,
filter,
filterWithIndex,
foldMap,
foldMapWithIndex,
fromArray,
getEq,
getSemigroup,
Expand All @@ -17,23 +20,23 @@ import {
groupBy,
groupSort,
head,
init,
insertAt,
last,
max,
min,
modifyAt,
nonEmptyArray,
NonEmptyArray,
reverse,
snoc,
sort,
tail,
updateAt,
NonEmptyArray,
init,
concat
updateAt
} from '../src/NonEmptyArray'
import { isSome, none, option, some } from '../src/Option'
import { ordNumber } from '../src/Ord'
import { semigroupSum } from '../src/Semigroup'
import { showString } from '../src/Show'

describe('NonEmptyArray', () => {
Expand Down Expand Up @@ -330,4 +333,16 @@ describe('NonEmptyArray', () => {
['a', 'b']
)
})

it('foldMap', () => {
const f = foldMap(semigroupSum)((s: string) => s.length)
assert.deepStrictEqual(f(['a']), 1)
assert.deepStrictEqual(f(['a', 'bb']), 3)
})

it('foldMapWithIndex', () => {
const f = foldMapWithIndex(semigroupSum)((i: number, s: string) => s.length + i)
assert.deepStrictEqual(f(['a']), 1)
assert.deepStrictEqual(f(['a', 'bb']), 4)
})
})

0 comments on commit afe153d

Please sign in to comment.