These are chat archives for jdubray/sam

21st
Apr 2018
Fred Daoud
@foxdonut
Apr 21 2018 00:20
Greetings!
patchinko is a simple yet powerful utility for mutating objects, able to handle deeply nested properties, modifying properties with functions, and deleting properties. You might find it useful for mutations in your Model.
Here is a patchinko demo
devin ivy
@devinivy
Apr 21 2018 06:36
that does look like another good way to describe proposals!
boris sabadach
@bsabadach
Apr 21 2018 09:41
just layed with this: http://editor.ifmledit.org/
WIYSIWYG editor that creates an entire application you can then download; based on MVVM and observables with the forgotten knockout js.
Jean-Jacques Dubray
@jdubray
Apr 21 2018 13:49
@jshrt thank you for joining the forum and your questions. SAM is quite different from MVC. It's foundation lies in a new way to think about the view, as a function of the model: V = f(M). In other words, what you see is directly and always computed from the model, which is quite different from MVC where the relationship between the view and the model is one of data binding.
SAM goes one step further and prescribes what the f() function looks like:
V = State(Model.present(Action(event))).then(NAP)
That formulation is "reactive" (unlike MVC), the elements of the pattern never wait on any response from the following components. The roles and responsibilities of Actions are quite different from the controller, actions are limited to create proposals to mutate the model and the model is solely responsible for deciding if the mutation is acceptable or not.
The State function ultimately computes the state representation of the application (from which the view is derived). The Next-Action-Predicate is use to decide if given the state of the system there is a need to automatically trigger a "next-action". Without NAP one would need to write this code either in the View, the State representation or even the Model which are not a good place.
SAM is just a simple factoring of your code that can be used wherever you have an event handler. So it is a replacement for MVC, but it's quite different.
boris sabadach
@bsabadach
Apr 21 2018 13:57
Hello @jdubray how are you doing?
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:28
great so far! thank you. I started a new job too!
boris sabadach
@bsabadach
Apr 21 2018 15:30
Cool - and you know what? Daniel @dagatsoin and me have been hired by the same company at more or less the same time! I didn’t even know he was french
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:31
yes, I saw that, small world!
or maybe the SAM community is so large?
boris sabadach
@bsabadach
Apr 21 2018 15:33
Lol - btw I was thinking of merging model and state...
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:35
They are very close, depending on the type of application you many not need a model (proposals are acceptable without much logic and everything else is "computable") or a state (when the application is mutation heavy without too many computables).
SAM is just a pattern, its definition can and should be adapted to a particular situation.
boris sabadach
@bsabadach
Apr 21 2018 15:36
I mean I’would have a model with receive, data and state functions
because state is very related to model so why having a separate object
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:37
sure that works too. There is no right way to structure SAM.
I don't use objects :-)
boris sabadach
@bsabadach
Apr 21 2018 15:37
mean objecr litterals
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:37
:-)
boris sabadach
@bsabadach
Apr 21 2018 15:39
and I can’t find a cleaner way to display views according state function predicates: two many "if"
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:39
Please not that often you could end up with several State functions (Web, Mobile, Voice, IoT). The State Representation can potentially be shared with different types of clients.
Could you clarify?
I am not sure I understand what you mean.
devin ivy
@devinivy
Apr 21 2018 15:41
@bsabadach one idea i've had is to just write a model "validator". you virtually accept the proposal, then check if the model would be valid. if so you commit the change, and if not you throw it out.
boris sabadach
@bsabadach
Apr 21 2018 15:41
`if (state.isready(model) then )… etc
@devinivy Imean on the view side; to choose with view to display
devin ivy
@devinivy
Apr 21 2018 15:42
ah, i see!
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:43
@bsabadach the way I do it is based on the "selected menu item" value.
devin ivy
@devinivy
Apr 21 2018 15:43
in jsx you see this a lot,
<div>
    {loggedIn && <span>Welcome, Boris</span>}
    {!loggedIn && <span>Please log in</span>}
</div>
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:44
yes, that's another way too
boris sabadach
@bsabadach
Apr 21 2018 15:44
That’s what I don’t like in JSX.
devin ivy
@devinivy
Apr 21 2018 15:45
it's a little strange, yeah. but then you have to make a choice :D explicit if()s or implicit evaluation using &&. ironically the latter is what TLA+ programs kinda look like :P
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:46
I do it that way:
page(params) {
        if (params.menuItem === 'home') {
            return this.home(params.home) ;
        } else {
            if (this[params.menuItem] !== undefined) {
                return this[params.menuItem](params[params.menuItem]) ;
            } else {
                return this.fullPage(params[params.menuItem]) ; 
            }
        }
    }
The full sample can be found here
boris sabadach
@bsabadach
Apr 21 2018 15:47
What is params in SAM context?
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:48
it's the state representation.
It would look like this:
{ 
    menuItem: 'home',
    home: { 
        lotsOfStuff: [...]
    },
    blog: {
        posts: [...]
    }
    ...
}
boris sabadach
@bsabadach
Apr 21 2018 15:50
Well anyway your code doesn’t suit me sorry too many if branches.
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:51
?? This is all you need:
page(params) {
            if (this[params.menuItem] !== undefined) {
                return this[params.menuItem](params[params.menuItem]) ;
            } else {
                return this.fullPage(params[params.menuItem]) ; 
            }
    }
You don't need one per "page", it's generic.
boris sabadach
@bsabadach
Apr 21 2018 15:51
I use components
Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:52
Yes, that's a bit more complex in that case, that's why I love so much the V = f(M) paradigm.
boris sabadach
@bsabadach
Apr 21 2018 15:53
But this is the same parad
igm

```const observer = (model) => {
let nextView

if (state.initial(model) || state.busy(model)) {
nextView = view.waiting()
}

if (state.ready(model)) {
nextView = view.ready(model)
}

if (state.wasFiltered(model)) {
nextView = view.ready(model)
}
if (state.wasSorted(model)) {
nextView = view.ready(model)
}
view.display(nextView)
}```

Jean-Jacques Dubray
@jdubray
Apr 21 2018 15:56
Looks like you could use a switch statement?
      switch ( ControlState(model) ) {
          case 'ready': nextView = view.ready(model)
          ...
     }
Note that I was merely proposing that you could write this as:
      nextView = view[ControlState(model)](model)
it's just one line of code now.
boris sabadach
@bsabadach
Apr 21 2018 15:59
in my case all the state function are predicate they return a Boolean
Jean-Jacques Dubray
@jdubray
Apr 21 2018 16:00
You should at a minimum encapsulate them in a ControlState function, it's not always the case that they would be predicate (e.g. menuItem).
I view this as a key advantage of SAM because it aligns well with Finite State Machines so in a case like yours, it still works.
boris sabadach
@bsabadach
Apr 21 2018 16:07
Ah perhaps a map of control states as (Symbols) <=> view states (as Symbols) to be type safe
Jean-Jacques Dubray
@jdubray
Apr 21 2018 16:09
yes, I would imagine.
Jean-Jacques Dubray
@jdubray
Apr 21 2018 16:27
@jshrt WIth respect to your other questions, the devil is in the detail. I spent many years creating the metamodels of different programming paradigms. Here is an example of what I mean by that. If you draw the metamodel of the other patterns/approaches, you will see that it is not the same. For instance Redux's reducers connect events to view via a pure function, a far cry from SAM. Because that simplistic view does not work, lots of stuff was added but additional parts create some confusion. There is nothing I need to add (or take away) when I use SAM. It gives me a simple (but not simplistic) way to factor my code, and I never struggle to decide where to put a piece of code. Everything lands itself clearly and precisely (especially API calls).

@jshrt With respect to that question

multiple action predicates are better handled with a FSM

I used to be a big fan of FSM until I discovered the work of Dr. Lamport and ultimately landed on SAM. FSMs tend to no scale in scope, once you have a dense graph of states and actions, it's becomes difficult to maintain. I tend to have a better experience with SAM.

Fred Daoud
@foxdonut
Apr 21 2018 21:50
@jdubray
what you see is directly and always computed from the model, which is quite different from MVC where the relationship between the view and the model is one of data binding.
SAM goes one step further and prescribes what the f() function looks like:
V = State(Model.present(Action(event))).then(NAP)
That is very well said :100: :+1:
I know it's not the first time you say this :)
But reading it again now I find it's a great summary of SAM.