Skip to content

Commit

Permalink
make Tree functions data-last
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 31, 2019
1 parent badab02 commit 533d561
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/modules/Tree.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Added in v2.0.0
**Signature**

```ts
export function elem<A>(E: Eq<A>): (a: A, fa: Tree<A>) => boolean { ... }
export function elem<A>(E: Eq<A>): (a: A) => (fa: Tree<A>) => boolean { ... }
```

Added in v2.0.0
Expand Down
14 changes: 8 additions & 6 deletions src/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,16 @@ export function unfoldForestM<M>(
/**
* @since 2.0.0
*/
export function elem<A>(E: Eq<A>): (a: A, fa: Tree<A>) => boolean {
const go = (a: A, fa: Tree<A>): boolean => {
if (E.equals(a, fa.value)) {
return true
export function elem<A>(E: Eq<A>): (a: A) => (fa: Tree<A>) => boolean {
return a => {
const go = (fa: Tree<A>): boolean => {
if (E.equals(a, fa.value)) {
return true
}
return fa.forest.some(tree => go(tree))
}
return fa.forest.some(tree => go(a, tree))
return go
}
return go
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ describe('Tree', () => {
}
const S: Eq<User> = contramap(eqNumber, (user: User) => user.id)
const users = make({ id: 1 }, [make({ id: 1 }, [make({ id: 3 }), make({ id: 4 })]), make({ id: 2 })])
assert.strictEqual(elem(S)({ id: 1 }, users), true)
assert.strictEqual(elem(S)({ id: 4 }, users), true)
assert.strictEqual(elem(S)({ id: 5 }, users), false)
assert.strictEqual(elem(S)({ id: 1 })(users), true)
assert.strictEqual(elem(S)({ id: 4 })(users), true)
assert.strictEqual(elem(S)({ id: 5 })(users), false)
})

it('getShow', () => {
Expand Down

0 comments on commit 533d561

Please sign in to comment.