Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • May 16 17:07

    dependabot[bot] on npm_and_yarn

    (compare)

  • May 16 17:07
    dependabot[bot] closed #51
  • May 16 17:07
    dependabot[bot] commented #51
  • May 16 17:07
    dependabot[bot] labeled #80
  • May 16 17:07
    dependabot[bot] opened #80
  • May 16 17:07

    dependabot[bot] on npm_and_yarn

    Bump nodemailer from 2.7.2 to 6… (compare)

  • Apr 09 05:31
    dependabot[bot] labeled #79
  • Apr 09 05:31
    dependabot[bot] opened #79
  • Apr 09 05:31

    dependabot[bot] on npm_and_yarn

    Bump moment from 2.24.0 to 2.29… (compare)

  • Apr 08 22:56
    dependabot[bot] labeled #78
  • Apr 08 22:56
    dependabot[bot] opened #78
  • Apr 08 22:56

    dependabot[bot] on npm_and_yarn

    Bump moment in /angular4-Bootst… (compare)

  • Mar 02 07:53

    dependabot[bot] on npm_and_yarn

    (compare)

  • Mar 02 07:53
    dependabot[bot] closed #69
  • Mar 02 07:53
    dependabot[bot] commented #69
  • Mar 02 07:53
    dependabot[bot] labeled #77
  • Mar 02 07:53
    dependabot[bot] opened #77
  • Mar 02 07:53

    dependabot[bot] on npm_and_yarn

    Bump karma from 4.0.1 to 6.3.16… (compare)

  • Mar 01 23:56

    dependabot[bot] on npm_and_yarn

    (compare)

  • Mar 01 23:56
    dependabot[bot] closed #67
Jean-Jacques Dubray
@jdubray
SAM is also inherently easy to persist (see the SAM-SAFE implementation)
david kaye
@dfkaye_twitter
@jdubray For clarification, by "function generators in javascript" do you mean generator functions that create iterable/iterator objects?
That's a problem that Redux was never able to solve, unfortunately.
Jean-Jacques Dubray
@jdubray
No, I had not. Generator functions could be used to implement SAM (as you can see, it's easy to "wait for an action"), I am not sure about the other parts of the pattern like render and nap. A generator function inherently yields to the caller and wait for another call (which work well for even handlers and request/response types of interactions). I am not sure about a "reactive loop implementation" where you pass messages along the control thread.
Jean-Jacques Dubray
@jdubray
After that the author gets into the mode "with my new hammer, everything looks like a nail", that piece of code is horrendous https://jsfiddle.net/awto/y3x6L9wu/
Slađan Ristić
@sladiri
Generators seem to be another way to implement state machines in plain javascript. Another example https://codepen.io/rgdelato/pen/vGNRqP. I wanted to try that to represent the control state, in some cases it might be useful, when your logic benefits from being defined as a state machine.
Slađan Ristić
@sladiri
But yes, the generator state is ephermal, but maybe you can still benefit because it is easier to read. Being able to serialise the state is really nice, that is what we do to save state in a SPA when navigating away from a page and want to come back. We got this feature almost for free. (Heh, so maybe generators are not really possible to use easily.)
Slađan Ristić
@sladiri
image.png
messages can only be bits, not commands
just saw this and it reminded me of the "proposal pattern". This seems to me the most foreign principle when explaining SAM, but it has been proposed before, no pun intended
https://www.youtube.com/watch?v=9MqVfzxAp6A
Slađan Ristić
@sladiri
on my millionth thought though, this is just decoupling systems via events :)
Jean-Jacques Dubray
@metapgmr_twitter
I may I have shared this article before (from Achim Schneider): https://codingconnects.com/sam-pattern
1 reply
david kaye
@dfkaye_twitter

Andrew Ray updated his blog design and tweeted about it so I revisited and found this Flux pattern post (dated Nov. 13 2021, but I recall seeing it circa 2014). Have to say, understanding the SAM pattern helped me understand Flux (without Redux) this time around.

https://andrewray.me/blog/the-mental-model-that-helped-me-finally-understand-flux

Also a list from a different tweeter about 3 state management libraries:
  • Zustand is for flux-like state, like Redux.
  • Jotai is for atomic state, like Recoil.
  • Valtio is for proxy state, like MobX.
Jean-Jacques Dubray
@metapgmr_twitter
@sladiri this is coming really the definition of Actions from Dr Lamport that i tried to translate, and i dont claim that any ideas in SAM are mine, I am just a translator and if anyone has a better translation, I'll take it. I just don't believe that scientists the caliber of Newton or Lamport are blogging all day (like me), so i tend to be dismissive when people who thought about something for 10 min come up with better alternatives to TLA+ for instance.
Jean-Jacques Dubray
@metapgmr_twitter
Who needs a new framework? https://remix.run/docs/en/v1/tutorials/blog
it doesn't seem to have state management built in (who needs that anyways?) https://remix.run/docs/en/v1/guides/data-writes#remix-mutation-start-to-finish
Jean-Jacques Dubray
@metapgmr_twitter
That's unrelated to SAM, but this project is near and dear to my heart, we have been busy in my engineering organization building a schema architecture that I designed 12 years ago. GraphQL has been a game changer to realize that design. I expect that this now FOSS project will solve a major pain point in microservices: https://github.com/nav-inc/nav-schema-architecture
Special credit to Michal Scienski, Daniel Zemichael, Jeff Warner, Jovon McCloud and Brent Tubbs. Michal has created a build pipeline that's out of this world. I expect that it will take a life of its own and make a durable impact on low code projects.
For reference, this is the article I wrote 12 years ago: https://www.infoq.com/news/2009/02/message-type-architecture/
GraphQL (as a syntax) and its metamodel deliver an unprecedented DevX.
Jean-Jacques Dubray
@metapgmr_twitter
Here is my post on if-then-else. It's a key aspect of keeping mutations ckearly defined. https://dzone.com/articles/revisiting-switch-and-if-then-else
Gaetano Miranda
@gaemir
Hello all, I am receiving a 404 when loading the dzone article. Is anyone able to load it? Thank you.
Pete Barnett
@petebarnett
@metapgmr_twitter do you have another copy of that article anywhere? i'm really interested in reading it
Jean-Jacques Dubray
@metapgmr_twitter
Sorry, I had made a mistake and the article went back in moderation when I corrected it (for the last couple of days)

In computer science, there is nothing more fundamental and intuitive than control structures. Every student learned about them in the first few weeks of any computer science program. We could not code without them, period. But they are not set in stone: we did get rid of the infamous GOTO in the 80s!

At the pre-ALGOL meeting held in 1959 Heinz Zemanek explicitly threw doubt on the necessity for GOTO statements; at the time no one paid attention to his remark, including Edsger W. Dijkstra, who later became the iconic opponent of GOTO.[3] The 1970s and 1980s saw a decline in the use of GOTO statements in favor of the "structured programming" paradigm, with goto criticized as leading to "unmaintainable spaghetti code"

And this paper is far from being the first call for using less conditional flows in programming. Even Uncle Bob has tackled the topic. In npm alone, countless packages have been published to provide an alternative. People tend to think that the catch with switch and if-then-else is that the more branches in your code, the more opportunities for untested and unexpected behavior. Every branch requires different use cases to be tested thoroughly, and even though, some tools, such as Istanbul, provide some indication as to how many branches have been executed, in addition to the traditional function coverages. But branches are part of the nature of programming, there is nothing we can do about it.

In this short opinion brief, I would like to give a new spin on when to use control structures and when to use some functional alternatives.

Ever since I discovered Dr. Lamport's Temporal Logic of Actions, my programming style has changed to become more explicit about assignments vs mutations of the application state, and more conscious about temporal logic. For me, there are three core patterns to programming: Object-Oriented Programming, Functional Programming, and Temporal Programming. A developer should master all three and use them accordingly, no one pattern wins over the other. We cannot write good programs with one pattern only.

One of the key notions of Temporal Programming is control state: when the light is off, and If the switch is in the off position, I can turn the light on otherwise check the circuit breaker (in bold we have the actions that are expected in a given control state). These are extremely familiar concepts we use every day, yet for some reason, we rarely make the control state explicit in the code we write. My take on it is that situation came about from a lack of alternative constructs, and the ease of use of switch and if-then-else, over the rigor needed to use temporal programming constructs, but that discussion is for another article. I'd like to focus here on something much simpler and actionable today.

First, we must absolutely stop using these control structures for (complex) assignments, it often leads to some pretty ugly code that is hard to read and debug. How many times have you written some code like this? come on, tell me in the comments...

How about a functional alternative?

My code is not new, it is actually heavily inspired by that article from Hajime Yamasaki Vukelic, which probably got inspired by many similar articles. I brushed up his code to make it a bit more developer-friendly and implement some automatic behavior.

We can now rewrite the code above in a functional way and do a proper assignment.

As a side note, my programming style uses named predicates such as exists, rather than writing inline condition functions. It makes the code a lot more readable, maintainable and testable for complex predicates (imagine a bug in a complex condition...).

The matchReduce function composes an array of functions conditionally. That may well be related to monads, but I have no idea since, to this day, I am still looking for a clear definition of what a monad is.

You can find some code examples on how to use match and matchReduce here.

Now we could stop here and say, alright, for all assignments I'll use this functional construct, and if you did that, that

const matched = (x) => ({
  on: () => matched(x),
  otherwise: () => x,
})

const match = (...x) => ({
  on: (pred, fn) => {
    const _pred = typeof pred !== 'function' ? (z) => z === pred : pred
    const _fn = typeof fn !== 'function' ? () => fn : fn
    return _pred(...x) ? matched(_fn(...x)) : match(...x)
  },
  otherwise: (fn) => (fn === undefined ? x[0] : typeof fn !== 'function' ? fn : fn(...x)),
})

const matchReduce = (...x) => ({
  on: (pred, fn) => {
    const _pred = typeof pred !== 'function' ? (z) => z === pred : pred
    const _fn = typeof fn !== 'function' ? () => fn : fn
    return _pred(...x) ? matchReduce(_fn(...x)) : matchReduce(...x)
  },
  end: () => (x.length > 1 ? x : R.head(x)),
})
It allows you to write assignments like this:
Jean-Jacques Dubray
@metapgmr_twitter
const a = match(someValue)
    .on(exists, 'foo')
    .otherwise(bar)
Jean-Jacques Dubray
@metapgmr_twitter
You can use this lib to replace things like
if (xyz === 2) a = 5  
// a = match(xyz).on(2, 5)
or more complex conditions and assignments
if (x == 2 && y > 5) a = x + y
// a = match(x, y)       
//       .on( (u,v) => u == 2 && v > 5, (u, v) => u + v)
//       .otherwise( (u, v) => u - v)  // a = x - y
As I mention in the article, I prefer a style nowadays where all the predicates are named functions:
const p = (x,y) => x == 2 && y > 5
Jean-Jacques Dubray
@metapgmr_twitter
the assignment looks like (you can also use values instead of functions in the assignments:
const a = match(x,y)
                .on(p, x + y)
                .otherwise(x - y)
Jean-Jacques Dubray
@metapgmr_twitter
Jean-Jacques Dubray
@metapgmr_twitter
Best wishes for the new year. 2021 was a special year for me, I had brain surgery exactly 3 weeks ago to fix a pinched nerve and the surgery was successful. Science is just amazing, just a hundred years ago I would have been dead as my condition is part of the suicide disease family because of the level of pain it creates. I am like new! It was also a great year professionally and for SAM in particular with the development of sam-fsm. I hope 2022 brings you health, happiness, and success.
JohnGurin
@JohnGurin
:wave: Happy New Year!
Jean-Jacques Dubray
@metapgmr_twitter
My company is hiring for pretty much any position (US remote or in-office UT, CA, PA). We are in a good place and a happy bunch of people. https://www.nav.com/company/careers/current-openings/
Daniel Neveux
@dagatsoin
happy new year everyone, great to read that your operation was a success. It is a relief, sincerely. You made my day !
Pete Barnett
@petebarnett
@metapgmr_twitter happy new year and congrats on the good outcome. I cant imagine how that must have affected your life to date. Huge respect for coming through that. Best wishes for the year to come!
Jean-Jacques Dubray
@metapgmr_twitter
@dagatsoin @petebarnett Thank you! Truly a miracle of science and talent from the OR team.
Aaron W. Hsu
@arcfide
I'm getting back into some web development projects, and I've been reading about SAM rather curiously (especially the links with TLA+). I'm trying to understand how the Svelte and SAM philosophies may or may not overlap, conflict, or compare. I didn't see much in this respect. Does anyone have any opinions on this? I'm tempted to just explore using SAM directly in Vanilla JS (maybe with Web Components), but I'm a little out of touch with the state of the art these days.
Jean-Jacques Dubray
@jdubray
Welcome @arcfide ! I had tried to use SAM with Svelte several years back unsuccessfully, I am not sure where things are at right now and if it would be easier/possible. In general, the closest to SAM's philosophy is Vue with Vuex (Action-Mutation-State). Do you have a pointer to current practices for state management in Svelte?
Aaron W. Hsu
@arcfide
@jdubray I'm afraid the best that I have is the introduction article to Svelte 3 in 2019, in particular, the philosophical platform for reactivity in the section "Moving reactivity into the language." From what I can see, the V = S(M) equation is fulfilled mostly by the S() function taking the model and then updating the appropriate state variables (there are things like contexts and the like for managing and coalescing state into global, cross-component "contexts"), whereupon the View just gets updated automatically. Assuming you don't go the other way and make use of Svelte's update mechanisms within components to update those same variables from components, I think you could call that the SAM pattern. I'm not sure if that's a good fit or not, though, as I'm not super experienced with web-side implementations of the SAM pattern.
When I mean state variables, I mean variables that were used as parameters into Svelte components and that a Svelte component is reactive to, not the Model or State in the TLA+ sense.
I think I'm probably not going to use Svelte for other reasons on this next project, but I'm still curious as to the compatibility and philosophical differences, since I remember Svelte being much nicer to use than the other systems for me when I used it a few years ago.
Aaron W. Hsu
@arcfide
I have a question about the SAM pattern wiring example. In the model, it runs state(model) first and then model.persist() after that. This means that potentially the View could be updated with a state that might be incorrect or inconsistent with the persistent model if the persist() fails for some reason. This would mean that the view could temporarily show an incorrect data to the user, yes?