Scala: How to design the high order function?2019 Community Moderator ElectionScala vs. Groovy vs. ClojureIs the Scala 2.8 collections library a case of “the longest suicide note in history”?Ordering of parameters to make use of curryingScala type mismatch problem (expected Map, found scala.collection.mutable.HashMap)What are all the uses of an underscore in Scala?Constructors with multi-parameter or sequences in Scala?Scala Higher Order Function Little ConfusedSpark performance for Scala vs PythonHow to deal with List of currying function in ScalaScala high order functions confusion
Examples of a statistic that is not independent of sample's distribution?
Expressing logarithmic equations without logs
Accepted offer letter, position changed
How many characters using PHB rules does it take to be able to have access to any PHB spell at the start of an adventuring day?
Coax or bifilar choke
Is "conspicuously missing" or "conspicuously" the subject of this sentence?
Vocabulary for giving just numbers, not a full answer
How to detect if C code (which needs 'extern C') is compiled in C++
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
An alternative proof of an application of Hahn-Banach
How did Alan Turing break the enigma code using the hint given by the lady in the bar?
NASA's RS-25 Engines shut down time
Rewrite the power sum in terms of convolution
Are babies of evil humanoid species inherently evil?
I reported the illegal activity of my boss to his boss. My boss found out. Now I am being punished. What should I do?
How are showroom/display vehicles prepared?
Should I tell my boss the work he did was worthless
How do I express some one as a black person?
Intuition behind counterexample of Euler's sum of powers conjecture
What was the Kree's motivation in Captain Marvel?
cat shows nothing
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Having the player face themselves after the mid-game
Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?
Scala: How to design the high order function?
2019 Community Moderator ElectionScala vs. Groovy vs. ClojureIs the Scala 2.8 collections library a case of “the longest suicide note in history”?Ordering of parameters to make use of curryingScala type mismatch problem (expected Map, found scala.collection.mutable.HashMap)What are all the uses of an underscore in Scala?Constructors with multi-parameter or sequences in Scala?Scala Higher Order Function Little ConfusedSpark performance for Scala vs PythonHow to deal with List of currying function in ScalaScala high order functions confusion
I want to design a high order function in scala which may looks like flowing:
def process(data: Seq[Double], costFun: **): Double
costFun is a function which can be used to calculate the cost of a method, since I have serval cost functions, which may have different signatures, like:
def costGauss(data: Seq[Double], scalaShift: Boolean): Double
def costKernal(data: Seq[Double], theta: Int): Double
how should I design the process function to enable that cost functions which have different signatures can be passed to it as parameter costFun?
scala currying
add a comment |
I want to design a high order function in scala which may looks like flowing:
def process(data: Seq[Double], costFun: **): Double
costFun is a function which can be used to calculate the cost of a method, since I have serval cost functions, which may have different signatures, like:
def costGauss(data: Seq[Double], scalaShift: Boolean): Double
def costKernal(data: Seq[Double], theta: Int): Double
how should I design the process function to enable that cost functions which have different signatures can be passed to it as parameter costFun?
scala currying
If each function has a different signature, how do you plan to call them on yourprocessfunction?
– Luis Miguel Mejía Suárez
Mar 6 at 15:15
3
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37
add a comment |
I want to design a high order function in scala which may looks like flowing:
def process(data: Seq[Double], costFun: **): Double
costFun is a function which can be used to calculate the cost of a method, since I have serval cost functions, which may have different signatures, like:
def costGauss(data: Seq[Double], scalaShift: Boolean): Double
def costKernal(data: Seq[Double], theta: Int): Double
how should I design the process function to enable that cost functions which have different signatures can be passed to it as parameter costFun?
scala currying
I want to design a high order function in scala which may looks like flowing:
def process(data: Seq[Double], costFun: **): Double
costFun is a function which can be used to calculate the cost of a method, since I have serval cost functions, which may have different signatures, like:
def costGauss(data: Seq[Double], scalaShift: Boolean): Double
def costKernal(data: Seq[Double], theta: Int): Double
how should I design the process function to enable that cost functions which have different signatures can be passed to it as parameter costFun?
scala currying
scala currying
edited Mar 6 at 16:10
Andrey Tyukin
29.2k42350
29.2k42350
asked Mar 6 at 15:14
xiaojia zhangxiaojia zhang
10315
10315
If each function has a different signature, how do you plan to call them on yourprocessfunction?
– Luis Miguel Mejía Suárez
Mar 6 at 15:15
3
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37
add a comment |
If each function has a different signature, how do you plan to call them on yourprocessfunction?
– Luis Miguel Mejía Suárez
Mar 6 at 15:15
3
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37
If each function has a different signature, how do you plan to call them on your
process function?– Luis Miguel Mejía Suárez
Mar 6 at 15:15
If each function has a different signature, how do you plan to call them on your
process function?– Luis Miguel Mejía Suárez
Mar 6 at 15:15
3
3
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37
add a comment |
1 Answer
1
active
oldest
votes
Seems that you need just Seq[Double] => Double there:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(data: Seq[Double], scalaShift: Boolean): Double = ???
def lossKernel(data: Seq[Double], theta: Int): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(_, true))
processData(data, lossKernel(_, 1234))
Even better, use multiple argument lists with currying:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(scalaShift: Boolean)(data: Seq[Double]): Double = ???
def lossKernel(theta: Int)(data: Seq[Double]): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(true))
processData(data, lossKernel(1234))
And by the way: don't use Floats, especially not for tiny results that take O(1) memory. Seq[Float] => Double would make some sense, but not the other way round.
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
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%2f55026379%2fscala-how-to-design-the-high-order-function%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
Seems that you need just Seq[Double] => Double there:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(data: Seq[Double], scalaShift: Boolean): Double = ???
def lossKernel(data: Seq[Double], theta: Int): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(_, true))
processData(data, lossKernel(_, 1234))
Even better, use multiple argument lists with currying:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(scalaShift: Boolean)(data: Seq[Double]): Double = ???
def lossKernel(theta: Int)(data: Seq[Double]): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(true))
processData(data, lossKernel(1234))
And by the way: don't use Floats, especially not for tiny results that take O(1) memory. Seq[Float] => Double would make some sense, but not the other way round.
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
add a comment |
Seems that you need just Seq[Double] => Double there:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(data: Seq[Double], scalaShift: Boolean): Double = ???
def lossKernel(data: Seq[Double], theta: Int): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(_, true))
processData(data, lossKernel(_, 1234))
Even better, use multiple argument lists with currying:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(scalaShift: Boolean)(data: Seq[Double]): Double = ???
def lossKernel(theta: Int)(data: Seq[Double]): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(true))
processData(data, lossKernel(1234))
And by the way: don't use Floats, especially not for tiny results that take O(1) memory. Seq[Float] => Double would make some sense, but not the other way round.
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
add a comment |
Seems that you need just Seq[Double] => Double there:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(data: Seq[Double], scalaShift: Boolean): Double = ???
def lossKernel(data: Seq[Double], theta: Int): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(_, true))
processData(data, lossKernel(_, 1234))
Even better, use multiple argument lists with currying:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(scalaShift: Boolean)(data: Seq[Double]): Double = ???
def lossKernel(theta: Int)(data: Seq[Double]): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(true))
processData(data, lossKernel(1234))
And by the way: don't use Floats, especially not for tiny results that take O(1) memory. Seq[Float] => Double would make some sense, but not the other way round.
Seems that you need just Seq[Double] => Double there:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(data: Seq[Double], scalaShift: Boolean): Double = ???
def lossKernel(data: Seq[Double], theta: Int): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(_, true))
processData(data, lossKernel(_, 1234))
Even better, use multiple argument lists with currying:
def processData(data: Seq[Double], lossFunc: Seq[Double] => Double): Double = ???
def lossGauss(scalaShift: Boolean)(data: Seq[Double]): Double = ???
def lossKernel(theta: Int)(data: Seq[Double]): Double = ???
val data: Seq[Double] = Seq(1.0, 2.0, 3.0)
processData(data, lossGauss(true))
processData(data, lossKernel(1234))
And by the way: don't use Floats, especially not for tiny results that take O(1) memory. Seq[Float] => Double would make some sense, but not the other way round.
answered Mar 6 at 15:38
Andrey TyukinAndrey Tyukin
29.2k42350
29.2k42350
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
add a comment |
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
Thank you ^-^. I like the curring method. and Float is mistyping, I should use Double instead.
– xiaojia zhang
Mar 6 at 15:44
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%2f55026379%2fscala-how-to-design-the-high-order-function%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
If each function has a different signature, how do you plan to call them on your
processfunction?– Luis Miguel Mejía Suárez
Mar 6 at 15:15
3
it seems you don't need a higher order function but rather just partially apply the cost functions with hyperparamters before you pass them to the process
– Arnon Rotem-Gal-Oz
Mar 6 at 15:20
@ArnonRotem-Gal-Oz Thanks a lot, I thinks the partially apply is what I need.
– xiaojia zhang
Mar 6 at 15:37