Hacker News new | ask | show | jobs
by karatinversion 2313 days ago
As I interpret it, here is a Java 7 version. With generic types A and B, and the following types defined:

  interface Adapter<S, T> {public T adapt(S s);}
  interface Statistic<T> {public int valueOf(T t);}
  interface Summary<T> {public int summarize(Statistic<T> st);}
we want to implement

  interface Jonk<A, B> {
    public Summary<B> jonk(Adapter<A, B> adapter, Summary<A> summary);
  }
So we start implementing,

  class MyJonk implements Jonk<A, B> {
    public Summary<B> jonk(Adapter<A, B> adapter, Summary<A> summary) {
      // Equivalent to the author's jonk ab aii = _
    }
  }
Looks like we'll need to create a Summary<B>, so let's do that:

  class MyJonk implements Jonk<A, B> {
    public Summary<B> jonk(Adapter<A, B> adapter, Summary<A> summary) {
      return new Summary<B>() {
        public int summarize(Statistic<B> st) {
          // Equivalent to jonk ab aii = \bi -> _
        }
      }
    }
  }
Following the article, notice that there are a lot of signatures returning int around; pick summary rather than st;

  class MyJonk implements Jonk<A, B> {
    public Summary<B> jonk(Adapter<A, B> adapter, Summary<A> summary) {
    return new Summary<B>() {
      public int summarize(Statistic<B> st) {
        return summary.summarize( /* ...? */ );
          // Equivalent to jonk ab aii = \bi -> aii $ _
        }
      }
    }
  }
Skipping some intermediate steps, we end up eventually with the solution which maps to the haskell

  // equivalent to jonk ab aii = \bi -> aii $ \a -> bi $ ab $ a
  class MyJonk implements Jonk<A, B> {
    public Summary<B> jonk(Adapter<A, B> adapter, Summary<A> summary) {
      return new Summary<B>() {
        public int summarize(Statistic<B> st) {
          return summary.summarize(new Statistic<A>() {
            public int valueOf(A a) {
              return st.valueOf(adapter.adapt(a));
            }
          });
        }
      }
    }
  }
EDIT: I'm bad at formatting.