This document provides an overview of functional and multi-core programming using the F# language. It introduces Rodrigo Vidal as the presenter and covers the following topics: the functional programming paradigm, why F# and functional programming are well-suited for multi-core applications, how the F# language has evolved over time to support functional, parallel, and asynchronous programming, and examples of using parallel programming patterns in F#.
2. Quem sou eu?
Rodrigo Vidal
Twitter: @rodrigovidal
Blog: www.rodrigovidal.net
•Focado em Arquitetura de Software e Boas Práticas.
•Agilista e entusiasta de F# e IronRuby/Ruby.
•Membro ativo do grupo .NetArchitects
•Professional Scrum Developer
•Presente no maiores eventos do tecnologia do país.
5. Functional – Why now?
• Novo desafios
• Processar grandes quantidades de dados
• Escalabilidade para Cores e Clusters
• Declarative Programming
6. Why another language?
• Programação Funcional está por ai a um
longo tempo.
– Não é algo novo
– Tem uma longa história
• Programação Funcional é “safe”
– Devemos nos preocupar principalmente
quando lidamos com aplicações MultiCore e
de Cloud Computing
• Programção Funcional está crescendo!
8. Quem usa ForEach() ?
|
List<string> res = new List<string>();
foreach(Product p in Products) {
if (p.UnitPrice > 75.0M) {
res.Add(String.Format("{0} - ${1}",
p.ProductName, p.UnitPrice));
}
}
var res = from p in Products
where p.UnitPrice > 75.0M
select string.Format("{0} - ${1}",
p.ProductName, p.UnitPrice);
return res;
9. Domain Driven Design
Foco no Core Domain, revelando a
intenção do seu código.
Interfaces Reveladoras de Intenções
13. Side-Effects
– Also knowed as “The Butterfly Effect”
• A side effect
– Modifies some state
– Has observable interaction with calling functions
– Has observable interaction with the outside world
– Example: a function or method with no return value
19. What is F#?
• Functional language developed by
Microsoft Research
– By Don Syme and his team, who
productized Generics
– Based on OCaml (influenced by C# and
Haskell)
23. F# 2.0
• Succinct, Expressive, Functional
– Productive, simple, powerful, and fun language
• Parallel, Explorative, Data-rich, Algorithmic
– Extends the .NET platform to important new audiences
• Innovative, Industry/Platform-Leading
– Async, parallel, easy-objects, immutability, tuples, units-
of-measure
• Note: F# is not a replacement for C#/VB/C++
– Complementary, interoperable and augments and builds
on .NET as multi-lang platform
List&lt;string&gt; res = new List&lt;string&gt;(); #1
foreach(Product p in Products) { #2
if (p.UnitPrice &gt; 75.0M) {
res.Add(String.Format(&quot;{0} - ${1}&quot;,
p.ProductName, p.UnitPrice)); #3
}
}
var res = from p in Products
where p.UnitPrice &gt; 75.0M #1
select string.Format(&quot;{0} - ${1}&quot;,
p.ProductName, p.UnitPrice); #2
return res;
C#
Parallel.For(1,inp.Length-1,i =&gt; {
var sum = inp[i-1] +
inp[i] + inp[i+1];
res[i] = sum / 3;
});
F#
let pfor nfrom nto f =
Parallel.For(nfrom, nto + 1, Action&lt;_&gt;(f))
pfor 1 (inp.Length-2) (fun i -&gt;
let sum = inp.[i-1] +
inp.[i] + inp.[i+1]
res.[i] &lt;- sum / 3
)
The key idea behind the declarative style of programming is that the code doesn&apos;t specify
how exactly it should be executed. Execution is provided by a minimal number of primitives
such as select and where in LINQ or map and filter in F#, and these primitives can
behave in a sophisticated way.
The key idea behind the declarative style of programming is that the code doesn&apos;t specify
how exactly it should be executed. Execution is provided by a minimal number of primitives
such as select and where in LINQ or map and filter in F#, and these primitives can
behave in a sophisticated way.
The key idea behind the declarative style of programming is that the code doesn&apos;t specify
how exactly it should be executed. Execution is provided by a minimal number of primitives
such as select and where in LINQ or map and filter in F#, and these primitives can
behave in a sophisticated way.
The key idea behind the declarative style of programming is that the code doesn&apos;t specify
how exactly it should be executed. Execution is provided by a minimal number of primitives
such as select and where in LINQ or map and filter in F#, and these primitives can
behave in a sophisticated way.