explanation: accepted gzm0's answer because rocked!
@eduardo did come in comment suggesting: (for(i <- 10..20; j=runtest(i)) yield -> j).tomap
lets me run build, never posted answer , @gzm0 answer conceptually awesome accepted it.
once other issue figured out relating "can't call constructor" able test these out running program lol
question: have error in expression, how fix more missing fp or scala make mistake?
timingsmap = (i <- powerslist; j <- runtest(i)) yield -> j
i writing first gradle/scala project analysis of algorithms assignment. scala not part of assignment not using homework tag. except work spark in java, brand new functional programming sure that's problem.
here's snippet, the full .scala file on github, ok or post full program here if flailed :)
val powerslist = list(10 20) // create map keep track of power of 2 , resulting timings var timingsmap: map[integer, double] = map() // call runtest function once each power of 2 want, powerslist, // assign timingsmap power of 2 value , results of runtest tree size timingsmap = (i <- powerslist; j <- runtest(i)) yield -> j
the error is:
/home/jim/workspace/scala/redblacktree4150/src/main/scala/main.scala:36: value map not member of double timingsmap = (i <- powerslist; j <- runtest(i)) yield -> j
what think doing in timingsmap = ...
line elements of powerslist
mapped onto i
each iteration of loop, , return value runtest(i)
mapped onto j
each iteration, , taking pairs , putting them timingsmap
. way trying use i
in loop call runtest(i)
causes problem?
runtest looks this:
def runtest(poweroftwo: range): double = { // create tree here var tree = new treemap[int, double] // care create tree integer keys, not value (x <- poweroftwo) { // set next entry in map key, random number tree += (x -> math.random) } stopwatchinst.start() // go through , values in tree, (x <- poweroftwo) { // value, don't take time/overhead store in var, don't need tree.get(x) } // stop watch check time report , return time stopwatchinst.stop() val totaltime = stopwatchinst.elapsed(timeunit.milliseconds) loggerinst.info("run 2 power of " + poweroftwo + " took " + totaltime + " ms") return totaltime }
note: i've had suggestions change j <-
=
in j <-
in line: timingsmap = (i <- powerslist; j <- runtest(i)) yield -> j
another suggestion didn't using yield @ , suggested replacing (10 20).map...
the strange part existing code not show error in intellij editor, breaks when run it. suggestions give type mismatch errors in ide. trying figure out conceptually doing wrong, help! (and of course need work!)
after trying gzm0 answer getting down same road ... code presented doesn't show type mismatches until use gradle run
... whereas when make suggested changes starts give me errors right in ide ... keep em coming! here's latest error based on gzm0s answer:
/home/jim/workspace/scala/redblacktree4150/src/main/scala/main.scala:37: type mismatch; found : list[(scala.collection.immutable.range.inclusive, double)] required: map[integer,double] timingsmap = (i <- powerslist) yield -> runtest(i)
you want:
for (i <- powerslist) yield -> runtest(i)
the result of runtest
not list, therefore can't give for
statement. reason bit of strange error message, due how for
desugared:
for (i <- powerslist; j <- runtest(i)) yield -> j // turns powerslist.flatmap { => runtest(i).map { j => -> j } }
however, result of runtest(i)
double
, doesn't have map
method. looking @ desugaring, error message makes sense.
note point runtest
's result not being list not correct: has map
method allow above statement (i.e. taking kind of lambda) do. however, beyond scope of answer.
we have created list of tuples (since powerslist
list
, result of for
loop list
well). however, want map
. luckily, can call tomap
on list
s contain tuples convert them map
:
val tups = (i <- powerslist) yield -> runtest(i) timingsmap = tups.tomap
a note aside: if want keep j
inside for
-loop, can use equals sign instead of left arrow:
for (i <- powerslist; j = runtest(i)) yield -> j
this turn into:
powerslist.map { => val j = runtest(i) -> j }
which want.
Comments
Post a Comment