


(apple -> banana).let z = 1
let add = x + y // int -> int ->int
(apple -> banana), (banana -> cherry), (apple -> cherry). , – .httpRequest -> httpResponse. , , .
(Composite) «», , .
, . int – . . Customer – . . int -> int – . «» — .type Birthday = Person * Datetype PaymentMethod =
| Cash
| Cheque of ChequeNumber
| Card of CardType * CardNumber«» .
Entity Framework , id.

int -> int ! 0, . NonZeroInteger -> int int -> int option.


let printList anAction aList =
for i in aList do
anAction ipublic static int Product(int n)
{
int product = 1; //
for (int i = 1; i <= n; i++) //
{
product *= i; //
}
return product; //
}
public static int Sum(int n)
{
int sum = 0; //
for (int i = 1; i <= n; i++) //
{
sum += i;
}
return sum; //
} let product n =
let initialValue = 1
let action productSoFar x = productSoFar * x
[1..n] |> List.fold action initialValue
let sum n =
let initialValue = 0
let action sumSoFar x = sumSoFar+x
[1..n] |> List.fold action initialValueAggregate, ! , LINQ :)C#. «» SelectManyinterface IBunchOfStuff
{
int DoSomething(int x);
string DoSomethingElse(int x); // -
void DoAThirdThing(string x); //
}
interface IBunchOfStuff
{
int DoSomething(int x);
}
int -> int. F# , , « » . «» :let DoSomethingWithStuff strategy x =
strategy x
let isEvenWithLogging = log >> isEven >> log // int -> bool. .

int -> int -> int . , int, int , int -> int. n, n — . ., . , 1920- , — , .
let three = 1 + 2
let three = (+) 1 2
let three = ((+) 1) 2
let add1 = (+) 1
let three = add1 2 //
let getCustomerFromDatabase connection (customerId:CustomerId) =
from connection
select customer
where customerId = customerId
//
let getCustomer1 = getCustomerFromDatabase myConnection int Divide(int top, int bottom)
{
if (bottom == 0)
{
// , ?
throw new InvalidOperationException("div by 0");
}
else
{
return top/bottom;
}
}
void Divide(int top, int bottom, Action ifZero, Action<int> ifSuccess)
{
if (bottom == 0)
{
ifZero();
}
else
{
ifSuccess( top/bottom );
}
}

let ifSomeDo f opt =
if opt.IsSome then
f opt.Value
else
Nonelet example input =
doSomething input
|> ifSomeDo doSomethingElse
|> ifSomeDo doAThirdThing
|> ifSomeDo (fun z -> Some z)


bind
let bind nextFunction optionInput =
match optionInput with
//
| Some s -> nextFunction s
// None
| None -> None
bind//
let example input =
let x = doSomething input
if x.IsSome then
let y = doSomethingElse (x.Value)
if y.IsSome then
let z = doAThirdThing (y.Value)
if z.IsSome then
let result = z.Value
Some result
else
None
else
None
else
None
//
let bind f opt =
match opt with
| Some v -> f v
| None -> None
let example input =
doSomething input
|> bind doSomethingElse
|> bind doAThirdThing
|> bind (fun z -> Some z)
, Either,string UpdateCustomerWithErrorHandling()
{
var request = receiveRequest();
validateRequest(request);
canonicalizeEmail(request);
db.updateDbFromRequest(request);
smtpServer.sendEmail(request.Email)
return "OK";
} string UpdateCustomerWithErrorHandling()
{
var request = receiveRequest();
var isValidated = validateRequest(request);
if (!isValidated)
{
return "Request is not valid"
}
canonicalizeEmail(request);
try
{
var result = db.updateDbFromRequest(request);
if (!result)
{
return "Customer record not found"
}
}
catch
{
return "DB error: Customer record not updated"
}
if (!smtpServer.sendEmail(request.Email))
{
log.Error "Customer email not sent"
}
return "OK";
} bind . , F#:

. «, »

. .
1 * 2 * 3 * 4
[ 1; 2; 3; 4 ] |> List.reduce (*)1 + 2 + 3 + 4. 1 + 2 , 3 + 4 — , . — .reduce : ? , ? , ., . , , .

— «» ( ). , , ., Event Sourcing — . Flux unidirectional data flow, .

, — GOF . «» — .
Source: https://habr.com/ru/post/337880/
All Articles