Skip to content

Commit

Permalink
add getOrElse method to Option
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 22, 2017
1 parent d0ae4e8 commit f9c97e8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- **New Feature**
- make Array<T> a HKT and deprecate `to`,`from` helper functions, fix #5 (@gcanti)
- add `Traced` comonad (@bumbleblym)
- add `getOrElse` method to `Option` (@gcanti)
- **Polish**
- add tslint

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"jsnext:main": "lib-jsnext/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"lint": "tslint src/**/*.ts",
"lint": "tslint src/**/*.ts test/**/*.ts",
"test": "npm run lint && mocha -r ts-node/register test/*.ts",
"build": "rm -rf lib/* && tsc && tsc -m es6 --outDir lib-jsnext"
},
Expand Down
7 changes: 7 additions & 0 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Option<A> extends HKTOption<A> {
alt(fa: Option<A>): Option<A>
extend<B>(fy: Function1<Option<A>, B>): Option<B>
fold<B>(n: Lazy<B>, s: Function1<A, B>): B
getOrElse(f: Lazy<A>): A
concat(semigroup: Semigroup<A>, fy: Option<A>): Option<A>
equals(setoid: Setoid<A>, fy: Option<A>): boolean
}
Expand Down Expand Up @@ -63,6 +64,9 @@ export class None<A> implements Option<A> {
fold<B>(n: Lazy<B>, s: Function1<A, B>): B {
return n()
}
getOrElse(f: Lazy<A>): A {
return f()
}
concat(semigroup: Semigroup<A>, fy: Option<A>): Option<A> {
return fy
}
Expand Down Expand Up @@ -113,6 +117,9 @@ export class Some<A> implements Option<A> {
fold<B>(n: Lazy<B>, s: Function1<A, B>): B {
return s(this.value)
}
getOrElse(f: Lazy<A>): A {
return this.value
}
concat(semigroup: Semigroup<A>, fy: Option<A>): Option<A> {
return fy.fold(() => this, y => new Some(semigroup.concat(this.value, y)))
}
Expand Down
5 changes: 2 additions & 3 deletions test/Arr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ describe('Arr', () => {
})

it('takeWhile', () => {
assert.deepEqual(arr.takeWhile((n) => n % 2 == 0, as), [2])
assert.deepEqual(arr.takeWhile((n) => n % 2 === 0, as), [2])
})

it('drop', () => {
assert.deepEqual(arr.drop(2, as), [3])
})

it('dropWhile', () => {
assert.deepEqual(arr.dropWhile((n) => n % 2 == 0, as), [1, 3])
assert.deepEqual(arr.dropWhile((n) => n % 2 === 0, as), [1, 3])
})

it('init', () => {
Expand Down Expand Up @@ -129,5 +129,4 @@ describe('Arr', () => {
assert.deepEqual(arr.sort(numberOrd, [3, 2, 1]), [1, 2, 3])
})


})
7 changes: 6 additions & 1 deletion test/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('Option', () => {
assert.strictEqual(fold(f, g, some('abc')), 'some3')
})

it('getOrElse', () => {
assert.strictEqual(some(1).getOrElse(() => 0), 1)
assert.strictEqual(none.getOrElse(() => 0), 0)
})

it('equals', () => {
assert.strictEqual(equals(setoidNumber, none, none), true)
assert.strictEqual(equals(setoidNumber, none, some(1)), false)
Expand Down Expand Up @@ -51,7 +56,7 @@ describe('Option', () => {
})

it('getMonoid', () => {
const { concat } = getMonoid({ concat(x: number, y: number){ return x + y } })
const { concat } = getMonoid({ concat(x: number, y: number) { return x + y } })
eq(concat(none, some(1)), some(1))
eq(concat(some(2), none), some(2))
eq(concat(some(2), some(1)), some(3))
Expand Down
6 changes: 3 additions & 3 deletions test/Traced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { identity } from '../src/function'
import { monoidSum } from '../src/Monoid'
import {
getComonad,
Traced,
Traced
} from '../src/Traced'

describe('Traced', () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Traced', () => {

assert.strictEqual(
extract(extend(ea => extract(ea), t)),
extract(t),
extract(t)
)
})

Expand All @@ -81,7 +81,7 @@ describe('Traced', () => {

assert.strictEqual(
extract(extend(f , t)),
f(t),
f(t)
)
})
})

0 comments on commit f9c97e8

Please sign in to comment.