Scala function to change an integer [closed] - scala

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there any function in Scala that interchanges an integer with a letter of the alphabet? For example whenever there is a '1' in a list it is interchanged with an 'a'? I have to search through a list, if I find a '1' I have to change It to 'a' ,else print list as it is. Thanks

def alphabet(i: Int): Char = ('a' to 'z')(i - 1)
alphabet: (i: Int)Char
scala> alphabet(1)
res0: Char = a
scala> alphabet(14)
res2: Char = n
scala> alphabet(30)
java.lang.IndexOutOfBoundsException: 29

scala> List[Any](2,1,5,6) map { case 1 => 'a'; case x => x }
res0: List[Any] = List(2, a, 5, 6)

Related

Reverse Integer in Scala [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've been trying to figure out a way to reverse an integer in scala (e.g. 1932 -> 2391) without converting to string and in a purely functional way. My goal is to reverse the Int by converting to List then just using List().reverse then converting back to Int.
def reverseIntList(x: Int) : List[Int] = {
if (!(x <= 0))
if ((x > 0) && (x < 10))
x
else
(x % 10) :: reverseIntList(x / 10) :: Nil
else
List()
}
However, I only get this error code:
recursive method reverseIntList needs result type
First unfold() it, then fold() it back up. No .reverse needed.
def reverseInt(x: Int): Int =
List.unfold(x)(n => Option.when(n > 0)((n%10,n/10)))
.fold(0)(_ * 10 + _)
You are returning an Int instead of List[Int] for the first condition.
Also, some enhancement to apply to your code.
No need in this case for the ":: Nil" to construct your list.
No need for "(x > 0)" condition, as it’s already verified by your first if.
It should look like this.
def reverseIntList(x: Int): List[Int] = {
if (!(x <= 0))
if ((x < 10))
List(x)
else
x % 10 :: reverseIntList(x / 10)
else
List()
}

How to pass a List to function in scala? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to pass a list to the scala function, transform it and return it.
val fun=(lst:List[Int]):List[Int]=>lst+10
error: identifier expected but integer literal found.
There are several issues with your code.
You are mixing the definition of the type of fun with its implementation.
You probably want something like this:
val fun: List[Int] => List[Int] = (lst: List[Int]) => lst + 10
Then the type of the parameter is actually redundant with the type definition of fun, so you can remove it:
val fun: List[Int] => List[Int] = lst => lst + 10
You are trying to use the + operation/method on a List[Int] and a Int. There's no such method. If you want to append an item into a list you can use :+ instead:
val fun: List[Int] => List[Int] = lst => lst :+ 10
More about the available methods on List in the official documentation: https://www.scala-lang.org/api/2.13.x/scala/collection/immutable/List.html

What is the alternative for nested for loops in Scala? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to know what is the alternative for two nested for loops in Scala?
I am converting C code to Scala: I am using two map operationss instead of two nested for loops. Is it correct or are other options available?
Give some example to see how hard is your problem.
Most times you should use just single for loop instead of two nested loops. Consider following snippet:
scala> val array = Array.tabulate(2,3)( (a,b) => a+b )
array: Array[Array[Int]] = Array(Array(0, 1, 2), Array(1, 2, 3))
scala> var sum : Int = 0
sum: Int = 0
scala> for {
| a <- array
| b <- a
| } {
| sum += b
| }
scala> sum
res175: Int = 9
What is the alternative for two nested for loops in scala?
Scala for loops are not comparable to C-ish for loops. The alternatives that come closest to C are:
Scala while-loop
cfor optimization from spire project. See the spire github here https://github.com/non/spire/search?utf8=%E2%9C%93&q=cfor and usage here: https://www.chrisstucchio.com/blog/2014/learning_spire_cfor.html
Example:
cfor(0)(_ < x.size, _ + 1)(i => {
result(i) = 2.0*x(i) + 3.0
})

in scala the variables called res that are returned by the REPL are val (constant) or var (mutable)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In scala shell, are the res variables val or var?
thanks.
res in scala shell are val.
you can verify this by trying to reassign a value to res.
e.g. - scala> List(1) res1: List[Int] = List(1)
scala> res1=List(2) console>:8: error: reassignment to val
res1=List(2)
Showing that the res variable (varName) is only used in val res = expr:
https://github.com/scala/scala/blob/v2.11.5/src/repl/scala/tools/nsc/interpreter/IMain.scala#L495
(The variant is that x ; y is rewritten to x ; val res = y or similar.)

Why is scala tuple access syntax not easier to type? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I find myself wanting to avoid tuples just because the access syntax is ugly. Given that the arity is limited, why does Scala not support a nicer syntax the looks better and is easier to type? This is my suggestion:
val t = (1,2,3)
// Proposed equivalent reference syntax.
assert(t.a == t._1)
assert(t.b == t._2)
assert(t.c == t._3)
So, am I trying to be elegant, or just stupid?
Usually you don't need to use the _1 etc. methods because you can do pattern matching:
val t = (1,2,3)
val (a, b, c) = t
val t2 = ((1, 2), (3, 4))
t2 match {
case ((a, b), (c, d)) if a > c && b > d => true
case _ => false
}