scala - Why do these similar looking statements yield objects of different types? -
in book 'scala impatient' author provides following 2 examples 'for-comprehension':
for (c <- "hello"; <- 0 1) yield (c + i).tochar // yields "hieflmlmop" (i <- 0 1; c <- "hello") yield (c + i).tochar // yields vector('h', 'e', 'l', 'l', 'o', 'i', 'f', 'm', 'm', 'p')
however, didn't mention why output string in first case, , vector in second. please explain? thanks.
your first example translated like:
"hello".flatmap(c => (0 1).map(i => (c + i).tochar))
and second to
(0 1).flatmap(i => "hello".map(c => (c + i).tochar))
stringops.flatmap
returns string
, first example returns string
well. range.flatmap
returns indexedseq
instead.
Comments
Post a Comment