scala - Randomizing the List -
i wrote function choose 1 word randomly lists of words. here code. cannot choose 1 word , cannot print. please tell me what's wrong code.
def long(a: int, b: int): string = { var = 5 var b = 100 (i <- args(1)){ if (i > && < b){ val rand = new random(system.currenttimemillis()) val random_index = rand.nextint(new_sun .length) val result = new_sun(random_index) var guess = println("_ " * result.length) } else{ println("you have input word length of 5 < 100") } return i.tostring } }
there's lot gone wrong here, it's difficult knowing start. taking 1 fragment @ time:
def long
really, unbelievably bad name method!
def long(a: int, b: int): string = { var = 5 var b = 100
you're taking a
, b
parameters, shadowing names create vars. parameters useless.
for (i <- args(1)){ if (i > && < b){ ... } else{ ... } ... }
this doesn't follow recognised pattern nesting of scope, it's practice leave space before opening brace of block. worse still, closing brace for
block aligned closing brace else
clause. guaranteed way produce unmaintainable code. i'm advocate of so-called "one true bracket style", have code formatted this:
for (i <- args(1)) { if (i > && < b) { val rand = new random(system.currenttimemillis()) val random_index = rand.nextint(new_sun .length) val result = new_sun(random_index) var guess = println("_ " * result.length) } else { println("you have input word length of 5 < 100") } return i.tostring }
moving onward...
for (i <- args(1))
what args(1)
, come from? must option or sort of collection used in for-comprehension, , contents must of same type a
, b
i > && < b
comparison valid. i'm assuming args(1)
returns option[int]
, collection seems unlikely.
val random_index = rand.nextint(new_sun .length) val result = new_sun(random_index)
same question. new_sun
, come from. name tells me nothing. space in new_sun .length
odd looking.
for (i <- args(1)) { if (i > && < b) {
is better written:
for (i <- args(1) if > && < b) {
or even
args(1) filter (a b contains _) map { =>
but sadly, can't that, value i
used return value in spite of guard condition.
which reminds me:
var = 5 var b = 100 ... if (i > && < b) ... else println("you have input word length of 5 < 100")
this checking 5 < < 100
, or 6 <= <= 99
in other words. check doesn't match error message.
var guess = println("_ " * result.length)
println
returns unit
. there absolutely no possible reason assign return value variable never used.
return i.tostring
return
statements needed in scala, final expression evaluated become return value. can't delete keyword though, interacts other problems , stop code compiling.
generally, want write code if/else
blocks, for-comprehensions, , other such constructs don't perform side-effects (such calling println
). instead, should try , make these blocks evaluate string
, pass evaluated string println
@ end of method.
you need more aware of variables have, come from, , how informative names are.
and pay attention syntax/layout/formatting. programmers have fought religious ways on less..
Comments
Post a Comment