40. LINQ
New delegates in System namespace
Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult>
etc.
41. LINQ
New delegates in System namespace
Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult>
etc.
System.Linq
Extension methods Where, Select, OrderBy etc.
42. LINQ
New delegates in System namespace
Action<T>, Action<T1, T2>, Func<TResult>, Func<T1, TResult>
etc.
System.Linq
Extension methods Where, Select, OrderBy etc.
Some compiler magic to translate sql style code to method calls
43. LINQ
var even = ints.Where(value => value % 2 == 0)
var greaterThan3 = ints.Where(value => value > 3)
or
var even = from value in ints
where value % 2 == 0
select value
var greaterThan3 = from value in ints
where value > 3
select value
54. Transformational Mindset
We can just pass functions around instead in most cases
- find an example where it still makes sense to use the GOF approach
though.
94. var first = “Mark”;
var middle = “Harold”;
var surname = “Needham”;
var fullname = first + “ “ + middle + “ “ + surname;
or
var names = new[] {first, middle, surname};
var fullname = String.Join(“ “, names);
95. public class SomeObject
{
public SomeObject(string p1, string p2, string p3)
{
if(p1 == null)
throw new Exception(…);
if(p2 == null)
throw new Exception(…);
if(p3 == null)
throw new Exception(…);
// rest of constructor logic
}
}
96. public class SomeObject
{
public SomeObject(string p1, string p2, string p3)
{
var parameters = new[] {p1, p2, p3};
if(parameters.Any(p => p = null)
throw new Exception(…).
// rest of constructor logic
}
}
108. public class Some<T> : Maybe<T>
{
private readonly T theThing;
public Some(T theThing)
{
this.theThing = theThing;
}
public bool HasValue ()
{
return true;
}
public T Value()
{
return theThing;
}
}
109. public class None<T> : Maybe<T>
{
public bool HasValue ()
{
return false;
}
public T Value()
{
throw new NotImplementedException();
}
}
110. public class Some
{
public static Some<T> Thing<T>(T thing)
: where T : class
{
return new Some<T>(thing);
}
}
112. public static class MaybeExtensions
{
public static Maybe<T> Maybify<T>(this T source)
where T : class
{
if(source == null)
return No.Thing<T>();
return Some.Thing(source);
}
}
recordFromDatabase.Maybify():
113. public class FooService
{
public Foo FindOrCreate(int fooId)
{
var foo = fooRepository.Find(fooId);
if(foo.HasValue())
{
return foo.Value();
}
return fooRepository.Create(fooId);
}
}
125. var people = new []
{
new Person { Id=1, Address =
new Address { Road = "Ewloe Road" }},
new Person { Id=2},
new Person { Id=3, Address =
new Address { Road = "London Road"}}
};
people.Select(p => p.Address.Road);
136. IEnumerable<Person> people = GetPeople();
foreach (var person in people)
{
Console.WriteLine(person.Name);
}
Console.WriteLine("Total number of people: " +
people.Count());
141. Total salary for a company
company.Employees
.Select(employee => employee.Salary)
.Sum()
This could lead to duplication
What if we add rules to the calculation?
Who should really have this responsibility?
.Sum()
So now we can change that function to read like this
origins of functional programming are found in lambda calculation/maths
origins of functional programming are found in lambda calculation/maths
functions that take in a function or return a function. Need to have first class functions in the language to do that. We have that with all the LINQ methods - select, where, and so on.
side effect free functions - input and output. ‘Nothing’ else should be affected
We can't achieve this idiomatically in C# because the language isn't really designed for it.
in this section?
the whole premise of functional programming with side effect free functions assumes that we have immutable data.
We can't achieve this idiomatically in C# because the language isn't really designed for it.
I want to put an example of how immutability is easy in F#, can that go in this section?
iterators in C# do this with yield keyword
It's not necessary to have lazy evaluation to be functional but it's a characteristic of some functional languages.
seems quite obvious but the most extreme guideline to follow is that we shouldn't need to store anything in variables.
Look at the data as a whole
if we don't store any intermediate values then we truly do have some data that we are passing through different filters and applying some transformation
it's quite like the pipes and filters architectural pattern in fact.
This is the way that we can combine functions on the unix command line.
what is CPS?
is where we pass in a function that represents the rest of the program which will be called with the result of another function.
A version of the maybe monad
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
what is CPS?
is where we pass in a function that represents the rest of the program which will be called with the result of another function.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
the idea is that the rest of the program is contained in the continuation so we don't need to come back to the call site.
Encapsulates the state but over complicates the program flow perhaps
what is CPS?
is where we pass in a function that represents the rest of the program which will be called with the result of another function.
Encapsulates the state but over complicates the program flow perhaps
Name the delegates if they’re used all over the place