Skip to content

Instantly share code, notes, and snippets.

View vector623's full-sized avatar

David Gallmeier vector623

View GitHub Profile
@vector623
vector623 / AggregateUntilUsage.cs
Last active February 10, 2018 02:26
AggregateUntil() example
public static List<Label> GetLabels()
{
/*
* use linq's lazy loading feature to enumerate an unlimited number of page requests. so i created AggregateUntil() to
* take in a Func() which will inform when to stop iterating - in this case, we want to stop after the entries count
* exceeds the totalNumEntries value, which Google Adwords returns from the first query
*/
var labels = Enumerable
.Range(0, Int32.MaxValue) //range generates a list of integers, from 1 to practically infinity
.AggregateUntil(
@vector623
vector623 / RetryWebRequests.cs
Last active March 6, 2018 13:53
retry web requests, with exponential backoff, using functional linq in c#
var labelsPage = Enumerable
.Range(0, 5)
.Select(pageAttempt =>
{
Thread.Sleep(2500 * pageAttempt * pageAttempt);
try
{
var labelsAwql = "select LabelId,LabelName,LabelStatus limit 0,100";
var attemptedlabelsPage = Config.LabelService.query(labelsAwql);
var page = require('webpage').create();
page.open('http://davidgallmeier.com', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
var results = page.evaluate(function() {
var firstlinktext = $('a.post-link:first').
text().
toString();
var firstlinkhref = $('a.post-link').
map(function () {
return $( this ).attr('href');
require 'net/http'
require 'nokogiri'
#you need Nokogiri for this to work: gem install 'nokogiri'
#fetch html if not cached
html_cache_file = 'blackjack-chart.html'
if File.exist? html_cache_file
html = File.read(html_cache_file)
else