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] = ?
scala
add a comment |
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] = ?
scala
6
What's wrong with3 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 methodsto
andfilter
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
add a comment |
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] = ?
scala
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
scala
edited Nov 19 at 18:18
asked Nov 19 at 18:15
FrankS
11
11
6
What's wrong with3 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 methodsto
andfilter
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
add a comment |
6
What's wrong with3 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 methodsto
andfilter
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
add a comment |
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
Do you want to add some explanations what your code does?
– Boern
Nov 20 at 9:28
add a comment |
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
Do you want to add some explanations what your code does?
– Boern
Nov 20 at 9:28
add a comment |
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
Do you want to add some explanations what your code does?
– Boern
Nov 20 at 9:28
add a comment |
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
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
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
add a comment |
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
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.
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.
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%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
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
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
andfilter
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