Skip to content

Commit

Permalink
#88: Splitting types of Response
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-zozol committed Oct 15, 2017
1 parent bea13b8 commit 83ef833
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 70 deletions.
7 changes: 4 additions & 3 deletions integration-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import * as masala from './masala';
import Bundle from '../src/lib/index'

// Can I get rid of this line ?
let {Stream, F, C}:masala.Bundle = Bundle;

let stream:masala.Stream<string> = Stream.ofString('abc');
let parser:masala.Parser<string> = C.char('a');
let stream = Stream.ofString('abc');
let parser= C.char('a');
const parsing = parser.parse(stream);
const x = ['a', 'b', 'c'] === parsing.value; //compiling, types are almost OK
const x = 'a' === parsing.value; //compiling, types are almost OK



Expand Down
174 changes: 111 additions & 63 deletions integration-ts/masala.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,145 @@
*/

// Not needed
export interface Option<Type>{
isPresent():boolean;
map( bindCall: ()=>any ):any;
flatmap (bindCall: ()=>any): Type;
filter (f :(value:Type)=>boolean):Type|Option<Type>
export interface Option<Type> {
isPresent(): boolean;
map(bindCall: () => any): any;
flatmap (bindCall: () => any): Type;
filter (f: (value: Type) => boolean): Type | Option<Type>
get(): Type
orElse<X>(v:X): Type|X;
orLazyElse<X>(v:X): Type|X;
orElse<X>(v: X): Type | X;
orLazyElse<X>(v: X): Type | X;

}

export interface Try<V,E>{
export interface Try<V, E> {
isSuccess(): boolean;
isFailure(): boolean;
onSuccess(f : ()=>any):Try<V,E>;
onFailure(f : ()=>any):Try<V,E>;
map( bindCall: ()=>any ):Try<V,E>;
flatmap (bindCall: ()=>any): Try<V,E>;
success():V;
failure():E;
recoverWith<X>(X):V|X;
lazyRecoverWith<X>(X):V|X;
filter(f :(value:V)=>boolean):Try<V,E>
onSuccess(f: () => any): Try<V, E>;
onFailure(f: () => any): Try<V, E>;
map(bindCall: () => any): Try<V, E>;
flatmap (bindCall: () => any): Try<V, E>;
success(): V;
failure(): E;
recoverWith<X>(X): V | X;
lazyRecoverWith<X>(X): V | X;
filter(f: (value: V) => boolean): Try<V, E>

}
// Not needed
export interface List<E>{
size:number;
export interface List<T> {
size: number;
isEmpty: boolean;
array: Array<E>
array: Array<T>
}


export interface Stream<Data> {
location(index: number): number;
get(index: number): Try<Data, void>;
subStreamAt(index: number, stream: Stream<Data>)

export interface Stream<Data>{
location(index:number):number;
get(index:number):Try<Data,void>;
subStreamAt(index:number, stream:Stream<Data>)
}

export interface StreamFactory {
ofString(string: string): Stream<string>;
ofArray<X>(): Stream<Array<X>>;
}


export interface ArrayResponse<T> extends Response<T> {
value: Array<T>
}

export interface ListResponse<T> extends Response<T> {
value: List<T>
}

export interface StreamFactory{
ofString(string:string):Stream<string>;
ofArray<X>():Stream<Array<X>>;
export interface OptionResponse<T> extends Response<T> {
value: Option<T>
}

export interface Monoid<T>{
value:T|Array<T>

export interface SingleResponse<T> extends Response<T> {
value: T
}

export interface Response<T> extends Monoid<T>{
isAccepted():boolean
fold(accept, reject?):Response<T>;
map(f):Response<T>;
flatmap(f):Response<T>;
filter(f:(value)=>boolean):Response<T>;
export interface VoidResponse<T>extends Response<T> {
value: void
}

export interface Parser<T>{
then<Y>(p:Parser<Y>):Parser<T|Y>;
parse<X>(stream:Stream<X>, index?:number):Response<T>;
flatmap<Y> (f: ()=>Y): Parser<Y>;
map<Y> (f: ()=>Y): Parser<Y>;
filter(f :()=>boolean):Parser<T>
match(value:T):Parser<T>;
drop():Parser<void>;
thenReturns<Y>(value:Y): Parser<Y>;
or<Y>(p:Parser<Y>):Parser<T|Y>;
opt():Parser<T>;
rep():Parser<List<T>>;
occurrence(n:number):Parser<List<T>>;
optrep():Parser<List<T>>;
chain<Y>():Parser<Y>;
debug(hint?:string, details?:boolean):Parser<T>;
export interface Response<T> {
isAccepted(): boolean
fold(accept, reject?): Response<T>;
map<Y>(f): Response<Y>;
flatmap<Y>(f): Response<Y>;
filter(f: (value) => boolean): Response<T>;
//value:any
}

export interface Response<T> extends Monoid<T>{
isAccepted():boolean
fold(accept, reject?):Response<T>;
map(f):Response<T>;
flatmap(f):Response<T>;
filter(f:(value)=>boolean):Response<T>;
/* Array with different values*/
export interface ArrayParser<T> extends Parser<T> {
parse<X>(stream: Stream<X>, index?: number): ArrayResponse<T>;
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
map<Y> (f: (T) => Y): ArrayParser<T>;
flatmap<Z> (f: () => Z): ArrayParser<T>;
filter(f: (T) => boolean): ArrayParser<T>
}

export interface CharBundle{}
export interface FlowBundle{}
/* List with same values ; need bo call array() */
export interface ListParser<T> {
parse<X>(stream: Stream<X>, index?: number): Response<List<T>>;
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
}

interface SingleParser<T> extends Parser<T> {
parse<X>(stream: Stream<X>, index?: number): SingleResponse<T>;
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
map<Y> (f: () => Y): SingleParser<Y>;
filter(f: () => boolean): SingleParser<T>
}

interface VoidParser extends Parser<void> {
then<Y>(p: Parser<Y>): SingleParser<Y>;
}

interface Parser<T> {
// Needed because opt() won(t know if we have void, array or single
then<Y>(p:Parser<Y>):Parser<any>;
flatmap<Y> (f: () => Y): Parser<Y>;
map<Y> (f: (T) => Y): Parser<Y>;
filter(f: (T) => boolean): Parser<T>
match(value: T): Parser<T>;
drop(): VoidParser;
thenReturns<Y>(value: Y): SingleParser<Y>;
or<Y>(p: Parser<Y>): Parser<T | Y>;
opt(): Parser<T>;
rep(): ListParser<List<T>>;
occurrence(n: number): ListParser<T>;
optrep(): Parser<List<T>>;
chain<Y>(): Parser<Y>;
debug(hint?: string, details?: boolean): Parser<T>;
parse<X>(stream: Stream<X>, index?: number): Response<T>;
}

export interface Response<T> {
isAccepted(): boolean
fold(accept, reject?): Response<T>;
map(f): Response<T>;
flatmap(f): Response<T>;
filter(f: (value) => boolean): Response<T>;
}

export interface CharBundle {
char(string):SingleParser<string>;
}
export interface FlowBundle {
}

export interface Bundle{
Stream:StreamFactory;
C:CharBundle;
F:FlowBundle
export interface Bundle {
Stream: StreamFactory;
C: CharBundle;
F: FlowBundle
}

//export default Bundle;
25 changes: 21 additions & 4 deletions integration-ts/working.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import * as masala from './masala';
import {Stream, F, C} from '../src/lib/index'

import{ assertTrue} from '../integration-npm/assert';

let stream: masala.Stream<string> = Stream.ofString('abc');
let parser: masala.SingleParser<string> = C.char('a');
let arrayParser = parser.then(C.char('b'))//.map(x=>x+'yop');
let parsing = arrayParser.parse(stream);

let stream:masala.Stream<string> = Stream.ofString('abc');
let parser:masala.Parser<string> = C.char('a');
const parsing = parser.parse(stream);
const x = ['a', 'b', 'c'] === parsing.value; //compiling, types are almost OK
assertTrue(['a', 2] === parsing.value[0]) ; //compiling, types are almost OK


parser = C.char('a');

let charParsing = parser.parse(Stream.ofString('a'));
let charParsingValue = charParsing.value;
assertTrue(charParsingValue === 'a');


let singleParsing = parser.parse(stream);
assertTrue( 'a' === singleParsing.value); //compiling, types are almost OK


parser = C.char('a');
singleParsing = parser.parse(stream);
assertTrue( ['a', 'b', 'c'] === parsing.value); //compiling, types are almost OK

0 comments on commit 83ef833

Please sign in to comment.