-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic cube representation types #3
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { CubeIndex } from "./cubeindex" | ||
import { CubeOp } from "./cubeop" | ||
|
||
export interface Cube { | ||
readonly dimension: number; | ||
readonly positionOf: (facelet: number) => CubeIndex; | ||
readonly occupantOf: (position: CubeIndex) => number; | ||
readonly apply: (op: CubeOp) => Cube; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface CubeIndex { | ||
readonly face: number; | ||
readonly row: number; | ||
readonly column: number; | ||
} | ||
|
||
export namespace CubeIndex { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think namespaces are discouraged (or rather, not preferred) in TS/JS. Will need @itstang to fact check me on this, but for es6 the file itself is effectively the "namespace": There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to keep the grouping you can also make it an object. Or, I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 would probably recommend using es6 standard modules over namespaces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. I've moved the static factory methods into a factory object. |
||
export function fromSlotNumber(slotNumber: number, dimension: number): CubeIndex { | ||
const dimSquared = dimension ** 2; | ||
return { | ||
face: Math.trunc(slotNumber / dimSquared), | ||
row: Math.trunc((slotNumber % dimSquared) / dimension), | ||
column: slotNumber % dimension | ||
} | ||
} | ||
|
||
export function toSlotNumber(position: CubeIndex, dimension: number): number { | ||
return ((dimension ** 2) * position.face) + (dimension * position.row) + position.column; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { CubeIndex } from "./cubeindex" | ||
|
||
export interface CubeOp { | ||
readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex; | ||
readonly inverse: () => CubeOp; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk if you want to add more context to these primitives (e.g.
number
). But if so, you can make type aliases:then reference the alias instead of
number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think he might want to abstract way from specific colors and just be able to use facelet indicies, in which case I don't think there's a simple way where we can enforce that type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, the color was just meant to show what you can do with types. The
Facelet = number
is the more relevant example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's neat. Done.