Tagged: java

A simple templating engine

I wanted to talk about templating, since templating is a common thing you run into. Often times you want to cleanly do a string replace on a bunch of text, and sometimes even need minimal language processing to do what you want. For example, Java has a templating engine called Velocity, but lots of languages have libraries that do this kind of work. I thought it’d be fun to create a small templating engine from scratch with F# as an after work exercise.

The goal is to give the templating processor a set of lookup bags that can be resolved by variables. For example, if I use a variable $devshorts.isgreat that should correspond to a bag that is keyed first off of devshorts which returns a new bag, and then a new bag that has a key isgreat which should return a value.

Getting the AST

First, lets parse the … Read more

RxJava Observables and Akka actors

I was playing with both akka and rxjava and came across the following post that described how to map rxjava observables from messages posted to akka actors.

Since my team works in java, I decided to try mapping the concept to java directly, but found that there was an issue. When I tried to have multiple subscribers listen on the stream I’d get an exception since more than one subscriber would send the “subscribe” message and try to modify the akka receive context.

I also wanted to make it easier to extend the actors to be able to process a piece of work, and then resubmit it for consumption by the observable.

The subscribe command messages

First, let me show the commands we can send to the actors. This is just mapping the scala union type that the original blog post had. The @Data attribute is part of project lombokRead more

Building LINQ in Java pt 2

In my last post I discussed building a static class that worked as the fluent interface exposing different iterator sources that provide transformations. For 1:1 iterators, like take, skip, while, for, nth, first, last, windowed, etc, you just do whatever you need to do internally in the iterator by manipulating the output the stream.

But if you want to do a transformation like a map, you need to project the input source to something else. Now the base class each iterator inherits from isn’t good enough. But thankfully we can just add another generic parameter and create a new mappable base class that can handle transformations.

The following iterator handles projecting from a source to a result type and yields an enumerable iterator of the result type.

package com.devshorts.enumerable.iterators;

import java.util.function.Function;

public class MapIterator<TSource, TResult> extends EnumerableIterator<TResult> {

    private Function<TSource, TResult> projection;

     * Need this constructor for flatMap
Read more
Building LINQ in Java

Now that Java 8 has lambdas, I decided to check out what kind of lazy collection support their streams functionality had. It had some cool stuff, like

  • map
  • filter
  • flatMap
  • distinct
  • sorted
  • limit (i.e. take)
  • skip
  • reduce (i.e fold)
  • min
  • max
  • any
  • all
  • generate (for building infinite lists)

Not too bad. But, there are some problems, as far as I see it. First, you can’t extend it. With .NET they solved the lambda problem with extension methods, but Java oddly chose “defender” methods, which lets you safely update an interface declaration, but ONLY the author can do it, not any consumer. That sucks. I want other fun functions like

  • first
  • last
  • nth
  • windowed
  • intersperse
  • intercalate
  • tails
  • intersect
  • zip
  • groupRuns

And pretty much any other F#/Haskell list function. Why not? If we’re going to go with lazy evaluated lists we might as well have all the fun that comes with them. … Read more

Java lambdas

I’m not a java person. I’ve never used it in production, nor have I spent any real time with it outside of my professional work. However, when a language dawns upon lambdas I am drawn to try out their implementation. I’ve long since despised Java for the reasons of verbosity, lack of real closures or events, type erasure in generics, and an over obsession with anonymous classes, so I’ve shied away from doing anything in it.

Still, at least the Java world is trying. While I’d love to just ignore the fact that Java exists, I can’t. Lots of interesting projects are done in Java, and a lot of world class tools are written in Java, so from a professional standpoint it’d be good for me to know it.

In the past when I looked into how past Java utilities did “functional” it never felt natural to me. People have … Read more

Tech talk: Hacking droid

Todays tech talk was based off of a blog entry posted by facebook recently where they described the things they needed to do to get their mobile app running on android OS Froyo (v 2.2).

The gist of the post was that facebook migrated a lot of javascript code to Java and then found themselves in an interesting situation: they were unable to load the app due to the number of methods declared being larger than the method metadata buffer could hold. The buffer, called “LinearAlloc”, on this particular droid version was 5MB in size. Later versions were increased to 8MB which meant that the bug was no longer exhibited.

Facebook’s engineers tried to break their application up into multiple droid executable files (dex), tried automatic source transformations to minimize their method calls, tried refactoring some of their code (but were unable to make siginifant headway due to the strong … Read more