Help with Set::map(Function)

56 views
Skip to first unread message

Aleksander Długosz

unread,
Aug 20, 2015, 12:04:04 PM8/20/15
to Dart Misc
Im trying to port some dart code to java and i have an issue with one of the dart's functions.
var regions = connectorRegions[connector].map((region) => merged[region]);


This code is from (ln. 211) https://github.com/munificent/hauberk/blob/db360d9efa714efb6d937c31953ef849c7394a39/lib/src/content/dungeon.dart

I want to know how connectorRegions[connector].map translates to java.

Please help :c

-Aleksander Długosz

Bob Nystrom

unread,
Aug 20, 2015, 5:59:45 PM8/20/15
to General Dart Discussion
Hey, that looks familiar. :)

I've explained that exact line of code twice before. I'll reuse one of my previous explanations:

      // Merge the connected regions. We'll pick one region (arbitrarily) and
      // map all of the other regions to its index.
      var regions = connectorRegions[connector]
          .map((region) => merged[region]); 

connectorRegions is a Map in Dart, similar to Map in Java. It associates keys with values. Here, the key is a Vec, a position in the dungeon, and the value is the set of regions the tile at that position is next to. Each region is identified by a number, so the value type is Set<int>, which is like Set in Java. This map tells you which tiles are next to more than one isolated region in the dungeon. These tiles will be used to carve passages that connect the option regions together.

connector is a Vec, one key randomly chosen from connectorRegions. It's the connector that we're carving open to connect its neighboring regions together.

Now we have:

connectorRegions[connector]

That looks up the Set of region numbers that we're going to be merging together.

The Set type extends the IterableBase class which defines a bunch of methods that all sequence-like types support. This includes .map(), which takes a sequence and transforms each element to another object and then returns a new sequence based on those results. Java doesn't currently have an equivalent method, but JDK 8 will.

We're up to:

connectorRegions[connector].map(...)

That takes the set of regions numbers touching the connector and makes a new list that transforms those regions to something else. The something else is based on:

(region) => merged[region]

This is a lambda—an anonymous function. Java is getting these in JDK 8. It creates a new function. That function takes a single parameter, region, and the body returns the result of evaluating merged[region].

merged is another Map. It keeps track of which regions have been combined already. It does this by having keys for each original region number. The value is the new region number that that region was changed to. If, for example, regions 2, 3, 4 all got merged together, then this would have keys 2, 3, 4 and they would all have the same number as their value.

Now we're up to the whole thing:

      var regions = connectorRegions[connector]
          .map((region) => merged[region]); 

So what this does is get the regions touching a connector and get the list of their current region numbers. Since any given region may get merged multiple times during the process of the algorithm (region 5 may become 3 which then becomes 2, etc.), we have to make sure we look up the current region numbers for each neighbor of the connector.

In vanilla Java code, it would be something like:

var regions = new ArrayList<Integer>();
for (int region : connectorRegions.get(connector)) {
  regions.add(merged.get(region));
}

(Forgive any mistakes. It's been a few years since I've written any Java.)

Cheers!

- bob

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Reply all
Reply to author
Forward
0 new messages