Itertools for F #

I got used to Python itertools for performing functional tasks using iterators (F #: sequence) and wondered if equivalents exist in F # or a commonly used library, since they are so convenient.

Best tools for me:

  • product: Cartesian product equivalent to a nested cycle
  • combinations
  • Permutations
  • TakeWhile
  • dropwhile chain
  • : merging multiple iterators into a new longer iterator
  • repeat *: repeat (5) β†’ 5, 5, 5 ...
  • count *: count (10) -> 10, 11, 12 ...
  • cycle *: cycle ([1,2,3]) β†’ 1,2,3,1,2 ...

* I believe that these 3 will give monads in F #? How do you make them endless?

They will tell me because I saw this question about permutations in F # and was surprised that it was not part of the library or built into the language.

+4
source share
1 answer

I don’t know if there is a commonly used library that contains functions such as a product, combinations and permutations, but the others you mentioned are already in the Seq and List modules or can be implemented without any problems, and there are also useful methods in System.Linq.Enumerable .

  • takewhile β†’ Seq.takeWhile
  • dropwhile β†’ Seq.skipWhile
  • chain β†’ Seq.concat
  • repeat β†’ Seq.initInfinite
  • count(10) β†’ Seq.initInfinite ((+) 10)
  • cycle([1, 2, 3]) β†’ Seq.concat <| Seq.initInfinite (fun _ -> [1; 2; 3]) Seq.concat <| Seq.initInfinite (fun _ -> [1; 2; 3])

You can also check out the excellent FSharpx library - it contains many useful functions for working with collections and something else.

+4
source

All Articles