iterate

Good design: Quantity leads to quality (and iteration speed is vital)

When I saw this tweet this morning:

[DOG MAGICIAN] think of a color, any color ... is it ... gray?

[OTHER DOG] oh my GOD

I knew that I loved the joke, but I didn’t like the presentation. I wanted to put the joke on Facebook, but I know that people like images more than they like text, so I made a second cup of coffee and began putting the text on an image.

How to iterate over a Map in Kotlin (for loop)

Kotlin FAQ: How do I iterate over a Map in Kotlin?

Solution: Here’s an example of how to iterate over a Kotlin Map using a for loop:

val map = mapOf("a" to 1, "b" to 2, "c" to 3)

for ((k,v) in map) {
    println("value of $k is $v")
}

Scala for/yield examples (for-loop and yield examples)

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will be helpful to know how it works. Here's a statement of how the yield keyword works in for loops, based on the documentation in the book, Programming in Scala:

How to iterate (loop) over the elements in a Map in Java 8

If you need to iterate over the elements in a Map in Java 8, this source code shows how to do it:

Map<String, String> map = new HashMap<String, String>();
map.put("first_name", "Alvin");
map.put("last_name",  "Alexander");

// java 8
map.forEach((k,v)->System.out.println("key: " + k + ", value: " + v));

How to loop over a Scala collection with foreach

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.9, “How to loop over a Collection with foreach”.

Problem

You want to iterate over the elements in a collection with the foreach method.

Scala tuple examples and syntax

Scala FAQ: Can you share some examples of using tuples in Scala?

A Scala tuple is a class that can contain a miscellaneous collection of elements. I like to think of them as a little bag or container you can use to hold things and pass them around.

You create a tuple with the following syntax, enclosing its elements in parentheses. Here's a tuple that contains an Int and a String:

Scala Maps (Map class examples)

Here's a quick look at how to use the Scala Map class, with a colllection of Map class examples.

The immutable Map class is in scope by default, so you can create an immutable map without an import, like this:

val states = Map("AL" -> "Alabama", "AK" -> "Alaska")

To create a mutable Map, import it first:

Java String array examples (with Java 5 for loop syntax)

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the Java 5 for loop syntax?

Sure. In this tutorial, we'll show how to declare, populate, and iterate through a String array, including the Java 5 for loop syntax. Because creating a String array is just like creating and using any other Java object array, these examples can also work as more generic object array examples.

How to iterate over Scala Lists with foreach and for

Scala List/sequence FAQ: How do I iterate over a Scala List using the foreach method or for loop?

There are a number of ways to iterate over a Scala List using the foreach operator and for comprehension, and I'll show a few of those approaches here.