Complex case class instance updating
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
add a comment |
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
OK, I find myupdate4variant the best.
– Vasily Kirichenko
Nov 16 '18 at 7:13
add a comment |
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
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
scala
asked Nov 13 '18 at 13:40
Vasily KirichenkoVasily Kirichenko
477211
477211
OK, I find myupdate4variant the best.
– Vasily Kirichenko
Nov 16 '18 at 7:13
add a comment |
OK, I find myupdate4variant 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
add a comment |
1 Answer
1
active
oldest
votes
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.
1
() => Booleanlooks a bit useless, no? It's either the same as constantBoolean, or something side-effecty. If you let the boolean condition depend on theThingfrom the previous round, then yourconditionUpdatesbecome justSeq[Thing => Thing], in which case you can simplyreduce(_ 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
() => Booleanlooks a bit useless, no? It's either the same as constantBoolean, or something side-effecty. If you let the boolean condition depend on theThingfrom the previous round, then yourconditionUpdatesbecome justSeq[Thing => Thing], in which case you can simplyreduce(_ 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
add a comment |
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.
1
() => Booleanlooks a bit useless, no? It's either the same as constantBoolean, or something side-effecty. If you let the boolean condition depend on theThingfrom the previous round, then yourconditionUpdatesbecome justSeq[Thing => Thing], in which case you can simplyreduce(_ 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
add a comment |
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.
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.
edited Nov 13 '18 at 14:08
answered Nov 13 '18 at 13:46
Terry DactylTerry Dactyl
1,104412
1,104412
1
() => Booleanlooks a bit useless, no? It's either the same as constantBoolean, or something side-effecty. If you let the boolean condition depend on theThingfrom the previous round, then yourconditionUpdatesbecome justSeq[Thing => Thing], in which case you can simplyreduce(_ 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
add a comment |
1
() => Booleanlooks a bit useless, no? It's either the same as constantBoolean, or something side-effecty. If you let the boolean condition depend on theThingfrom the previous round, then yourconditionUpdatesbecome justSeq[Thing => Thing], in which case you can simplyreduce(_ 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
OK, I find my
update4variant the best.– Vasily Kirichenko
Nov 16 '18 at 7:13