#how can i make an increment or decrement to a length of an array?

71 messages · Page 1 of 1 (latest)

chrome aurora
#

type FixedArray<T, L extends number> = [T, ...T[]] & { length: L };
interface piece {}
type Board<W extends number, H extends number> = FixedArray<FixedArray<piece | null, W>, H>;
function createBoard<W extends number, H extends number>(
  width: W,
  height: H,
  filler: (width: number, height: number) => piece | null = () => null
): Board<W, H> {
  return Array.from({ length: height }, (_e, w) =>
    Object.freeze(Array.from({ length: width }, (_e, h) => filler(w, h))) as FixedArray<piece | null, W>
  ) as Board<W, H>;
}
export const factories = {
  board: {
    /**
     * 产生一个不可变棋盘
     */
    create<W extends number, H extends number>(
      width: W,
      height: H,
      filler: (width: number, height: number) => piece | null = () => null
    ): Board<W, H> {
      return Object.freeze(Array.from({ length: height }, (_e, w) =>
        Object.freeze(Array.from({ length: width }, (_e, h) => filler(w, h))) as ReadonlyArray<piece | null>
      )) as Board<W, H>;
    },
    remove: {
      row<W extends number, H extends number, count extends number>(
        board: Board<W, H>,
        count: count
        rowIndex: number
      ): /* something like Board<W - count, H> */ {
        // ...
      }
    } as const
  } as const
} as const;

I'm new to Functional programming, and I'm working on fectories of Two dimensional grids or Boards.
I need to define something Board<W - count, H> or Board<W + count, H>
But it's obvious that I can't just define it like that.
BTW: if I define FixedArray as readonly, factories will throw a lot of type errors, and define all of them as const won't fix, I want to know why this is happening

chrome aurora
#

!helper

tropic hollowBOT
#

:warning: Please wait a bit longer. You can ping helpers <t:1759210136:R>.

chrome aurora
#

!helper

proven reef
#

i did this before, it was not useful at all in practice and ended up losing the length property
the easiest way is to use tuples [T, T, T] then L extends number = number, and L extends ? [] : L extends 1 ? [T] : L extends 2 ? [T, T] : ... and so on
there is a case similar to this in the source code, array.falt uses this type

chrome aurora
#

Or maybe I can just define it as Board<number, number>

#

BTW I think the rule somewhere on this server seems to say that it's best not to screenshot the code

proven reef
chrome aurora
#

hmm

#

I may have been a bit too fixated on this

proven reef
#

Its part of the process

chrome aurora
#

but do you have any idea about why I can't set FixedArray as readonly in code above?

proven reef
chrome aurora
#

to define

proven reef
chrome aurora
#

object reference should be immutable

#

prevent all kind of changes, each step create a new gameState

#

and I'm trying to do some Functional programming

#

which need everything immutable

proven reef
#

Typescript is not "real", its still JavaScript when compiled, the Readonly types are guides for what you should not edit but you can always do (as any) and edit whatever you want

proven reef
chrome aurora
#

and I have used Object.freeze almost everywhere to prevent changes

chrome aurora
#

you're right, it's good to keep in mind.

proven reef
#

I just got an idea but it depends on the game you are making, may i say it?

chrome aurora
#

sure

#

go on

proven reef
#

You can skip the state memory stuff by making the board editable and take track of the actions done
As example in chess it says "Rook from A2 to A6, takes Queen" and lists the moves to go back and forward in the game without making copies of the board

chrome aurora
#

well

#

I think there are something that i need to clear about

#

sorry

#

but

#

I'm making a tetris rougelite game

#

which board's height increase to prevent you to die

#

and width decrease to make clearing lines easier

#

it should happen on the fly

proven reef
#

How cool

chrome aurora
#

thanks

#

but that will be the problem

#

if All I see is Board<number, number>

#

with out actural width and height

#

It will be a headache

#

just to be sure, you know about those basic concept of rougelite right? players start with nothing, and get upgrades during gameplay, After death or clearance, all upgrades will disappear and be converted into talent points

proven reef
#

Its still possible

chrome aurora
#

Players have schools

#

which defines different board sizes

#

at least those types in hard coded schools and skillpoints data should be right

#

I think

#

I will use codes to generates some data

#

like, board width +1 to be an Weakness of a op school

#

and game generates jsons

#

and other stuff

#

I want typescript to track as many info as possible

#

and piece[][] does not prevent stuff like
[
[piece]
[piece, piece]
]

#

keep some places undefined

proven reef
# chrome aurora I want typescript to track as many info as possible

To achieve that behavior you will need to create a type for each school
But im afraid the moment you do applySchool(school) you will have Board<number, number> again
to fix that you have to start making conditional generic types which might yet not be able to keep track of everything you want
It's possible, it will take time, might get tricky and hard to expand
I again suggest you to use debug or add a window with the variables values which you can just hide when its not on "developer mode"

proven reef
# chrome aurora keep some places undefined

Thats some specific behavior, not done by Array itself, you can create a wrapper class for this so you limit the read only part and keep consistency of all the rules you want like avoiding undefineds

chrome aurora
#

interface FixedArray<T, L extends number>{
[x: number]: T,
length: L
};
interface piece {}
interface Board<W extends number, H extends number> extends FixedArray<FixedArray<piece | null, W>, H> {
width: W,
height: H,
};

proven reef
#

I have to go! hope I have helped at least a little

chrome aurora
#

thanks

#

i have to go too

proven reef
#

Maybe some super coding guy will bring an old stack overflow post with this specific case solved years ago in a fancy way