Friday, May 05, 2017

Chunked Enumerable revisited (in F#)

Revisiting an old post on the subject, prompted by a recent question on the MSDN Visual F# forum.

Despite it being a very common thing to want to do, to process a stream of data in blocks, there isn't a library function for it yet; and it's not something that can be put together in a functional form using the pieces we have in the Collections.Seq module. Either, as in my earlier attempts, we have to go into object-land, with intrusions into the implementation details of enumerations (and their disposal), and eager evaluation at best; or we have to go imperative/procedural to get full lazy evaluation as per the code buried within the this C# example.

For idiomatic F#, we don't want an extension method, but a stand-alone function which we can chain into the usual pipeline of |> operations, so we would translate it thus —


where we bury the imperative/mutable kernel, the place where we count the number of steps we take (or stop early when we hit the end of the input), inside an inner function.

Monday, May 01, 2017

April Cycling

Up to 14655.9, 274.0, 142.1 = 173.2 miles, which is about the same as the monthly rate I averaged over Q1 (YTD 714 miles). Way down on last year; I didn't even keep up the 30 Days challenge, as the 4th was wet in the afternoon/early evening. When we have had good weather, I've been more inclined to work in the garden, where it's sheltered from the rather chill winds that characterised most of the month or get things done around the house; and it's less draining to start early, drive, finish early than cycle, work, cycle, work some more, cook. What rain we have had in a generally dry month also managed to skew towards driving and staying dry on days where I might normally have cycled; and these days, the chance to stop in town on the way home has reduced the need or incentive to cycle into town at weekends.