You can use List.choose instead of the List.filter + List.map combination:

1
2
3
4
abcList

|> List.choose (function | A -> Some(someFunc A) | _ -> None)
By on 4/4/2011 7:01 AM ()

bravo!!

By on 4/4/2011 4:59 PM ()

hai hapeens,
The filter adn apply some operation from list of union.

val length :

1
'a list -> int

Return the length (number of elements) of the given list.

val hd :

1
'a list -> 'a

Return the first element of the given list. Raise

1
Failure "hd"

if the list is empty.

val tl :

1
'a list -> 'a list

Return the given list without its first element. Raise

1
Failure "tl"

if the list is empty.

val nth :

1
'a list -> int -> 'a

Return the n-th element of the given list.
The first element (head of the list) is at position 0.
Raise

1
Failure "nth"

if the list is too short.
Raise

1
Invalid_argument "List.nth"

if n is negative.

val rev :

1
'a list -> 'a list

List reversal.

<i> <i> I have indicated <i>some<i> incompatibilities in the following text.

By on 9/29/2011 5:35 AM ()

Hi,

we can remove pattern matching code from List.map in this case.

1
2
3
4
abc list
|>List.filter (fun item->match item with |A->true|_->false)
|>List.map someFunc
List.filter (fun item->match item with |A->true|_->false)

will return only the items of A. So we don't need to do it again.

Regards,
Nyi Nyi

By on 4/2/2011 9:29 PM ()

Hi,

Thxs for your reply.

Compiler issues warning for the map function of the source like below

1
2
3
4
5
6
7
type ABC= |A of int|B|C

let abcList=[A(1);B;A(2);C]

let filtered=abcList|>List.filter (fun items->match items with |A(x)->true|_->false)

filtered|>List.map(fun (A(x))->Console.WriteLine("A:{0}",x))

but it's still valid.right?

By on 4/3/2011 12:08 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper