On 7 January 2011 13:22, Casper Bang <[email protected]> wrote:

>
> We've all learned that we should avoid polluting scope with temporary
> variables and exposed state, which is why we should favor iterators
> over indexing. However, there are a great deal of times where we need
> the index either directly or indirectly when i.e. operating of
> multiple aligned structures, treating the first item specially, or the
> last, etc.
>
>
Neither of these are good reasons for needing an actual numeric index, take
this example of aligning structures with the zip operation in scala:


scala> val letters = "abcde" //in Scala, a string is also a Seq[Char]
letters: java.lang.String = abcde


scala> val numerals = List("I", "II", "III", "IV", "V")
numerals: List[java.lang.String] = List(I, II, III, IV, V)

scala> letters zip numerals
res15: scala.collection.immutable.IndexedSeq[(Char, java.lang.String)] =
Vector((a,I), (b,II), (c,III), (d,IV), (e,V))

scala> val lines = letters zip numerals map { case (char, numeral) =>
"position " + numeral + " is " + char }
lines: scala.collection.immutable.IndexedSeq[java.lang.String] =
Vector(position I is a, position II is b, position III is c, position IV is
d, position V is e)

scala> val out = lines mkString "\n"
out: String =
position I is a
position II is b
position III is c
position IV is d
position V is e


or this one for treating the first item in a list specially:

scala> numerals match {

     | case head :: tail => "[" + head + "] " + tail.mkString(" ")

     | case _ => ""
     | }
res13: java.lang.String = [I] II III IV V

See, not an index in sight!  Though if you really do need one, then there's
the ever-useful zipWithIndex:

scala> numerals.zipWithIndex
res14: List[(java.lang.String, Int)] = List((I,0), (II,1), (III,2), (IV,3),
(V,4))


Once you start thinking of this logic as a transform operation from one
structure to another, then it all really starts to make much more sense.



> Also, let's not do parallelization just for the sake of
> parallelization. Lots of iterator blocks make no practical sense to
> spend energy on trying to parallelize, either because the input size
> is statically defined to be small, or because the complexity makes it
> a piece of cake for a modern CPU (< O(n^2) problems) leaving I/O being
> a much larger bottleneck. Thus, I think it is much more important to
> teach the use of the surrounding library (algorithms and data
> structures), than to naively aim to avoid the for loop.
>
>
True, the system should be doing this sort of optimisation for you:
http://ppl.stanford.edu/wiki/index.php/Delite


> In C#,thanks to closures, if you determine your for loop has no side-
> effects and needs to go faster, you just use Parallel.For(...) instead
> and the runtime will take care of the rest. That's simple and easy,
> all you need to understand is whether your loop body lends itself to
> parallelization since this is obviously outside the ability of the
> compiler* to magically infer.
>
> *Here we're talking mainstream imperative Java/C#, not Haskell/ML/
> Clojure/Scala or other experimental languages, AFAIK this is still
> called The Java Posse.
>
>
Arguably, Scala is already fairly mainstream if you look at current
adopters, I wouldn't really call it experimental.
Also, Java is a platform - not just a language.  If a language runs on the
JVM then it's part of the Java ecosystem; I think of this as the posse for
the Java platform...



> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>


-- 
Kevin Wright

gtalk / msn : [email protected]
<[email protected]>mail: [email protected]
vibe / skype: kev.lee.wright
twitter: @thecoda

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to