public static IEnumerable<double> ExponentialMovingAverage(this IEnumerable<double> source, double alpha) { double? last = null; return source.Select( value => { var average = last != null ? alpha*value + (1 - alpha)*last.Value : value; last = average; return average; }); }
Thursday, June 28, 2012
Calculate the exponential moving average of a stream of numbers
Thursday, February 16, 2012
Create a fast compiled method invoker using LINQ
var method = new Func<double, double, double>(Math.Pow).Method; var parameter = Expression.Parameter(typeof(object[])); var args = method.GetParameters() .Select((param, i) => Expression.Convert( Expression.ArrayIndex(parameter, Expression.Constant(i)), param.ParameterType)); var expr = Expression.Lambda<Func<object[], object>>( Expression.Convert(Expression.Call(method, args), typeof(object)), parameter); var invoker = expr.Compile(); var result = invoker(new object[] {Math.PI, 2.0});
Friday, February 10, 2012
Create a Google calendar event in an existing calendar
var credentials = new GDataCredentials(userName, password); var service = new CalendarService(null) {Credentials = credentials}; var query = new CalendarQuery("https://www.google.com/calendar/feeds/default/owncalendars/full"); var feed = service.Query(query); var eventFeedLink = feed.Entries.Cast<CalendarEntry>() .Where(item => item.Title.Text == calendarName) .SelectMany(item => item.Links) .First(link => link.Rel == GDataParserNameTable.NSGCal + "#eventFeed"); var entry = new EventEntry(title) {Times = {new When(start, end, allDay)}}; service.Insert(new Uri(eventFeedLink.AbsoluteUri), entry);Google Data API .NET client library
Thursday, February 2, 2012
Page through potentially large NHibernate criteria query results
public static IEnumerable<T> Enumerate<T>(this ICriteria criteria, int pageSize = 1000) { var entityCount = CriteriaTransformer.TransformToRowCount(criteria).UniqueResult<int>(); var pageCount = (int) Math.Ceiling(entityCount/(double) pageSize); criteria = CriteriaTransformer.Clone(criteria).SetMaxResults(pageSize); return Enumerable.Range(0, pageCount) .SelectMany(pageNum => criteria.SetFirstResult(pageNum*pageSize).List<T>()); }NHibernate
Friday, January 20, 2012
Create a YouTube playlist containing all the uploads of a given user
var credentials = new GDataCredentials(userName, password); var service = new YouTubeService(applicationName, developerKey) {Credentials = credentials}; var entry = new AtomEntry {Title = {Text = playlistName}}; entry = service.Insert(new Uri(YouTubeQuery.CreatePlaylistsUri(null)), entry); var id = ((XmlExtension) entry.FindExtension("playlistId", YouTubeNameTable.NSYouTube)).Node.InnerText; var batchUri = new Uri(string.Format("https://gdata.youtube.com/feeds/api/playlists/{0}/batch", id)); var query = new YouTubeQuery(YouTubeQuery.CreateUserUri(sourceUser)) {StartIndex = 1}; AtomFeed feed; do { feed = service.Query(query); service.Batch(feed, batchUri); query.StartIndex += feed.Entries.Count; } while (query.StartIndex < feed.TotalResults);Google Data API .NET client library
Sunday, January 8, 2012
List the users of a TFS project associated with a local workspace path
var workspace = Workstation.Current.GetLocalWorkspaceInfo(path); var tfs = new TfsTeamProjectCollection(workspace.ServerUri); var gss = tfs.GetService<IGroupSecurityService>(); var grp = gss.ReadIdentity(SearchFactor.EveryoneApplicationGroup, null, QueryMembership.Expanded); var names = gss.ReadIdentities(SearchFactor.Sid, grp.Members, QueryMembership.None) .Where(identity => identity.Type == IdentityType.WindowsUser) .Select(identity => string.Format(@"{0}\{1}", identity.Domain, identity.AccountName)) .ToArray();
Friday, January 6, 2012
Quartz scheduler job that re-triggers itself on completion
public class RetriggerJob : IInterruptableJob { private bool _interrupted; public void Execute(IJobExecutionContext context) { Thread.Sleep(1000); if (_interrupted) { return; } context.Scheduler.TriggerJob(context.JobDetail.Key); if (_interrupted) { context.Scheduler.Interrupt(context.JobDetail.Key); } } public void Interrupt() { _interrupted = true; } }Quartz.NET
Subscribe to:
Posts (Atom)