functional programming - A bigger loop in Scala -
i'm using scala create program, hitting wall how many iterations loop can do. i'm still quite new when comes functional programming , programming in scala, have @ moment:
val s = range(1, 999999999).view.foldleft(0)(_ + _ / whatever);
but can't loop few orders of magnitude bigger 999999999, in max value of long. know use loop, cant see fold option that.
anyone know how can achieved?
thanks.
as you've found, seqs cannot contain more int.maxvalue elements. until feature fixed, don't use seq. can
1) use while-loop
2) use for-loop without sequence
but these ways can't use methods of scala collections foldleft
in example.
so need iterator
. e.g.
def bigiterator(start: bigint, end: bigint, step: bigint = 1) = iterator.iterate(start)(_ + step).takewhile(_ <= end)
then
bigiterator(0, bigint("3000000000")).foldleft(bigint(0))(_ + _)
etc work. note: if don't need full range of bigint
, use long
instead it's faster.
Comments
Post a Comment