Nevoic
10/7/2019 - 9:45 AM

Transformation of List Comparison

  • Create an immutable final list of names ["potato", "muffin", "oofsted", "alamony", "ruff"]
  • Transform those names into an immutable final list of lengths.
  • Enforce immutability/finality at compile time if it's a statically typed language

Make sure to list of the things the language cannot do if it fails to meet the requirements

The languages are sorted by character count (most -> least)

immutable

The values cannot be modified after initialization

final

The reference cannot be modified after declaration

Lengths

Language        | Char # | Line # | Passed (if false see code for details)

Java_6          |  427   |   15   | False

Java_10         |  173   |    2   | False

C               |  153   |    5   | False

C++             |  153   |    5   | False

Typescript      |  145   |    2   | True

Perl            |  111   |    2   | False

Kotlin          |  106   |    2   | True

Scala           |  100   |    2   | True

Swift           |   99   |    2   | True

Clojure         |   94   |    2   | True

Python          |   91   |    2   | False

Ruby            |   90   |    2   | False

Haskell         |   85   |    2   | True
final List<String> _names = new ArrayList<String>();
_names.add("potato");
_names.add("muffin");
_names.add("oofsted");
_names.add("alamony");
_names.add("ruff");

final List<String> names = Collections.unmodifiableList(_names);

final List<Integer> _lengths = new ArrayList<Integer>();

for (final String name : names)
    _lengths.add(name.length());

final List<Integer> lengths = Collections.unmodifiableList(_lengths);

// Unmodifiable/Immutable Java lists only enforce immutability at runtime (i.e they throw an exception on `add`/`remove` etc.)
final var names = List.of("potato", "muffin", "oofsted", "alamony", "ruff");
final var lengths = names.stream().map(String::length).collect(Collectors.toUnmodifiableList());

// Unmodifiable/Immutable Java lists only enforce immutability at runtime (i.e they throw an exception on `add`/`remove` etc.)
const char names[][8] = {"potato", "muffin", "oofsted", "alamony", "ruff"};
int lengths[5];

for(int i = 0; i < 5; i++)
  lengths[i] = strlen(names[i]);

// Indexes are not finalized (can't copy to a const)
const string names[] {"potato", "muffin", "oofsted", "alamony", "ruff"};
int lengths[5];

for(int i = 0; i < 5; i++)
    lengths[i] = names[i].length();

// Indexes are not finalized (can't copy to a const)
// I made an alternative C++ one that did have final indexes, but there wasn't a way to cap/set the size (it was with vectors). It was also ~ 50% longer
const names: readonly string[] = ["potato", "muffin", "oofsted", "alamony", "ruff"];
const lengths: readonly number[] = names.map(n => n.length);
use constant NAMES => qw(potato muffin oofsted alamony ruff);
use constant LENGTHS => map { length $_ } NAMES;

# Cannot make final variable bindings
val names = listOf("potato", "muffin", "oofsted", "alamony", "ruff")
val lengths = names.map { it.length }
val names = List("potato", "muffin", "oofsted", "alamony", "ruff")
val lengths = names.map(_.length)
let names = ["potato", "muffin", "oofsted", "alamony", "ruff"]
let lengths = names.map { $0.count }
(def names ["potato", "muffin", "oofsted", "alamony", "ruff"])
(def lengths (map count names))
names = ("potato", "muffin", "oofsted", "alamony", "ruff")
lengths = tuple(map(len, names))

# Cannot make final variable bindings
# Cannot make immutable lists
# Using tuples instead of lists. They're immutable, but they're heterogeneous instead of homogeneous.
names = %w(potato muffin oofsted alamony ruff).freeze
lengths = names.map(&:length).freeze

# Cannot make final variable bindings
names = ["potato", "muffin", "oofsted", "alamony", "ruff"]
lengths = map length names