Iterate over object coffeescript [duplicate] - coffeescript

This question already has answers here:
How to iterate over the keys and values in an object in CoffeeScript?
(4 answers)
Closed 9 years ago.
How do I iterate through the keys in an object to display the key and values associated with it? Say the hash is the following:
hash =
a: 'b'
c: 'd'
e: 'f'

for key, value of hash
console.log "#{key} = #{value}"

Try this:
for k, v of hash
console.log('%s %s', k, v)

Related

assign None to option when element is empty [duplicate]

This question already has answers here:
Convert a List into an Option if it is populated
(5 answers)
Closed 3 years ago.
I have a list of Integer which I need to convert to Option, however when List is empty i need to assign it as none.
please suggest
val list: List[Int] = List()
val x = option(list)
if list is empty x should be none and if list has some value say List(1,2) then x should be Some(List(1,2))
please suggest
Option(list).filter(_.nonEmpty)

get the maximum value in scala map along with the key [duplicate]

This question already has answers here:
Getting all key value pairs having the maximum value from a Scala map
(2 answers)
Closed 4 years ago.
Can anybody help how to get the the maximum value in a map along with its key.
For example map below:
Map(s -> 3, h -> 2, M -> 1, q-> 4)
I should get the key value (q -> 4) as the 4 is the highest.
I tried the max method,keys and values iterator. But none of the return both key and value.
You can use maxBy as
val map_values = Map("s" -> 3, "h" -> 2, "M" -> 1, "q"-> 4)
println(map_values.maxBy(_._2))
//(q,4)
You can get what you want as follows:
map.filter{case (k,v) => v == map.values.max}
With map.values.max you get the max value, and inserting it into the filter statement you obtain the whole key-value pair
How does this look like for you?
map.maxBy { case (key, value) => value }

How can I get all the indexes of a Particular String in String vector in scala [duplicate]

This question already has an answer here:
Find Indexes *Where*
(1 answer)
Closed 6 years ago.
I am new to scala,
How can i get all the indexs for particular string.
for example:
var taluk = List("Hyderabad", "Nampally", "Hyderabad" ,"Khairatabad")
taluk.indexOf("Hyderabad")
output is 0
But I want
output as 0,2
because there are two string match in vector.
One way to do this: zipWithIndex and then collect the indices for values matching yours:
scala> taluk.zipWithIndex.collect { case ("Hyderabad", i) => i }
res0: List[Int] = List(0, 2)

How can I overwrite the values of a tuple in scala? [duplicate]

This question already has answers here:
In Scala, how can I reassign tuple values?
(2 answers)
Closed 7 years ago.
I tried to overwrite the tuple values in scala using
val item = (1,'A',1);
item._1=2;
But I got an error 'reassignment to value'. Then I used stackoverflow "In Scala, how can I reassign tuple values?" and found the following solution:
val item = (1,'A',1);
item = item.copy(_1,2);
But I am getting the same error 'reassignment to value'. I tried with both val and var keyword.
A tuple in Scala is immutable, i.e. you can't change it values.
The second version goes in the correct direction, but you should use it like this:
var item = (1, 'A',1);
item = item.copy(_1 = 2);
I.e. you make it a var not a val so you can reassign it.

Idiomatic way of extracting lists from a list of tuples [duplicate]

This question already has answers here:
Scala: How to convert tuple elements to lists
(5 answers)
Closed 8 years ago.
Is there a concise way of performing the following mapping in Scala?
val listOfTuples: List[Tuple2[Foo, Bar]] = ???
val (foos, bars) = listOfTuples // foo:List[Foo], bar:List[Bar]
I have seen others map the List[Tuple[X, X]] onto a List[List[X]] and then transpose the list, although this only works with tuples composed from homogeneous type parameters.
You should use method unzip like this:
val (foos, bars) = listOfTuples.unzip
There is also a method unzip3 for collections of Tuple3.
And if for arity 3 to 22 you could do this with product-collections:
val foos = listOfTuples._1
val bars = listOfTuples._2