Created
January 11, 2016 20:06
-
-
Save miklund/98ace841dd1867cfafd0 to your computer and use it in GitHub Desktop.
2012-06-19 Expand - 7 higher order functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title: Expand - 7 higher order functions | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2012/06/19/expand-7-higher-order-functions.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var employees = new List<Employee> | |
{ | |
new Employee(name : "John Smith", salary : 45000.0), | |
new Employee(name : "Carla Sewer", salary : 38000.0), | |
new Employee(name : "Sam Shipwright", salary : 55000.0) | |
}; | |
const double Raise = 1.03; | |
var table = employees.Map(e => new | |
{ | |
e.Name, | |
Salaries = e.Salary.Expand(amount => amount * Raise) | |
}); | |
var format = "{0,14} | {1,7} | {2,7} | {3,7}"; | |
Console.WriteLine(format, string.Empty, "Year 1", "Year 2", "Year 3"); | |
foreach (var line in table) | |
{ | |
var salaries = line.Salaries.Take(3).ToArray(); | |
Console.WriteLine(format, line.Name, salaries[0], salaries[1], salaries[2]); | |
} | |
// | Year 1 | Year 2 | Year 3 | |
// John Smith | 45000 | 46350 | 47740,5 | |
// Carla Sewer | 38000 | 39140 | 40314,2 | |
// Sam Shipwright | 55000 | 56650 | 58349,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<T> Expand<T>(Func<T, T> yield_fn, Predicate<T> while_fn, T init) | |
{ | |
if (yield_fn == null) | |
{ | |
throw new ArgumentNullException("yield_fn", "Supplied yield_fn argument to Expand is not allowed to be null"); | |
} | |
if (while_fn == null) | |
{ | |
throw new ArgumentNullException("while_fn", "Supplied while_fn argument to Expand is not allowed to be null"); | |
} | |
var current = init; | |
yield return current; // yield initial value | |
while (while_fn(current)) | |
{ | |
// yield each calculated new value | |
yield return current = yield_fn(current); | |
} | |
} | |
public static IEnumerable<T> Expand<T>(this T init, Func<T, T> yield_fn, Predicate<T> while_fn) | |
{ | |
return Expand(yield_fn, while_fn, init); | |
} | |
public static IEnumerable<T> Expand<T>(this T init, Func<T, T> yield_fn) | |
{ | |
return Expand(yield_fn, i => true, init); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var previous = 0; | |
var fibonacci = 1.Expand(current => | |
{ | |
var next = previous + current; | |
previous = current; | |
return next; | |
}); | |
// fibonacci : [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89..] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = 1.Expand(i => i + 1); | |
// numbers : [1, 2, 3, 4, 5..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment