Co je F#What is F#
F#je funkční programovací jazyk, který usnadňuje psaní správného a udržovatelného kódu.F# is a functional programming language that makes it easy to write correct and maintainable code.
F#programování primárně zahrnuje definování typů a funkcí, které jsou odvozeny a zobecněny automaticky.F# programming primarily involves defining types and functions that are type-inferred and generalized automatically. Díky tomu může být zaostření v doméně problému a manipulaci s daty namísto podrobností programování.This allows your focus to remain on the problem domain and manipulating its data, rather than the details of programming.
open System // Gets access to functionality in System namespace.
// Defines a function that takes a name and produces a greeting.
let getGreeting name =
sprintf "Hello, %s! Isn't F# great?" name
[<EntryPoint>]
let main args =
// Defines a list of names
let names = [ "Don"; "Julia"; "Xi" ]
// Prints a greeting for each name!
names
|> List.map getGreeting
|> List.iter (fun greeting -> printfn "%s" greeting)
0
F#obsahuje mnoho funkcí, včetně:F# has numerous features, including:
- Prostá syntaxeLightweight syntax
- Neměnné ve výchozím nastaveníImmutable by default
- Odvození typu a Automatická generalizaceType inference and automatic generalization
- Funkce první třídyFirst-class functions
- Výkonné datové typyPowerful data types
- Porovnávání vzorůPattern matching
- Asynchronní programováníAsync programming
V F# referenční příručce k jazykujsou popsány úplné sady funkcí.A full set of features are documented in the F# language reference.
Formátované datové typyRich data types
Datové typy, jako jsou záznamy a rozlišené sjednocení , umožňují reprezentovat složitá data a domény.Data types such as Records and Discriminated Unions let you represent complex data and domains.
// Group data with Records
type SuccessfulWithdrawal = {
Amount: decimal
Balance: decimal
}
type FailedWithdrawal = {
Amount: decimal
Balance: decimal
IsOverdraft: bool
}
// Use discriminated unions to represent data of 1 or more forms
type WithdrawalResult =
| Success of SuccessfulWithdrawal
| InsufficientFunds of FailedWithdrawal
| CardExpired of System.DateTime
| UndisclosedFailure
F#záznamy a rozlišené sjednocení jsou ve výchozím nastavení nenulové, neměnné a srovnatelné, takže je velmi snadné je používat.F# records and discriminated unions are non-null, immutable, and comparable by default, making them very easy to use.
Vynutila správnost pomocí funkcí a porovnávání vzorů.Enforced correctness with functions and pattern matching
F#funkce jsou v praxi snadno deklarované a výkonné.F# functions are easy to declare and powerful in practice. V kombinaci se porovnáváním vzorůumožňuje definovat chování, jehož správnost je vynutila kompilátorem.When combined with pattern matching, they allow you to define behavior whose correctness is enforced by the compiler.
// Returns a WithdrawalResult
let withdrawMoney amount = // Implementation elided
let handleWithdrawal amount =
let w = withdrawMoney amount
// The F# compiler enforces accounting for each case!
match w with
| Success s -> printfn "Successfully withdrew %f" s.Amount
| InsufficientFunds f -> printfn "Failed: balance is %f" f.Balance
| CardExpired d -> printfn "Failed: card expired on %O" d
| UndisclosedFailure -> printfn "Failed: unknown :("
F#funkce jsou také první třídy, což znamená, že mohou být předány jako parametry a vráceny z jiných funkcí.F# functions are also first-class, meaning they can be passed as parameters and returned from other functions.
Funkce pro definování operací s objektyFunctions to define operations on objects
F#má plnou podporu pro objekty, které jsou užitečné datové typy, pokud potřebujete Blend data a funkce.F# has full support for objects, which are useful data types when you need to blend data and functionality. F#funkce se používají k manipulaci s objekty.F# functions are used to manipulate objects.
type Set<'T when 'T: comparison>(elements: seq<'T>) =
member s.IsEmpty = // Implementation elided
member s.Contains (value) =// Implementation elided
member s.Add (value) = // Implementation elided
// ...
// Further Implementation elided
// ...
interface IEnumerable<‘T>
interface IReadOnlyCollection<‘T>
module Set =
let isEmpty (set: Set<'T>) = set.IsEmpty
let contains element (set: Set<'T>) = set.Contains(element)
let add value (set: Set<'T>) = set.Add(value)
Místo psaní kódu, který je objektově orientovaný, v F#nástroji budete často psát kód, který bude zpracovávat objekty jako jiný datový typ pro zpracování funkcí.Rather than writing code that is object-oriented, in F#, you will often write code that treats objects as another data type for functions to manipulate. Funkce, jako jsou Obecná rozhraní, výrazy objektůa rozumné použití členů , jsou běžné ve větších F# programech.Features such as generic interfaces, object expressions, and judicious use of members are common in larger F# programs.
Další krokyNext steps
Pokud se chcete dozvědět víc o větší sadě F# funkcí, podívejte se na F# prohlídku.To learn more about a larger set of F# features, check out the F# Tour.
Váš názor
Načítání názorů...