Skip to content

Commit d34975a

Browse files
committed
feat: initial stab at parser idea
Added a very basic recursive descent parser in ideas. It's pretty straightforward and reuses the DecodeError from Decoder for tracking errors. In the long run, when I learn a bit more about parsers, lexers, and grammars, I'll likely completely change the implementation to something like an LR(1) parser generator. For now, this works ok for the minimal places I need some basic string parsing and don't want to deal with regex. Removed that silly bimap function from bimappable.ts. I think the idea was that once could create a Bimappable instance from map and mapSecond.. But that is easier with plain types. Anyway, that's gone now, just write your own bimap function. Also, cleaned up a bunch of the example code that was erroring during deno test --doc. It's not 100% yet and it seems the newest deno versions broke my coverage script.. but that's work for another day.
1 parent 3f2b352 commit d34975a

File tree

13 files changed

+354
-102
lines changed

13 files changed

+354
-102
lines changed

async_either.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,11 @@ export function getInitializableAsyncEither<A, B>(
513513
CA: Initializable<A>,
514514
CB: Initializable<B>,
515515
): Initializable<AsyncEither<B, A>> {
516-
const { init, combine } = E.getInitializableEither(CA, CB);
516+
const ie = E.getInitializableEither(CA, CB);
517517
return {
518-
init: () => () => resolve(init()),
518+
init: () => () => resolve(ie.init()),
519519
combine: (second) => (first) => async () =>
520-
combine(await second())(await first()),
520+
ie.combine(await second())(await first()),
521521
};
522522
}
523523

bimappable.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import type { $, Hold, Kind } from "./kind.ts";
1111
import type { Mappable } from "./mappable.ts";
1212

13-
import { pipe } from "./fn.ts";
14-
1513
/**
1614
* The Bimappable interface. Bimapple includes the methods map and mapSecond.
1715
*
@@ -24,35 +22,3 @@ export interface Bimappable<U extends Kind> extends Mappable<U>, Hold<U> {
2422
ta: $<U, [A, B, C], [D], [E]>,
2523
) => $<U, [A, J, C], [D], [E]>;
2624
}
27-
28-
/**
29-
* Apply two functions simultaneously to the first and second values in a Bimappable structure.
30-
*
31-
* @example
32-
* ```ts
33-
* import * as B from "./bimappable.ts";
34-
* import * as E from "./either.ts";
35-
* import { pipe } from "./fn.ts";
36-
*
37-
* const bimap = B.bimap(E.BimappableEither);
38-
* const transform = bimap(
39-
* (n: number) => n * 2,
40-
* (s: string) => s.toUpperCase()
41-
* );
42-
*
43-
* const result1 = pipe(E.right("hello"), transform); // Right("HELLO")
44-
* const result2 = pipe(E.left(5), transform); // Left(10)
45-
* ```
46-
*
47-
* @since 2.0.0
48-
*/
49-
export function bimap<U extends Kind>(
50-
{ map, mapSecond }: Bimappable<U>,
51-
): <A, B, I, J>(
52-
fai: (a: A) => I,
53-
fbj: (b: B) => J,
54-
) => <C = never, D = unknown, E = unknown>(
55-
ua: $<U, [A, B, C], [D], [E]>,
56-
) => $<U, [I, J, C], [D], [E]> {
57-
return (fai, fbj) => (ua) => pipe(ua, map(fai), mapSecond(fbj));
58-
}

datum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ export function map<A, I>(fai: (a: A) => I): (ta: Datum<A>) => Datum<I> {
512512
* const datumFn = replete((n: number) => n * 2);
513513
* const datumValue = replete(5);
514514
* const result = pipe(
515-
* datumValue,
516-
* apply(datumFn)
515+
* datumFn,
516+
* apply(datumValue)
517517
* );
518518
* console.log(result); // { tag: "Replete", value: 10 }
519519
* ```

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "@baetheus/fun",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"exports": {
55
"./ideas/dux": "./ideas/dux.ts",
6+
"./ideas/parser": "./ideas/parser.ts",
67
"./applicable": "./applicable.ts",
78
"./array": "./array.ts",
89
"./async": "./async.ts",

fn.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ export function memoize<D, A>(ua: Fn<D, A>): Fn<D, A> {
255255
* write: (s: string) => Promise<unknown>,
256256
* }
257257
*
258-
* const mockInOut: InOut = todo(); // InOut !!THROWS!!
258+
* try {
259+
* const mockInOut: InOut = todo(); // InOut !!THROWS!!
260+
* } catch (e) {
261+
* // todo() throws so this block would run
262+
* }
259263
* ```
260264
*
261265
* @since 2.0.0

free.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export function flatmap<A, I>(
275275
* const freeFn = node((n: number) => n * 2);
276276
* const freeValue = node(5);
277277
* const result = pipe(
278-
* freeValue,
279-
* apply(freeFn)
278+
* freeFn,
279+
* apply(freeValue)
280280
* );
281281
*
282282
* console.log(result); // { tag: "Node", value: 10 }

0 commit comments

Comments
 (0)