0
×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 10 minutes functional programming

1,109

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,109
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
8
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 2. Programação funcional em C# (10 minutos) Elemar Jr.Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 3. Apresentando Elemar JR 33 anos, pai, DEV e nerd Arquiteto, enxadrista, (ex) apaixonado por vinhos. Gosta de filosofia e teologia P&D na Promob onde trabalha há 14 anos Microsoft C# MVP janeiro 2012 Integrante do Void Podcast com Leandro Daniel [@leandronet] e Vinícius Quaiato [@vquaiato] Blogueiro e articulista elemarjr.net e www.infoq.com/br/author/Elemar-Jr. FOSS developer fluentil.org + github.com/elemarjr + elemarjr.github.comTwitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 11. Func<int, int, int> add = (a, b) => a + b; Func<int, int> inc = a => add(a, 1); Func<int, int> @double = x => x + x; Func<int, int> quadruple = x => @double(@double(x)); http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 13. public static IEnumerable<int> Infinite() { var result = 0; while (true) yield return result++; } http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 14. public static IEnumerable<int> Infinite() { var result = 0; while (true) yield return result++; } http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 15. from x in Enumerable.Range(1,3) from y in Enumerable.Range(4,5) select new { x, y}; Enumerable.Range(1, 3).SelectMany(x => Enumerable.Range(4, 5), (x, y) => new {x, y}) http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 16. public static int Factorial(int n) { if (n == 0) return 1; return Factorial(n - 1)*Factorial(n); } public static int Length(this IEnumerable source) { if (!source.GetEnumerator().MoveNext()) return 0; return 1 + source.Skip(1).Length(); } http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 18. public static class FunctionalProgramming { public static Func<T, T> Twice<T>(this Func<T, T> func) { return Combine(func, func); } public static Func<TC, TB> Combine<TA, TB, TC>(this Func<TA, TB> fa, Func<TC, TA> fb) { return arg => fa(fb(arg)); } } http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 19. Func<string, string> print = s => { Console.WriteLine(s); return s; }; print.Twice()("Hello World"); Func<bool, bool> not = a => !a; Func<int, bool> even = x => x % 2 == 0; var noteven = FunctionalProgramming.Combine(not, even); Func<int, int> twoTimes = x => x*2; var fourTimes = twoTimes.Twice(); http://elemarjr.net/tag/haskell101Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 20. public void Run() { Console.WriteLine("Starting..."); var f30 = Fibonacci(30); var f35 = Fibonacci(35); var f40 = Fibonacci(40); var f45 = Fibonacci(45); Console.WriteLine("Results: {0}, {1}, {2}, {3}", f30, f35, f40, f45); Console.WriteLine("Done!"); } public int Fibonacci(int n) { if (n == 0) return 0; if (n == 1) return 1; return Fibonacci(n-1) + Fibonacci(n - 2); } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 21. public void Run2() { Func<int, int> fibonacci = Fibonacci; Console.WriteLine("Starting..."); var f30 = fibonacci(30); var f35 = fibonacci(35); var f40 = fibonacci(40); var f45 = fibonacci(45); Console.WriteLine("Results: {0}, {1}, {2}, {3}", f30, f35, f40, f45); Console.WriteLine("Done!"); } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 22. public void Run() { Func<int, int> fibonacci = Fibonacci; Console.WriteLine("Starting..."); var f30 = fibonacci(30); Console.WriteLine("Input: {0} Result: {1}", 30, f30); var f35 = fibonacci(35); Console.WriteLine("Input: {0} Result: {1}", 35, f35); var f40 = fibonacci(40); Console.WriteLine("Input: {0} Result: {1}", 40, f40); var f45 = fibonacci(45); Console.WriteLine("Input: {0} Result: {1}", 45, f45); Console.WriteLine("Results: {0}, {1}, {2}, {3}", f30, f35, f40, f45); Console.WriteLine("Done!"); } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 23. public static class Combinators { public static Func<T, TResult> Print<T, TResult>(Func<T, TResult> func) { return (input) => { var result = func(input); Console.WriteLine("Input: {0} Result: {1}", input, result); return result; }; } } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 24. public void Run3() { Func<int, int> fibonacci = Combinators.Print<int, int>(Fibonacci); Console.WriteLine("Starting..."); var f30 = fibonacci(30); var f35 = fibonacci(35); var f40 = fibonacci(40); var f45 = fibonacci(45); Console.WriteLine("Results: {0}, {1}, {2}, {3}", f30, f35, f40, f45); Console.WriteLine("Done!"); } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 25. public static class Combinators { public static Func<T, TResult> Time<T, TResult>(Func<T, TResult> func) { return (input) => { var before = DateTime.Now; var result = func(input); Console.WriteLine("Time to this input: {0}", DateTime.Now - before); return result; }; } } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]
• 26. public void Run4() { Func<int, int> fibonacci = Combinators.Time<int, int>( Combinators.Print<int, int>(Fibonacci) ); Console.WriteLine("Starting..."); var f30 = fibonacci(30); var f35 = fibonacci(35); var f40 = fibonacci(40); var f45 = fibonacci(45); Console.WriteLine("Results: {0}, {1}, {2}, {3}", f30, f35, f40, f45); Console.WriteLine("Done!"); } http://elemarjr.net/tag/functional-programming/Twitter [@elemarjr] | Facebook [facebook.com/elemarjr] | Blog [elemarjr.net]