Getting every nth Element of a Sequence?

You can get the behavior by composing mapi with other functions: let everyNth n seq = seq |> Seq. Mapi (fun I el -> el, i) // Add index to element |> Seq. Filter (fun (el, i) -> I % n = n - 1) // Take every nth element |> Seq.

Map fst // Drop index from the result The solution using options and choose as suggested by Annon would use only two functions, but the body of the first one would be slightly more complicated (but the principle is essentially the same) A more efficient version using the IEnumerator object directly isn't too difficult to write: let everyNth n (input:seq) = seq { use en = input.GetEnumerator() // Call MoveNext at most 'n' times (or return false earlier) let rec nextN n = if n = 0 then true else en.MoveNext() && (nextN (n - 1)) // While we can move n elements forward... while nextN n do // Retrun each nth element yield en. Current } EDIT: The snippet is also available here: http://fssnip.net/1R.

You can get the behavior by composing mapi with other functions: let everyNth n seq = seq |> Seq. Mapi (fun I el -> el, i) // Add index to element |> Seq. Filter (fun (el, i) -> I % n = n - 1) // Take every nth element |> Seq.

Map fst // Drop index from the result The solution using options and choose as suggested by Annon would use only two functions, but the body of the first one would be slightly more complicated (but the principle is essentially the same). A more efficient version using the IEnumerator object directly isn't too difficult to write: let everyNth n (input:seq) = seq { use en = input.GetEnumerator() // Call MoveNext at most 'n' times (or return false earlier) let rec nextN n = if n = 0 then true else en.MoveNext() && (nextN (n - 1)) // While we can move n elements forward... while nextN n do // Retrun each nth element yield en. Current } EDIT: The snippet is also available here: http://fssnip.net/1R.

Don't know who was faster, you or 'Anon. ', but it's both the same suggestion, isn't it? It doesn't look horribly efficient, though, does it?

– Alexander Rautenberg Jan 13 at 3:40 It isn't horribly efficient (there are some additional function calls and indirections, because it uses 3 iterators under the cover), but it may not be too bad (there are no intermediate lists that would have to be allocated). For a more efficient version, you'll either need mutation (in sequence expression) or use the underlying IEnumerator – Tomas Petricek Jan 13 at 3:45.

Seq. Choose works nicely in these situations because it allows you do the filter work within the mapi lambda. Let everyNth n elements = elements |> Seq.

Mapi (fun I e -> if I % n = n - 1 then Some(e) else None) |> Seq. Choose id Similar to here.

Nice and elegant! – Alexander Rautenberg Jan 13 at 4:56 Yeah, I like it too! :) – Stephen Swensen Jan 13 at 5:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions