Complex case class instance updating












0















I need to update an instance of a case class, based on a complex logic. What I've come with:



case class Thing(i: Int, s: String) {
def map(f: Thing => Thing): Thing = f(this)
}

def update1(thing: Thing) = {
var x = thing
if (true) x = x.copy(i = x.i + 3)
if (true) x = x.copy(s = x.s.trim)
if (true) x = x.copy(i = x.i * 7)
x
}

def update2(thing: Thing) = {
val x1 = if (true) thing.copy(i = thing.i + 3) else thing
val x2 = if (true) x1.copy(s = x1.s.trim) else x1
if (true) x2.copy(i = x2.i * 7) else x2
}

def update3(thing: Thing) = {
def up1(x: Thing) = if (true) x.copy(i = x.i + 3) else x
def up2(x: Thing) = if (true) x.copy(s = x.s.trim) else x
def up3(x: Thing) = if (true) x.copy(i = x.i * 7) else x
(up1 _).compose(up2).compose(up3)(thing)
}

def update4(thing: Thing) =
thing
.map { x => if (true) x.copy(i = x.i + 3) else x }
.map { x => if (true) x.copy(s = x.s.trim) else x }
.map { x => if (true) x.copy(i = x.i * 7) else x }


Where if (true) may be a complex if..else or match, etc.



I don't like all the variants, except the last one, but it requires map function definition, which is not very nice (i.e. the approach is not completely general). Also note that Thing is not generic, so I don't need a Functor instance + Cats, etc.



Is there an idiomatic way for this task?










share|improve this question























  • OK, I find my update4 variant the best.

    – Vasily Kirichenko
    Nov 16 '18 at 7:13
















0















I need to update an instance of a case class, based on a complex logic. What I've come with:



case class Thing(i: Int, s: String) {
def map(f: Thing => Thing): Thing = f(this)
}

def update1(thing: Thing) = {
var x = thing
if (true) x = x.copy(i = x.i + 3)
if (true) x = x.copy(s = x.s.trim)
if (true) x = x.copy(i = x.i * 7)
x
}

def update2(thing: Thing) = {
val x1 = if (true) thing.copy(i = thing.i + 3) else thing
val x2 = if (true) x1.copy(s = x1.s.trim) else x1
if (true) x2.copy(i = x2.i * 7) else x2
}

def update3(thing: Thing) = {
def up1(x: Thing) = if (true) x.copy(i = x.i + 3) else x
def up2(x: Thing) = if (true) x.copy(s = x.s.trim) else x
def up3(x: Thing) = if (true) x.copy(i = x.i * 7) else x
(up1 _).compose(up2).compose(up3)(thing)
}

def update4(thing: Thing) =
thing
.map { x => if (true) x.copy(i = x.i + 3) else x }
.map { x => if (true) x.copy(s = x.s.trim) else x }
.map { x => if (true) x.copy(i = x.i * 7) else x }


Where if (true) may be a complex if..else or match, etc.



I don't like all the variants, except the last one, but it requires map function definition, which is not very nice (i.e. the approach is not completely general). Also note that Thing is not generic, so I don't need a Functor instance + Cats, etc.



Is there an idiomatic way for this task?










share|improve this question























  • OK, I find my update4 variant the best.

    – Vasily Kirichenko
    Nov 16 '18 at 7:13














0












0








0








I need to update an instance of a case class, based on a complex logic. What I've come with:



case class Thing(i: Int, s: String) {
def map(f: Thing => Thing): Thing = f(this)
}

def update1(thing: Thing) = {
var x = thing
if (true) x = x.copy(i = x.i + 3)
if (true) x = x.copy(s = x.s.trim)
if (true) x = x.copy(i = x.i * 7)
x
}

def update2(thing: Thing) = {
val x1 = if (true) thing.copy(i = thing.i + 3) else thing
val x2 = if (true) x1.copy(s = x1.s.trim) else x1
if (true) x2.copy(i = x2.i * 7) else x2
}

def update3(thing: Thing) = {
def up1(x: Thing) = if (true) x.copy(i = x.i + 3) else x
def up2(x: Thing) = if (true) x.copy(s = x.s.trim) else x
def up3(x: Thing) = if (true) x.copy(i = x.i * 7) else x
(up1 _).compose(up2).compose(up3)(thing)
}

def update4(thing: Thing) =
thing
.map { x => if (true) x.copy(i = x.i + 3) else x }
.map { x => if (true) x.copy(s = x.s.trim) else x }
.map { x => if (true) x.copy(i = x.i * 7) else x }


Where if (true) may be a complex if..else or match, etc.



I don't like all the variants, except the last one, but it requires map function definition, which is not very nice (i.e. the approach is not completely general). Also note that Thing is not generic, so I don't need a Functor instance + Cats, etc.



Is there an idiomatic way for this task?










share|improve this question














I need to update an instance of a case class, based on a complex logic. What I've come with:



case class Thing(i: Int, s: String) {
def map(f: Thing => Thing): Thing = f(this)
}

def update1(thing: Thing) = {
var x = thing
if (true) x = x.copy(i = x.i + 3)
if (true) x = x.copy(s = x.s.trim)
if (true) x = x.copy(i = x.i * 7)
x
}

def update2(thing: Thing) = {
val x1 = if (true) thing.copy(i = thing.i + 3) else thing
val x2 = if (true) x1.copy(s = x1.s.trim) else x1
if (true) x2.copy(i = x2.i * 7) else x2
}

def update3(thing: Thing) = {
def up1(x: Thing) = if (true) x.copy(i = x.i + 3) else x
def up2(x: Thing) = if (true) x.copy(s = x.s.trim) else x
def up3(x: Thing) = if (true) x.copy(i = x.i * 7) else x
(up1 _).compose(up2).compose(up3)(thing)
}

def update4(thing: Thing) =
thing
.map { x => if (true) x.copy(i = x.i + 3) else x }
.map { x => if (true) x.copy(s = x.s.trim) else x }
.map { x => if (true) x.copy(i = x.i * 7) else x }


Where if (true) may be a complex if..else or match, etc.



I don't like all the variants, except the last one, but it requires map function definition, which is not very nice (i.e. the approach is not completely general). Also note that Thing is not generic, so I don't need a Functor instance + Cats, etc.



Is there an idiomatic way for this task?







scala






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:40









Vasily KirichenkoVasily Kirichenko

477211




477211













  • OK, I find my update4 variant the best.

    – Vasily Kirichenko
    Nov 16 '18 at 7:13



















  • OK, I find my update4 variant the best.

    – Vasily Kirichenko
    Nov 16 '18 at 7:13

















OK, I find my update4 variant the best.

– Vasily Kirichenko
Nov 16 '18 at 7:13





OK, I find my update4 variant the best.

– Vasily Kirichenko
Nov 16 '18 at 7:13












1 Answer
1






active

oldest

votes


















0














How about something like:



val myThing = Thing(1, "destroy all monsters")

val conditionUpdates: Seq[(() => Boolean, Thing => Thing)] = ???

val conditionallyUpdatedThing = conditionUpdates.foldLeft(myThing){
case(thingAcc, (condition, update)) =>
if(condition())
update(thingAcc)
else
thingUpdate
}


The advantage is you can pass as many conditional updates as you want.






share|improve this answer





















  • 1





    () => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

    – Andrey Tyukin
    Nov 13 '18 at 14:19











  • It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

    – Terry Dactyl
    Nov 13 '18 at 14:22













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282328%2fcomplex-case-class-instance-updating%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














How about something like:



val myThing = Thing(1, "destroy all monsters")

val conditionUpdates: Seq[(() => Boolean, Thing => Thing)] = ???

val conditionallyUpdatedThing = conditionUpdates.foldLeft(myThing){
case(thingAcc, (condition, update)) =>
if(condition())
update(thingAcc)
else
thingUpdate
}


The advantage is you can pass as many conditional updates as you want.






share|improve this answer





















  • 1





    () => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

    – Andrey Tyukin
    Nov 13 '18 at 14:19











  • It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

    – Terry Dactyl
    Nov 13 '18 at 14:22


















0














How about something like:



val myThing = Thing(1, "destroy all monsters")

val conditionUpdates: Seq[(() => Boolean, Thing => Thing)] = ???

val conditionallyUpdatedThing = conditionUpdates.foldLeft(myThing){
case(thingAcc, (condition, update)) =>
if(condition())
update(thingAcc)
else
thingUpdate
}


The advantage is you can pass as many conditional updates as you want.






share|improve this answer





















  • 1





    () => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

    – Andrey Tyukin
    Nov 13 '18 at 14:19











  • It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

    – Terry Dactyl
    Nov 13 '18 at 14:22
















0












0








0







How about something like:



val myThing = Thing(1, "destroy all monsters")

val conditionUpdates: Seq[(() => Boolean, Thing => Thing)] = ???

val conditionallyUpdatedThing = conditionUpdates.foldLeft(myThing){
case(thingAcc, (condition, update)) =>
if(condition())
update(thingAcc)
else
thingUpdate
}


The advantage is you can pass as many conditional updates as you want.






share|improve this answer















How about something like:



val myThing = Thing(1, "destroy all monsters")

val conditionUpdates: Seq[(() => Boolean, Thing => Thing)] = ???

val conditionallyUpdatedThing = conditionUpdates.foldLeft(myThing){
case(thingAcc, (condition, update)) =>
if(condition())
update(thingAcc)
else
thingUpdate
}


The advantage is you can pass as many conditional updates as you want.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 14:08

























answered Nov 13 '18 at 13:46









Terry DactylTerry Dactyl

1,104412




1,104412








  • 1





    () => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

    – Andrey Tyukin
    Nov 13 '18 at 14:19











  • It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

    – Terry Dactyl
    Nov 13 '18 at 14:22
















  • 1





    () => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

    – Andrey Tyukin
    Nov 13 '18 at 14:19











  • It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

    – Terry Dactyl
    Nov 13 '18 at 14:22










1




1





() => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

– Andrey Tyukin
Nov 13 '18 at 14:19





() => Boolean looks a bit useless, no? It's either the same as constant Boolean, or something side-effecty. If you let the boolean condition depend on the Thing from the previous round, then your conditionUpdates become just Seq[Thing => Thing], in which case you can simply reduce(_ andThen _).

– Andrey Tyukin
Nov 13 '18 at 14:19













It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

– Terry Dactyl
Nov 13 '18 at 14:22







It was not made clear if the Boolean condition was dependent on the Thing instance. If it is then I guess we only need Seq[Thing => Thing] that contains the Boolean check.

– Terry Dactyl
Nov 13 '18 at 14:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282328%2fcomplex-case-class-instance-updating%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly