return a list of numbers in scala for which a predicate holds











up vote
-1
down vote

favorite












I want to write a function in scala calcMod(a, b, c) where a should serve as a predicate and b and c taking the range of numbers (e.g. 3(incl.)...9(excl.)) which have to be evaluated and return a list of numbers in this range for which the predicate holds.



For example the function-call calcMod(k => k % 2 == 0, 3, 9) should evaluate in Return(4, 6, 8)



The fact that I have mod 2 == 0 makes it clear that even numbers will always be returned. I want to solve this with linear recursion.



def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = ?









share|improve this question




















  • 6




    What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
    – Andrey Tyukin
    Nov 19 at 18:17








  • 2




    Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
    – Luis Miguel Mejía Suárez
    Nov 19 at 18:22















up vote
-1
down vote

favorite












I want to write a function in scala calcMod(a, b, c) where a should serve as a predicate and b and c taking the range of numbers (e.g. 3(incl.)...9(excl.)) which have to be evaluated and return a list of numbers in this range for which the predicate holds.



For example the function-call calcMod(k => k % 2 == 0, 3, 9) should evaluate in Return(4, 6, 8)



The fact that I have mod 2 == 0 makes it clear that even numbers will always be returned. I want to solve this with linear recursion.



def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = ?









share|improve this question




















  • 6




    What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
    – Andrey Tyukin
    Nov 19 at 18:17








  • 2




    Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
    – Luis Miguel Mejía Suárez
    Nov 19 at 18:22













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to write a function in scala calcMod(a, b, c) where a should serve as a predicate and b and c taking the range of numbers (e.g. 3(incl.)...9(excl.)) which have to be evaluated and return a list of numbers in this range for which the predicate holds.



For example the function-call calcMod(k => k % 2 == 0, 3, 9) should evaluate in Return(4, 6, 8)



The fact that I have mod 2 == 0 makes it clear that even numbers will always be returned. I want to solve this with linear recursion.



def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = ?









share|improve this question















I want to write a function in scala calcMod(a, b, c) where a should serve as a predicate and b and c taking the range of numbers (e.g. 3(incl.)...9(excl.)) which have to be evaluated and return a list of numbers in this range for which the predicate holds.



For example the function-call calcMod(k => k % 2 == 0, 3, 9) should evaluate in Return(4, 6, 8)



The fact that I have mod 2 == 0 makes it clear that even numbers will always be returned. I want to solve this with linear recursion.



def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = ?






scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:18

























asked Nov 19 at 18:15









FrankS

11




11








  • 6




    What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
    – Andrey Tyukin
    Nov 19 at 18:17








  • 2




    Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
    – Luis Miguel Mejía Suárez
    Nov 19 at 18:22














  • 6




    What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
    – Andrey Tyukin
    Nov 19 at 18:17








  • 2




    Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
    – Luis Miguel Mejía Suárez
    Nov 19 at 18:22








6




6




What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
– Andrey Tyukin
Nov 19 at 18:17






What's wrong with 3 until 9 filter(_ % 2 == 0)? Why reinvent anything at all here?
– Andrey Tyukin
Nov 19 at 18:17






2




2




Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
– Luis Miguel Mejía Suárez
Nov 19 at 18:22




Hi @FrankS, welcome to StackOverflow. You may take a look to how to ask to improve this and future questions. Specially, you should provide some research effort and/or some code to probe you already tried to solve your problem by yourself. BTW, I'm sure you're doing this for an assignment or for learning since as Andrey already point out you can just use the built-in methods to and filter instead of doing plain recursion - so maybe try something and if you get stuck come here, we will be happy to help. As an advice think on the base case first.
– Luis Miguel Mejía Suárez
Nov 19 at 18:22












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Below function will go from b until c, then apply filter with the function we got in argument.



 def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = (b until c filter a).toList





share|improve this answer























  • Do you want to add some explanations what your code does?
    – Boern
    Nov 20 at 9:28











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',
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%2f53380463%2freturn-a-list-of-numbers-in-scala-for-which-a-predicate-holds%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








up vote
0
down vote













Below function will go from b until c, then apply filter with the function we got in argument.



 def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = (b until c filter a).toList





share|improve this answer























  • Do you want to add some explanations what your code does?
    – Boern
    Nov 20 at 9:28















up vote
0
down vote













Below function will go from b until c, then apply filter with the function we got in argument.



 def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = (b until c filter a).toList





share|improve this answer























  • Do you want to add some explanations what your code does?
    – Boern
    Nov 20 at 9:28













up vote
0
down vote










up vote
0
down vote









Below function will go from b until c, then apply filter with the function we got in argument.



 def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = (b until c filter a).toList





share|improve this answer














Below function will go from b until c, then apply filter with the function we got in argument.



 def calcMod(a: Int => Boolean, b: Int, c: Int): List[Int] = (b until c filter a).toList






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 9:33

























answered Nov 20 at 8:31









Mukesh prajapati

389215




389215












  • Do you want to add some explanations what your code does?
    – Boern
    Nov 20 at 9:28


















  • Do you want to add some explanations what your code does?
    – Boern
    Nov 20 at 9:28
















Do you want to add some explanations what your code does?
– Boern
Nov 20 at 9:28




Do you want to add some explanations what your code does?
– Boern
Nov 20 at 9:28


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53380463%2freturn-a-list-of-numbers-in-scala-for-which-a-predicate-holds%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

Costa Masnaga

Fotorealismo

Sidney Franklin