DEV Community

Arthur Christoph
Arthur Christoph

Posted on

Polyglot Series in Javascript, Dart, Ruby, Python,Go: Iterating Map and Set

Alt Text

Playground path: collection/map_and_set/iter

Iterating Map

  • In Javascript, Dart and Go is commonly called Map, in Ruby is Hash, and in Python is Dictionary.
  • Iterating Map can be divided into three forms: key-only, value-only or key-value pair.

Iterating Set

  • Iterating set is simpler than list and map, since we do not have key or index to look for, it's simply just an unordered value-only collection.
  • Set is the standard term in these languages.
  • Iterating Set has one form, just like list - iterating its elements: value-only form

Javascript

In Javascript, map can be represented in two types: Object and Map. Both of them have keys(), values(), entries() methods to iterate the three forms. Both, by default, can be iterated, iterating Map returns the element in [key,value] form, whereas iterating object returns the object's property values.
Javascript Set is iterable using for-of

//iterate Map. By default, Map is iterable
for (const k of m) {
  log(k);
}

//iterate Map with keys
for (const k of m.keys()) {
  log(k);
}

//iterate Map with values
for (const v of m.values()) {
  log(v);
}

//iterate map with keys and values
for (const [k, v] of m) {
  log(k, v);
}

//iterate object by default using for-in')
for (const e in objects) {
  log(e);
}
//iterate object with keys
for (const k of Object.keys(objects)) {
  console.log(k);
}
//iterate object with values
for (const v of Object.values(objects)) {
  console.log(v);
}
//iterate object with keys and values
for (const [k, v] of Object.entries(objects)) {
  console.log(k, v);
}
//iterate set
let set = new Set([1, 2, 3]);
for (const e of set) {
  log(e);
}

Dart

Dart, like Javascript also has keys, values and entries but implemented as properties instead of methods. Unlike Javascript, Map is not iterable, so we need to use one of the three methods. However, Set is iterable using for-in

for (var k in d.keys) {
    print(k);
  }
for (var v in d.values) {
  print(v);
}
for (var e in d.entries) {
  print("${e.key} ${e.value}");
}

Set set = new Set.from([1, 2, 3]);
for (var e in set) {
  print(e);
}

Ruby

In Ruby, while there are keys and values properties in Hash class, using each_key and each_value are the more expressive options. Both Hash and Set are iterable using each method.
However, Set needs to be imported explicitly using the usual way of Ruby import: require 'set'

// with keys
d.each_key {|k| p k}
d.keys.each {|k| p k}

// with values
d.each_value {|v| p v}
d.values.each {|k| p k}

// with keys and values
d.each {|k,v| p "#{k} #{v}" }

// iterate set
require 'set'
set = Set.new([1,2,3])
set.each{|e| p e}

Python

Python also have the three forms, implemented as keys(), values(), and items(). Both Dictionary and Set are iterable using the usual for-in

for k in d:
    print(k)

for k in d.keys():
    print(k)

for k in d.values():
    print(k)

# with keys and values
for k, v in d.items():
    print(k, v)

# iterate set
set = {1,2,3}
for e in set:
    print(e)

Go

Map is iterable. Focusing for simplicity, Go only provides for-range to iterate by value, key, and key-value (using underscore to ignore key or value). There is no standard Set , instead Map can be used to simulate Set by assigning the value of each element with a constant value.

m := map[string]int{
"one":   1,
"two":   2,
"three": 3,
}

for k, v := range m {
  fmt.Println(k, v)
}

const t = true
set := map[string]bool{"one": t, "two": t, "three": t}

Top comments (0)