@SuppressWarnings("unchecked")
    public <T, K extends Node> List<T> getElementsAs(Function<K, T> f) {
        List<T> result = new ArrayList<>(elements.size());
        for (int i = 0; i < elements.size(); i++) {
            try {
                K k = (K) elements.get(i);
                result.add(f.apply(k));
            } catch (ClassCastException e) {
                // Attempt to present a more useful error message by parsing the class
                // that was trying to be converted. The only reason this would ever
                // fail is if the message is changed in the JDK, so just in case,
                // there's a fallback code-path.
                String message = e.getMessage();
                Matcher matcher = CAST_PATTERN_TYPE.matcher(message);
                String formatted = matcher.matches()
                        ? String.format("Expected array element %d to be a %s but found %s.",
                                i,
                                nodeClassToSimpleTypeName(matcher.group(1)),
                                nodeClassToSimpleTypeName(elements.get(i).getClass().getSimpleName()))
                        : String.format("Array element at position %d is an invalid type `%s`: %s",
                                i,
                                nodeClassToSimpleTypeName(elements.get(i).getClass().getSimpleName()),
                                e.getMessage());
                throw new ExpectationNotMetException(formatted, elements.get(i));
            }
        }

        return result;
    }
Gets the elements of the ArrayNode as a specific type by applying
a mapping function to each node.

<p>Each Node is cast to {@code K} and then mapped with the provided
function to return type {@code T}.

<pre>{@code
ArrayNode array = Node.fromStrings("foo", "baz", "bar");
List<String> strings = array.getElementsAs(StringNode::getValue);
}</pre>

@param f Mapping function that takes {@code K} and return {@code T}.
@param <K> Expected Node type.
@param <T> Mapping function return value.

@return Returns the List of type T.
@throws ExpectationNotMetException if the list contains elements of a different type than {@code K}.
smithy-model/src/main/java/software/amazon/smithy/model/node/ArrayNode.java