[Swift-user] last element array indexing?

Ben Clifford benc at hawaga.org.uk
Mon Mar 22 06:32:12 CDT 2010


If you have map and fold, my beloved functional operators, you get to say 
(using haskell-like syntax, because swiftscript has no lambda or 
operators-as-functions).


 how many elements in a? = fold (+) (map (\x->1)) a

 whats the last element in a? = fold (\(l,r) -> r) a
 (though that doesn't return an index or anything lvalue like, so you can 
  only use this to *read* the last element in a, not write some last 
  value)

That first one is quite like the way counting is done in google 
map-reduce.

Actual working haskell code:

foldl1 (+) (map (\x->1) [543,213,765])
3

foldl1 (\l->(\r->r)) [1,2,3,4,5]
5


-- 
http://www.hawaga.org.uk/ben/




More information about the Swift-user mailing list