Created
January 11, 2016 19:49
-
-
Save miklund/3b103e33357610ad896d to your computer and use it in GitHub Desktop.
2012-06-16 Reduce - 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: | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2012/06/16/reduce-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
// Links | |
var links = new[] | |
{ | |
"http://litemedia.info/map-7-higher-order-functions", | |
"http://litemedia.info/fold-7-higher-order-functions", | |
"http://litemedia.info/partition-7-higher-order-functions", | |
}; | |
// Map - noop because of yield | |
var map = links.Map(OutgoingUrls); | |
// Reduce | |
var outgoingLinks = map.Reduce((links1, links2) => | |
{ | |
// Add all links2 to links1 list | |
var result = new List<string>(links1); | |
foreach (var link in links2) | |
{ | |
// Filter unique | |
if (!result.Contains(link)) | |
{ | |
result.Add(link); | |
} | |
} | |
return result; | |
}); | |
// DONE: all outgoing links are now in variable 'outgoingLinks' | |
// For url, get outgoing urls | |
private IEnumerable<string> OutgoingUrls(string url) | |
{ | |
var result = new List<string>(); | |
var request = HttpWebRequest.Create(url); | |
// Request url content | |
using (var response = request.GetResponse()) | |
using (var reader = new StreamReader(response.GetResponseStream())) | |
{ | |
// Match all href="http://..." not litemedia | |
var matches = Regex.Matches(reader.ReadToEnd(), @"href=""(?<url>(?:http\:)?//(?!litemedia).*?)"""); | |
foreach (Match match in matches) | |
{ | |
result.Add(match.Groups["url"].Value); | |
} | |
} | |
return result; | |
} |
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 T Reduce<T>(Func<T, T, T> fn, IEnumerable<T> list) | |
{ | |
if (list == null) | |
{ | |
throw new ArgumentNullException("list", "Supplied list to Reduce is not allowed to be null"); | |
} | |
if (fn == null) | |
{ | |
throw new ArgumentNullException("fn", "Supplied function to Reduce is not allowed to be null"); | |
} | |
IEnumerator<T> enumerator = list.GetEnumerator(); | |
if (!enumerator.MoveNext()) | |
{ | |
throw new ArgumentException("Can't run reduce on an empty list", "list"); | |
} | |
var result = enumerator.Current; | |
while (enumerator.MoveNext()) | |
{ | |
result = fn(result, enumerator.Current); | |
} | |
return result; | |
} | |
public static T Reduce<T>(this IEnumerable<T> list, Func<T, T, T> fn) | |
{ | |
return Reduce(fn, list); | |
} |
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 int Sum(int[] numbers) | |
{ | |
return numbers.Reduce((a, b) => a + b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment