]>
git.kianting.info Git - uann/blob - index.ts
26cd10df0e081ec2ce445b72c26aeef17bdde0bb
1 var fs
= require('fs');
3 type Some
<T
> = { _tag
: "Some"; value
: T
};
4 type None
= {_tag
: "None"};
7 * @description Like the `Some(a)` and `None` in Rust.
11 * let exam1 : Maybe<Number> = { _tag: "Some", value: 12 };
12 * let exam2 : Maybe<Number> = None;
15 export type Maybe
<T
> = Some
<T
> | None
;
20 * the pair of the string to be matched later and the string that have been matched
21 * @param matched : string have been matched
22 * @param remained : string will be tested whether it'll be matched.
24 export type MatcheePair
= {matched
: string; remained
: string};
28 * it returns a function which test if the first char of the `remained` part of
29 * the argument of the function is `c`, if it's true, update the `MatchedPair` wrapped
30 * in `Some`. Otherwise, it returns `None`.
31 * * @param c : the char to be test.
32 * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
34 export function match1Char(c
: string) : (m
: MatcheePair
) => Maybe
<MatcheePair
> {
35 return (m
: MatcheePair
)=>{
36 const charToBeMatched
= m
.remained
[0];
37 if (charToBeMatched
=== c
){
38 return {_tag
: "Some", value
:{
39 matched
: m
.matched
+ charToBeMatched
,
40 remained
: m
.remained
.substring(1)}};
43 return {_tag
: "None"};
50 * it returns a function which test if the first char of the `remained` part of
51 * the argument of the function is between `l` and `u`, if it's true, update the `MatchedPair` wrapped
52 * in `Some`. Otherwise, it returns `None`.
53 * * @param l : lower bound char, 1-char string
54 * * @param u : upper bound char, 1-char string
55 * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
57 export function matchRange(l
: string, u
: string) : (m
: MatcheePair
) => Maybe
<MatcheePair
> {
58 let lCodepoint
= charToCodepoint(l
);
59 let uCodepoint
= charToCodepoint(u
);
61 throw new Error("Error: the codepoint of `"+l
+"` is not smaller than `"+u
+"`)");
63 return (m
: MatcheePair
)=>{
65 const charToBeMatched
= m
.remained
[0];
66 const codePointToBeMatched
= charToCodepoint(charToBeMatched
);
67 if (codePointToBeMatched
>= lCodepoint
&& codePointToBeMatched
<= uCodepoint
){
68 return {_tag
: "Some", value
:{
69 matched
: m
.matched
+ charToBeMatched
,
70 remained
: m
.remained
.substring(1)}};
73 return {_tag
: "None"};
79 * convert the one-char string to codepoint.
80 * @param s : the string to code point.
81 * @returns if `s.length > 1` return error; otherwise, return the codepoint of `s`.
83 export function charToCodepoint(s
: string): number{
85 throw new Error("Error: the length of input string for "+s
+ "is "+s
.length
+`,
86 however, it should be 1.`);
88 return s
.charCodeAt(0);
93 * @description thendo(input, f, ...) like
96 export function thenDo
<T
>(input
: Maybe
<T
>, f
: Function) : Maybe
<T
>{
97 if (input
._tag
== "None"){
101 let inner
= input
.value
;