2

What is your method of dealing with states in state machines? In programming, we often come across situations where we have to model states in our head as to not get the programming wrong, e.g. in my case right now:

You have a tabular webform that gets filled in.
Requirements:
- When the user is able to search the data for a specific value, it should be a cumulative search (e.g.: search for books with "eng" in the language and then with with "19" in the publishing date)

And so the current pseudocode is:
search(e) {
if(!word) {
set(previousState);
}
const searchdata = previousState.filter((row) => row.indexOf(word) != -1);
// but wait, what if I say: searchdata = searchdata.filter() for cumulative search? Oops, I can't, because it doesn't exist at that moment yet. What if I create another variable?

setState(data: searchdata);
}

Current bug:
- when deleting characters from the search field, it doesn't filter cumulatively; it just filters the data according to the current filter.

It's things like these that get me stuck sometimes.

Comments
  • 0
    Multiple maps with hooks
  • 1
    I made a multitasking state machine a while back and I have to make another one soon

    so hot actually

    isn't for anything as lame as web forms though. states is like statistics. looks really boring and super simple. once you have the state machinery figured out making the states is like accounting zzzz

    I didn't read your post very well but

    states (which are like enums):
    - search
    - search with filter
    - no search / recommended, whatever

    you can also have in js a state return the next state it wants to change to. so a normal state when filter applies turns into a filtered one or vice versa. in rust this is done by dereferencing a mutable to change yourself
  • 2
    video games use state machines a lot. like (classic, video game NPC) AI needs to be states like "idling" and "attacking" or "flee"

    I'm making something similar. except some moves can be done in the same turn, and this is a competitive MMO against other programmers lol

    so it has to have multitasking and also checking when you can't do any other potential states you can go into
  • 1
    We have a similar feature, but implemented more transparently.

    There's a search criterion list, and when you hit enter in the search field, it clears the field and adds its content to the criterion list. The contents of that list are introspectable. On any keypress, the search results are produced via all of the criteria, including the contents of the search field, if it's a meaningful filter.
  • 1
    I'm not convinced that this needs a state machine. State machine is a good model for situations where the way each element is processed and not just the numerical result is dependent on the previous section of the list. Even a typical reduce or fold isn't best modelled as a state machine if none of the information produced by the body selects an execution path in later calls to the body.
  • 2
    State machine is a super general model, pretty much the only thing you're promising is that the subset of state that selects an execution path on the next iteration is bounded. you should generally either choose a more specific model or try to phrase your code more directly. This description sounds like you can just put the filters into a list and fold that list into the list of results (in JS fold is the 2-argument form of Array.reduce)
  • 0
    To be specific: as a mediocre programmer I often struggle with state in semi-complex situations. Highly-interdependent states, keeping logic in your head.. it's all a bit cloudy sometimes. You want to save state A, which depends on state B, and you think you're going to keep it all in memory by adding it into a temp state C, but then state B depends on A and A depends on C and quickly, you're going crazy in your mind, losing oversight of the code.

    Solution: study algorithms and computer science better.
  • 1
    @jestdotty amagad jest, so hawt. Yous the cools.

    Permutations, innit? Accounting would be fun because then you already have the model ready.

    Thanks for the explanationz and the enums tip.
  • 0
    @jestdotty You're making cool and interesting stuffs. Well done, jestdotty.
  • 0
    @lorentz A criterion list. Interesting approach. It also decouples the searching from the criteria. Very nice.
  • 0
    @CaptainRant in js you could just have a state/data object and in state A add state A's stuff and in state B add that onto the data, etc

    but then you have to clear the data when appropriate and account for conditions where any piece of data would be missing

    that's how I used to code basically all of JavaScript. it then does not work in rust lol. but js because dynamic offers you a lot of flexibility with data models. you basically don't have to model anything at all! just account for when the data doesn't exist.

    if state A requires state B, then when state B is missing run the function that retrieves and adds state B before it tries state A again

    I actually did this extensively with async... when you work with complex enough APIs for example with timed login keys. every API call needs it and could return expired, if it does you need to get a new one before you retry the call again. sometimes you might also need data from other API calls for other API calls
  • 0
    I guess in rust you could just wrap everything in Option for the same functionality actually... but it might get RAM heavy with god objects or something. maybe could Box everything... lmao

    I just keep reinventing javascript

    ignore me thinking outloud
  • 1
    @jestdotty Accounting for when data doesn't exist is what I sometimes find hard because you have to know what you're going to do first.

    I see the hierarchical logic there.

    I worked with passport.js and the classic access_token and refresh_token in the past before, security wise. I prefer keeping things non-async and few API's if possible.
  • 1
    @jestdotty /ignore jestdotty -thinkingoutloud -jk. I appreciate a creative mind!
  • 0
    @CaptainRant in js you don't have to account for data that doesn't exist yet

    const cache = {}

    if (!cache.access_token)
    cache.access_token = access_token() //whatever

    etc

    or even dump it onto cache.auth
    where then auth is access_token and refresh_token. think that's how I did it for the devrant API when I made some scripts

    then dump other random stuff in there

    no modeling necessary

    I used to say to know how to code something I just have to go code it. turns out in rust that doesn't work but because you don't have to data model before you do stuff in JavaScript that's why I could write JavaScript to literally figure out how to write my code then!
  • 0
    @jestdotty I know, I knowwww, JS is loosely typed. -inner joke lol

    JavaScript sounds like your thing and Rust sounds rusty. It always makes me think of that videogame...
Add Comment