Tuesday, 17 June 2014

Slight Detour into Custom Sequences

Still trying to work my way around the Swift collection hierarchy with a view to working out mapping functions which work across all collection types (examples so far are Array only) - must say not the easiest thing to get your head around.

In doing my investigations however made a slight detour into understanding how it's possible to extend the collection (Sequence being the root) hierarchy and have your custom collections available to the for value in mysequence enumeration operations.

A very simple, but working, example is shown below which simply takes and array of integers and enumerates over them returning a tuple of the value and the value * 2.

I do have to say the string interpolation feature of Swift is really nice - even type checks (correctly in the main!)

No comments:

Post a Comment