Are you a C# developer who has embraced the language’s functional concepts such as lambda expressions and LINQ, but have not yet embraced F#?
In this talk I’ll compare and contrast code written in both C# and F# that will highlight the differences between the two languages while also showing how a functional-first language like F# can be a great tool for solving problems.
16. public static class SumOfSquaresHelper
{
public static int Square(int i)
{
return i * i;
}
public static int SumOfSquares(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += Square(i);
}
return sum;
}
}
18. public static class FunctionalSumOfSquaresHelper
{
public static int SumOfSquares(int n)
{
return Enumerable.Range(1, n)
.Select(i => i * i)
.Sum();
}
}
23. public static class SumOfSquaresHelper
{
public static int Square(int i)
{
return i * i;
}
public static int SumOfSquares(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
sum += Square(i);
}
}
return sum;
}
}
24. public static class FunctionalSumOfSquaresHelper
{
public static int SumOfSquares(int n)
{
return Enumerable.Range(1, n)
.Select(i => i * i)
.Where(i => i % 2 == 0)
.Sum();
}
}
25. let square x = x * x
let isEven x = x % 2 = 0
let sumOfSquares n =
[1..n] |>
List.filter
List.map square|>
List.filter isEven |>
List.sum
sumOfSquares 100