Skip to content

Instantly share code, notes, and snippets.

View lulu-berlin's full-sized avatar
🏠

Lulu lulu-berlin

🏠
  • Berlin
View GitHub Profile
@lulu-berlin
lulu-berlin / unfoldf.fsx
Last active October 13, 2015 20:40
F# unfold without tuples
// Seq.unfold (https://msdn.microsoft.com/en-us/library/ee340363.aspx)
// works with a generator function that gets a state and returns
// Some (element, state) to generate another iterator, or
// None to stop the iteration.
// The problem is that tuples are allocated every single iteration.
//
// The solution below uses instead a generator function that gets
// a state and an iterator function. The iterator function can be called
// to yield a new element and set a new state for the next iterator. If the
// generator function returns a unit, i.e. (), the iteration stops.