Skip to content

Commit

Permalink
refactor(gp): fix #256 replace enum w/ type alias
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace GeneType w/ type alias
  • Loading branch information
postspectacular committed Dec 22, 2020
1 parent b9cfacb commit 6fd4291
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 176 deletions.
9 changes: 3 additions & 6 deletions packages/gp/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { Fn, Fn2 } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";

export enum GeneType {
TERMINAL,
OP,
}
export type GeneType = "term" | "op";

/**
* Type alias for S-expression based AST
Expand Down Expand Up @@ -66,12 +63,12 @@ export interface MEPOpts<OP, T> extends GPOpts<OP, T, number> {
}

export interface TerminalGene<T> {
type: GeneType.TERMINAL;
type: "term";
value: T;
}

export interface OpGene<OP, A> {
type: GeneType.OP;
type: "op";
op: OP;
args: A[];
}
4 changes: 2 additions & 2 deletions packages/gp/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from "@thi.ng/api";
import { SYSTEM } from "@thi.ng/random";
import { iterate, iterator, repeatedly, takeWhile } from "@thi.ng/transducers";
import { Location, zipper } from "@thi.ng/zipper";
import { ASTNode, ASTOpts, GeneType, OpGene } from "./api";
import type { ASTNode, ASTOpts, OpGene } from "./api";
import { opNode, probabilities, terminalNode } from "./utils";

export class AST<OP, T> {
Expand Down Expand Up @@ -136,7 +136,7 @@ export class AST<OP, T> {
protected asZipper(tree: ASTNode<OP, T>) {
return zipper<ASTNode<OP, T>>(
{
branch: (x) => x.type === GeneType.OP,
branch: (x) => x.type === "op",
children: (x) => (<OpGene<OP, ASTNode<OP, T>>>x).args,
factory: (n, args) =>
opNode((<OpGene<OP, ASTNode<OP, T>>>n).op, args),
Expand Down
4 changes: 2 additions & 2 deletions packages/gp/src/mep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inRange } from "@thi.ng/math";
import { SYSTEM } from "@thi.ng/random";
import { repeatedly } from "@thi.ng/transducers";
import { ASTNode, GeneType, MEPChromosome, MEPGene, MEPOpts } from "./api";
import type { ASTNode, MEPChromosome, MEPGene, MEPOpts } from "./api";
import { opNode, probabilities, terminalNode } from "./utils";

export class MEP<OP, T> {
Expand Down Expand Up @@ -55,7 +55,7 @@ export class MEP<OP, T> {
const depths: number[] = [];
for (let i = 0; i < chromosome.length; i++) {
const gene = chromosome[i];
if (gene.type == GeneType.TERMINAL) {
if (gene.type == "term") {
res[i] = gene;
depths[i] = 1;
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/gp/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { assert } from "@thi.ng/api";
import { add, choices, range } from "@thi.ng/transducers";
import { GeneType, GPOpts, OpGene, TerminalGene } from "./api";
import type { GPOpts, OpGene, TerminalGene } from "./api";

export const terminalNode = <T>(value: T): TerminalGene<T> => ({
type: GeneType.TERMINAL,
type: "term",
value,
});

export const opNode = <OP, A>(op: OP, args: A[]): OpGene<OP, A> => ({
type: GeneType.OP,
type: "op",
op,
args,
});
Expand Down
14 changes: 7 additions & 7 deletions packages/gp/test/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ describe("gp (ast)", () => {

it("generate", () => {
assert.deepStrictEqual(ast.randomAST(), {
type: 1,
type: "op",
op: "+",
args: [
{
type: 1,
type: "op",
op: "-",
args: [
{
type: 0,
type: "term",
value: 5,
},
{
type: 0,
type: "term",
value: 1,
},
],
},
{
type: 1,
type: "op",
op: "*",
args: [
{
type: 0,
type: "term",
value: 8,
},
{
type: 0,
type: "term",
value: 3,
},
],
Expand Down
Loading

0 comments on commit 6fd4291

Please sign in to comment.