Skip to content

Commit

Permalink
#88: TS: try() and opt()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-zozol committed Nov 5, 2017
1 parent 81118b8 commit bad6707
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion integration-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.js
!/examples/beginner/chars.ts
#!/examples/beginner/chars.ts
!/examples/**.ts
1 change: 0 additions & 1 deletion integration-ts/examples/beginner/chars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Streams, F, C, N} from '@robusta/trash'

import {assertEquals} from '../../assert';


Expand Down
38 changes: 38 additions & 0 deletions integration-ts/examples/flow/try-with-no-or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Streams, F, C,Option, N, SingleParser} from '@robusta/trash'
import {assertFalse} from '../../assert';

function day() {
return C.stringIn(['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']);
}

function blank(){
return C.char(' ').rep().thenReturns(' ');
}

const separator = () => C.string('---');

function emptyTry() {
return F.try(C.string('xyz'));
}

function optAlternative() {
return C.string('xyz').opt()
}

function combinator() {

return day()
.then(blank()).rep()
.then(separator())
.then(optAlternative().map(x=>x.orElse('12')))//.debug('we pass the option', false)
.then(emptyTry())//.debug('we pass the try', false)
.then(day());

}

const string = 'TUESDAY THURSDAY TUESDAY ---FRIDAY';

let stream = Streams.ofString(string);
let parsing = combinator().parse(stream);

assertFalse(parsing.isAccepted());
40 changes: 39 additions & 1 deletion integration-ts/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,42 @@ class A {

class B extends A{
stuff(){return 5;}
}
}



export class Option<T> {
value:T;
constructor(x:T){
this.value = x;
}
get(){return this.value}

orElse(v: T){
return this.value || v;
}
}

class Parser<T>{
value: T

constructor(x:T){
this.value = x;
}

map<Y>( func: (x:T)=>Y ){
return func(this.value);
}
}

const option = new Option(['a', 'b', 'c']);

new Parser(option).map(x => x.orElse(['a'])[0].charAt(1));








0 comments on commit bad6707

Please sign in to comment.