Code Explainers

Code explainers tagged #functional

rust
use std::num::ParseIntError;
 
fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> {
    line.split(',')

Collecting Results in Rust iterators

iterators error-handling result
Intermediate 7 steps
java
public List<String> collectTagsFromArticles(List<Article> articles) {
    return articles.stream()
        .flatMap(article -> article.getTags().stream())
        .map(String::trim)

Flattening nested data with Java streams

streams flatmap collectors
Intermediate 7 steps
ruby
def collatz_lengths
  Enumerator.new do |y|
    n = 1
    loop do

Building infinite sequences with lazy enumerators

lazy-evaluation enumerators infinite-sequences
Intermediate 8 steps
typescript
type Ok<T> = { ok: true; value: T };
type Err<E> = { ok: false; error: E };
export type Result<T, E> = Ok<T> | Err<E>;
 

A Result type for typed error handling

discriminated-union error-handling type-guards
Intermediate 8 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) {
    return orders.stream()
            .collect(Collectors.groupingBy(Order::getCustomerId));
}

Grouping streams with Java Collectors

streams grouping collectors
Intermediate 5 steps