Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
expose concat APIs, fix #33 (#34)
Browse files Browse the repository at this point in the history
* Add Pointed and Copointed functor, fix #15

* expose concat APIs, fix #33
  • Loading branch information
gcanti committed Oct 6, 2016
1 parent ebef2d3 commit 80aefe0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
6 changes: 2 additions & 4 deletions src/Applicative.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @flow
import { HKT } from './HKT'
import type { Apply } from './Apply'
import type { Pointed } from './Pointed'

export interface Applicative<F> extends Apply<F> {
of<A>(a: A): HKT<F, A>
}
export interface Applicative<F> extends Apply<F>, Pointed<F> {}
7 changes: 2 additions & 5 deletions src/Comonad.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// @flow
import { HKT } from './HKT'
import type { Functor } from './Functor'
import type { Extend } from './Extend'
import type { Copointed } from './Copointed'

export interface Comonad<F> extends Functor<F>, Extend<F> {
extract<A>(ca: HKT<F, A>): A
}
export interface Comonad<F> extends Extend<F>, Copointed<F> {}
7 changes: 7 additions & 0 deletions src/Copointed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow
import { HKT } from './HKT'
import type { Functor } from './Functor'

export interface Copointed<F> extends Functor<F> {
extract<A>(ca: HKT<F, A>): A
}
20 changes: 12 additions & 8 deletions src/Either.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,20 @@ export function traverse<F, L, A, B>(applicative: Applicative<F>, f: (a: A) => H
return applicative.map(of, f(a.value0))
}

export function concat<L, R>(semigroup: Semigroup<R>): (fx: Either<L, R>, fy: Either<L, R>) => Either<L, R> {
return function concat(fx, fy) {
const x = prj(fx)
const y = prj(fy)
if (x instanceof Right && y instanceof Right) {
return right(semigroup.concat(x.value0, y.value0))
}
return fx
}
}

export function getSemigroup<L, R>(semigroup: Semigroup<R>): Semigroup<Either<L, R>> {
return {
concat(a, b) {
const av = prj(a)
const bv = prj(b)
if (av instanceof Right && bv instanceof Right) {
return right(semigroup.concat(av.value0, bv.value0))
}
return a
}
concat: concat(semigroup)
}
}

Expand Down
28 changes: 16 additions & 12 deletions src/Maybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,30 @@ export function empty<A>(): Maybe<A> {

export const pempty = empty

export function concat<A>(semigroup: Semigroup<A>): (fx: Maybe<A>, fy: Maybe<A>) => Maybe<A> {
return function concat(fx, fy) {
const x = prj(fx)
const y = prj(fy)
if (x == null) {
return fy
}
if (y == null) {
return fx
}
return of(semigroup.concat(x, y))
}
}

export function getSemigroup<A>(semigroup: Semigroup<A>): Semigroup<Maybe<A>> {
return {
concat(fx, fy) {
const x = prj(fx)
const y = prj(fy)
if (x == null) {
return fy
}
if (y == null) {
return fx
}
return of(semigroup.concat(x, y))
}
concat: concat(semigroup)
}
}

export function getMonoid<A>(semigroup: Semigroup<A>): Monoid<Maybe<A>> {
return {
empty,
concat: getSemigroup(semigroup).concat
concat: concat(semigroup)
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Pointed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow
import { HKT } from './HKT'
import type { Functor } from './Functor'

export interface Pointed<F> extends Functor<F> {
of<A>(a: A): HKT<F, A>
}
1 change: 1 addition & 0 deletions src/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function tell<W>(w: W): Writer<W, void> {
return inj(() => tuple.inj([undefined, w]))
}

// TODO: change to getMonad
export function monadWriter<W>(monoid: Monoid<W>): Monad<HKT<IsWriter, W>> {

function map<A, B>(f: (a: A) => B, fa: Writer<W, A>): Writer<W, B> {
Expand Down

0 comments on commit 80aefe0

Please sign in to comment.