F# for C# Developers by Tao Liu
Author:Tao Liu
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming / Microsoft Programming
ISBN: 9780735670211
Publisher: Microsoft Press
Published: 2013-06-11T04:00:00+00:00
Note
The underscore (_) in the second pattern match instructs the F# compiler to ignore the returned value from the active pattern.
Note
Active patterns cannot return more than seven possibilities. So the code let (|A|B|C|D|E|F|G|H|I|J|K|) x = x will generate a compile error.
Using Parameterized Active Patterns
The active pattern is a special function. If it is a function, can it accept parameters? The answer is yes. Parameters for an active pattern can be included immediately after the active pattern label, but they must appear before the active pattern return value. Example 6-60 shows how to parse a phone number and return all numbers in that phone number. The pattern string is the parameter and the out variable is used to store the return value from the active pattern.
Example 6-60. Parameterized active pattern example
open System.Text.RegularExpressions // define the parameterized active pattern let (|RegexMatch|_|) (pattern:string) (input:string) = let regex = Regex(pattern).Match(input) if regex.Success then Some(List.tail [ for x in regex.Groups -> x.Value]) else None let parsePhoneNumber str = match str with | RegexMatch "(\d{3})-(\d{3})-(\d{4})" out -> System.String.Join("", out) | RegexMatch "(\d{3})-(\d{3})(\d{4})" out -> System.String.Join("", out) | _ -> "Not supported format" // two statements below show 4251231234 parsePhoneNumber "425-123-1234" parsePhoneNumber "425-1231234" // this format does not parse and shows "Not supported format" parsePhoneNumber "(425)123-1234"
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7059)
Microservices with Go by Alexander Shuiskov(6822)
Practical Design Patterns for Java Developers by Miroslav Wengner(6739)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6680)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6085)
The Art of Crafting User Stories by The Art of Crafting User Stories(5613)
NetSuite for Consultants - Second Edition by Peter Ries(5552)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5352)
Kotlin in Action by Dmitry Jemerov(5062)
