yield - Scala Comprehension Errors -
i working on of exercism.io exercises. current 1 working on scala dna exercise. here code , errors receiving:
for reference, dna instantiated strand string. dna can call count (which counts strand single nucleotide passed) , nucletidecounts counts of respective occurrences of each nucleotide in strand , returns map[char,int]
.
class dna(strand:string) { def count(nucleotide:char): int = { strand.count(_ == nucleotide) } def nucleotidecounts = ( { n <- strand c <- count(n) } yield (n, c) ).tomap }
the errors receiving are:
error:(10, 17) value map not member of int c <- count(n) ^
error:(12, 5) cannot prove char <:< (t, u). ).tomap ^
error:(12, 5) not enough arguments method tomap: (implicit ev: <:<[char,(t, u)])scala.collection.immutable.map[t,u]. unspecified value parameter ev. ).tomap ^
i quite new scala, enlightenment on why these errors occurring , suggestions fixing them appreciated.
for
comprehensions work on traversable's have flatmap
, map
methods defined, error message pointing out.
in case count
returns simple integer no need "iterate" on it, add result set.
for { n <- strand } yield (n, count(n))
on side note solution not optimal in case of strand aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
count going called many times. recommend calling toset
distinct char
s only:
for { n <- strand.toset } yield (n, count(n))
Comments
Post a Comment