Higher Order Programming

Updated

Reading time ~4 minutes

Array- and Set-based Coding Techniques

An Anti-Pattern?

This is an exploration of advantages gained when you code everything as an array. (Using Jedi concepts from SmallTalk)

Here’s some guiding principles:

  1. All input is array-like. Even if an array of 1.
  2. Higher level functions should generally accept AND return arrays. (Except for callback methods for loops: map/reduce/each/filter)
  3. 99 out of 100 devs code suffers from what I call acute schema surplusage syndrome.
  4. Beware bloated class-backed models - with all the predictable trappings: fragile instance state w/ so many levers and knobs to mess with, DB transactions, sql locks, async/mutexing (that always works first time), using idiomatic property getter/setters, and your public/private/final/etc usage is solid, right?

  5. So let me take a common problem and shoehorn ~add~ some set-based musings.
  6. A hypothetical Blog Site has lots of Articles, and has even more Posts (Comments).
  7. Let’s add a delete method (below) - but with support for both singular OR arrays.
package net.danlevy.why.java___why.you.got.all.the.dots____it.must.be.all.the.factories;

public class Post {
  public String   title;
  public Date     created;
  public String   message;

  public Post(String title, String message) {
    this.title    = title;
    this.message  = message;
    this.created  = new Date();
  }

  public Date isArchived() {
    return this.created < new Date(2015, 0, 1);
  }

  // Post.delete` can be called w/ a singular Post or an array of Post[]
  public static int delete(Post post) {
    Post<List> posts = new Post<List>(post);
    return delete(posts)
  }

  public static int delete(Post<List> posts) {
    return posts.map(DB.cleanupPost);
  }
}



Forgive me if my Java is a little rusty.

Functional Promises in JavaScript Crash Course

# Functional Promises in JavaScript Crash Course> See the companion project to this article: [`escape-from-callback-mountain`](https://gi...… Continue reading