Store-passing style

Store-passing style is a programming technique that is used to model mutable state without using mutable state.[1][2] It generally arises in the conversion of imperative programs into purely functional ones.

So, for instance, consider this JavaScript program, written in a non-store-passing-style:

var lastWasA = false

// a treebin represents a binary tree of strings.

// a treebin is either
// - a string, or
// - {l : <treebin>, r: <treebin>}

// does an in-order traversal of this tree's
// leaves contain an 'a' followed by a 'b'?
function aThenB(treebin) {
    if (typeof(treebin) === "string") {
        if (treebin === "a") {
            lastWasA = true;
            return false;
        } else if (treebin === "b") {
            if (lastWasA) {
                return true;
            } else {
                lastWasA = false;
                return false;
            }
        } else {
            lastWasA = false;
            return false;
        }
    } else { // not a string, must be an internal node:
        return ((aThenB(treebin.l))||(aThenB(treebin.r)));
    }
}

This contains a reference to a global variable. In store-passing style, the value of the global variable (or variables) is passed along to each call, and also returned from each call and threaded through the next call. The code might look like this:

function aThenB(treebin, lastWasA) {
    if (typeof(treebin) === "string") {
        if (treebin === "a") {
            return {result: false, lastWasA: true};
        } else if (treebin === "b") {
            if (lastWasA) {
                return {result: true, lastWasA: false};
            }
        } else {
            return {result: false, lastWasA: false};
        }
    } else { // not a string, must be an internal node:
        var leftCall = aThenB(treebin.l, lastWasA);
        if (leftCall.result) {
            return {result: true, lastWasA: false}
        } else {
            return aThenB(treebin.r, leftCall.lastWasA);
        }
    }
}

Note that each call takes an extra argument, and two values are now returned; the ordinary return value, and a new value representing the state of the formerly mutable variable.

Store-passing style can be quite painful to write, but can help to eliminate race conditions by isolating state within function calls, and can potentially make code more parallelizable.

See also

References

  1. ^ Friedman, Daniel; Wand, Mitchell (April 2008). Essentials of Programming Languages (3rd ed.). Boston, MA: MIT Press. ISBN 978-0262062794.
  2. ^ Krishnamurthi, Shriram (November 2012). Programming Languages, Application and Interpretation (2nd ed.). self-published. Retrieved 10 February 2016.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.