3 Make question a bit clearer
source | link

Language which transforms mutable constructs Automatically convert mutations in imperative code into functional codeactions?

I wonder if there issomebody tried creating a language, where Iyou can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalentpure, Redux-style actionactions. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue" (as an exaggerated example).

You could also create an equivalent usingconvert code to use the command patterncommand pattern. While not functional, it does contain the mutation of state, and plays a similar role to Redux:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Commandcommand pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

Language which transforms mutable constructs into functional code?

I wonder if there is a language, where I can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalent Redux-style action. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue".

You could also create an equivalent using the command pattern. While not functional, it does contain the mutation of state:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

Automatically convert mutations in imperative code into functional actions?

I wonder if somebody tried creating a language where you can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to pure, Redux-style actions. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue" (as an exaggerated example).

You could also convert code to use the command pattern. While not functional, it does contain the mutation of state, and plays a similar role to Redux:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple sequential app, and at some point decide to add undo capability, you have to rewrite everything to use the command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

2 Added clarification
source | link

I wonder if there is a language, where I can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalent Redux-style action. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue".

You could also create an equivalent using the command pattern. While not functional, it does contain the mutation of state:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

I wonder if there is a language, where I can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalent Redux-style action. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue".

You could also create an equivalent using the command pattern. While not functional, it does contain the mutation of state:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

I wonder if there is a language, where I can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalent Redux-style action. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

To clarify, I do not expect this to work for arbitrary code of course. You would apply it to a scope of code that mutates one or more plain-old-data (POD) objects. The language or tool would then create an equivalent "action" object.

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue".

You could also create an equivalent using the command pattern. While not functional, it does contain the mutation of state:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)

1
source | link

Language which transforms mutable constructs into functional code?

I wonder if there is a language, where I can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to an equivalent Redux-style action. For example (pseudo-js):

atomic function repaint_car(car) {
    car.color = "Blue"
    car.value += 1000
}

would be converted to:

{
    type: REPAINT_CAR,
    new_color: "Blue",
    delta_value: 1000
}

I think this would help if you want to change something deeply nested in the state. It is often hard in functional programming to construct the new state, but in mutable code you just write state.garage[0].cars.cupholder.color = "blue".

You could also create an equivalent using the command pattern. While not functional, it does contain the mutation of state:

class RepaintCarCommand extends Command {
    constructor(color, value) {
        this.color = color;
        this.value = value;
    }
    function do(car) {
        this.previous_color = car.color;
        this.previous_value = car.value;
        car.color = this.color;
        car.value += this.delta_value;
    }
    function undo(car) {
        car.color = this.previous_color;
        car.value = this.previous_value;
    }
}

It would also make the transition from simple to complex app easier. If you start with a simple, sequential command line app, and at some point decide to add undo capability, you have to rewrite everything to use the Command pattern. This capability would allow you to isolate the mutations, without restructuring the app too much.

Does this idea have a name? Do you have introductory links? Does any language implement this? Or are there reasons why this would be impossible or not a good idea?

(Note: I'm not looking for a language recommendation. This is a question about programming language design. I would like to know if this idea has merit, and hope to learn something about equivalences between mutable and immutable/functional constructs. Please don't close this as a "recommendation" question.)