# Learning to use foldl and map

I have been working on a small line drawing project, and part of that project is indexing nested lists based on sets of indices.

Firstly I wrote a function to return the recursive nth item. So for this data `[[[1,2],[3,4]],[[5,6],[7,8]]]` and the input `[1,2,1]` the value `3` is returned. Here’s my original function:

``````point(Cube,[First|Rest]) ->
point(lists:nth(First,Cube),Rest);
point(Cube,[]) ->
Cube.  ``````

I then had a couple of functions to return point lists for each list of points look-ups. The first line was for connected points, and the second lines to draw separate lines. So using the same data above, and the input `[[1,2,1],[2,2,2],[1,1,1]]` to line would generate `[3,8,1]`, and lists of that input to lines creates lists of results. Brilliant I know, here’s my code:

``````lines(Cube,Lines) ->
lines(Cube,lists:reverse(Lines),[]).

lines(_Cube,[], Output) ->
Output;
lines(Cube,[First|Rest], Output) ->
lines(Cube,Rest,[line(Cube,First)|Output]).

line(Cube,Points) ->
line(Cube,lists:reverse(Points),[]).
line(Cube,[First|Rest],Output) ->
line(Cube,Rest,[point(Cube,First)|Output]);

line(_Cube,[],Output) ->
Output.  ``````

I used a wrapper function to reverse the input, to solve the list construction order. I was quite proud of my groking tail recursion. But couldn’t help notice line and lines are the same, but not the same. While reading how to do the output formatting, I came across some good examples on using foldl and map. Surprise, the examples where like my functions, so I rewrote them like this (renaming point to nth_nth):

``````lines(Cube,Lines) ->
lists:map(fun(Line) -> line(Cube, Line) end, Lines).

line(Cube,Points) ->
lists:map(fun(Point) -> nth_nth(Cube, Point) end, Points).

nth_nth(Vals,Nths) ->
lists:foldl(fun(Nth,List) -> lists:nth(Nth,List) end, Vals, Nths).  ``````

Much better, but I started to have a nagging feeling about using anonymous functions to just call a single function. I then found the way to refer to other functions by name, but this does not work for the map as I am binding an extra variable, but for foldl it does work as it’s arity is 2, thus nth_nth can be written as:

``````nth_nth(Vals,Nths) ->
lists:foldl({list,nth}, Vals, Nths).  ``````

I thought the use of the fold function to return nested look-up results was clever. The new functions have less total lines, but each line is much denser, so may appear harder to read, but I am using standard functions so the cost to comprehend should be lower.