Shopper.app week 2

I’ll just say it: I barely worked on the app this week. I’ve been feeling ill and most of my evenings were spent either watching TV or studying for the citizenship test (I’m writing this as I wait for the test to begin).

Week 2: map/reduce in Swift

I did work on it a bit. Mostly reading and trying to figure out the syntax for closures in Swift to do map/reduce.

There are many different ways to write an Array.reduce; I like the succinct syntax of Swift but I think it allows succinctness to be taken to an extreme. For example doing a reduce to add up all elements can be this:

myArray.reduce(0, {$0 + $1})

That’s something you’ll need to re-understand every time you read it mostly because of those$ signs[1]. It also feels dirty coming from ObjectiveC where everything had names. Long names. I liked that.

You can also give names to$0 and$1 which is nicer:

myArray.reduce(0, {x,y in x + y;})

I also like that because operators in Swift are just functions you can pass it like this:myArray.reduce(0,+).

So, there are many levels of succinctness depending on the amount of hair you want to lose when it’s time to read the code again after some time.

Personally, I like the last one for one-liners but for more complex transformations I’m gonna go with this syntax:

myArray.reduce(0) { x,y in
    return x + y;
}

I’ve also used named functions instead of closures just to keep code readable.

myArray.reduce(0, addUpElements);

All this was explained with more details by Silviu Pop on this article: Higher Order Functions: Map, Filter, Reduce and more – Part 1 .


Back to work

I’m behind schedule by a week and need to catch up. I’ll be alone at home next week while Mary goes to The Grace Hopper conference so I’ll probably will get more time into this. Hopefully. Unless Netflix wins again[2].

Most of the work is run as automated unit tests which is good because it has allowed me to quickly prototype and change the way I structure and massage the data.

Week 3?

Today (Oct 7) is already the middle of the week 3 so things are still piling up. Week 3 is about starting to do some UI work for basic things like shopping list creation and going shopping with it to start looking at real life data point (event if just for one user: me). I’m planning to go shopping today so that’s probably not gonna happen.

I’m sad and I’m hitting publish now.


  1. I come from a PHP background so$ in my code shouldn’t bother me but it does. Nowadays I prefer the$ in my pocket.

  2. I’ve been watching Bones, The Walking Dead and How I met your mother. I still have Dexter (no spoilers please).