Skip to content

Commit

Permalink
Add core data structures [ #9 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Strojewski authored and rebzden committed Oct 24, 2019
1 parent 8ea2470 commit 8861eae
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 16 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/data/applicative.ts
@@ -0,0 +1,5 @@
// https://github.com/fantasyland/fantasy-land#applicative
export interface IApplicative<T> {
ap<V>(afn: IApplicative<(val: T) => V>): IApplicative<V>
'fantasy-land/ap'<V>(afn: IApplicative<(val: T) => V>): IApplicative<V>
}
4 changes: 4 additions & 0 deletions packages/core/src/data/catamorphism.ts
@@ -0,0 +1,4 @@
// TypeClass for catamorphism
export interface ICatamorphism<F, T> {
cata<C>(l: (e?: F) => C, r: (v: T) => C): C
}
5 changes: 5 additions & 0 deletions packages/core/src/data/chain.ts
@@ -0,0 +1,5 @@
// https://github.com/fantasyland/fantasy-land#chain
export interface IChain<T> {
chain<V>(fn: (val: T) => IChain<V>): IChain<V>
'fantasy-land/chain'<V>(fn: (val: T) => IChain<V>): IChain<V>
}
5 changes: 5 additions & 0 deletions packages/core/src/data/functor.ts
@@ -0,0 +1,5 @@
// https://github.com/fantasyland/fantasy-land#functor
export interface IFunctor<T> {
map<V>(fn: (val: T) => V): IFunctor<V>
'fantasy-land/map'<V>(fn: (val: T) => V): IFunctor<V>
}
7 changes: 7 additions & 0 deletions packages/core/src/data/index.ts
@@ -0,0 +1,7 @@
export * from './applicative'
export * from './catamorphism'
export * from './chain'
export * from './functor'
export * from './monad'
export * from './setoid'
export * from './traversable'
15 changes: 15 additions & 0 deletions packages/core/src/data/monad.ts
@@ -0,0 +1,15 @@
import { IApplicative } from './applicative'
import { IChain } from './chain'
import { IFunctor } from './functor'

interface IMonad<T> extends IFunctor<T>, IChain<T>, IApplicative<T> {
readonly ['@@type']: string
bind<V>(fn: (val: T) => IMonad<V>): IMonad<V>
flatMap<V>(fn: (val: T) => IMonad<V>): IMonad<V>
chain<V>(fn: (val: T) => IMonad<V>): IMonad<V>
map<V>(fn: (val: T) => V): IMonad<V>
join<V>(): IMonad<V> // only if T = IMonad<V>
/* These are monet-Monad-specific: */
takeLeft(m: IMonad<T>): IMonad<T>
takeRight(m: IMonad<T>): IMonad<T>
}
5 changes: 5 additions & 0 deletions packages/core/src/data/setoid.ts
@@ -0,0 +1,5 @@
// https://github.com/fantasyland/fantasy-land#setoid
export interface ISetoid<A> {
equals(other: A): boolean
['fantasy-land/equals'](other: A): boolean
}
5 changes: 5 additions & 0 deletions packages/core/src/data/traversable.ts
@@ -0,0 +1,5 @@
// TypeClass for traversables
export interface ITraversable<T> {
foldLeft<V>(initial: V): (fn: (acc: V, val: T) => V) => V
foldRight<V>(initial: V): (fn: (val: T, acc: V) => V) => V
}
10 changes: 0 additions & 10 deletions packages/core/src/helpers/equals.spec.ts
Expand Up @@ -66,14 +66,4 @@ feature`equals`(() => {

})

// given`Not equal values expect result to be false`(() => {
// expect(equals(true)(false)).to.equal(false)
// expect(equals(0)(1)).to.equal(false)
// expect(equals('yes')('no')).to.equal(false)
// expect(equals(false)(true)).to.equal(false)
// expect(equals([])({})).to.equal(false)
// expect(equals({})({})).to.equal(false)
// expect(equals(3)([3])).to.equal(false)
// expect(equals(3)('3')).to.equal(false)
// })
})
8 changes: 2 additions & 6 deletions packages/core/src/helpers/setoid.ts
@@ -1,10 +1,6 @@
import { isFunction } from './function'
import { ISetoid } from '../data'

// https://github.com/fantasyland/fantasy-land#setoid
export interface ISetoid<A> {
equals(other: A): boolean
['fantasy-land/equals'](other: A): boolean
}
import { isFunction } from './function'

export const isSetoid = <T>(a: any): a is ISetoid<T> => isFunction(a.equals)

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
@@ -1 +1,2 @@
export * from './data'
export * from './helpers'

0 comments on commit 8861eae

Please sign in to comment.