Enumerable.OfType<TResult> equivalent for List, Array and Seq modules #527

Open
sergey-tihon opened this Issue Dec 28, 2016 · 2 comments

Projects

None yet

3 participants

@sergey-tihon
sergey-tihon commented Dec 28, 2016 edited

Enumerable.OfType equivalent for List, Array and Seq modules

I propose to extend List, Array and Seq modules with equivalent to Enumerable.OfType

The existing way of approaching this problem in F# is

// Option 1
itemsArray |> Array.choose (function | :? 'a as x -> Some(x) | _ -> None)
// Option 2
itemsArray.OfType<TResult>() |> Seq.toArray

more options a mentioned on StackOverflow

Pros and Cons

The advantages of making this adjustment to F# are:

  • Improvements of code readability and succinctness
  • Avoidance of the LINQ usage.

The disadvantages of making this adjustment to F#

  • ???

Extra informtion

Estimated cost (XS, S, M, L, XL, XXL): XS

Related suggestions: (put links to related suggestions here)

Affidavit (must be submitted)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I would be willing to help implement and/or test this
  • I or my company would be willing to help crowdfund F# Software Foundation members to work on this
@slang25
slang25 commented Jan 1, 2017

I'd like to vote for ofType being defined in each module. At the moment in a lot of my projects I use this:

Module Seq = 
    let ofType<'a> (items: _ seq)= items |> Seq.choose(fun i -> match box i with | :? 'a as a -> Some a |_ -> None)

I think this makes it discoverable, and where people might expect to find it.

@ErikSchierboom
Contributor

Nice suggestion! I assume this is a relatively small feature, so could this be something that someone new to the F# compiler (like me) can pick up? If so, I'd love to give it a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment