Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created June 27, 2017 10:21
Show Gist options
  • Save lkaczanowski/71b9cfc777733eb45a075e608bd28384 to your computer and use it in GitHub Desktop.
Save lkaczanowski/71b9cfc777733eb45a075e608bd28384 to your computer and use it in GitHub Desktop.
Calculator for paging
using System.Collections.Generic;
using System.Linq;
namespace PageCalculator
{
public class Page
{
public Page(int skip, int take)
{
Skip = skip;
Take = take;
}
public int Skip { get; }
public int Take { get; }
}
public interface IPageCalculator
{
IEnumerable<Page> GetPages(int totalCount, int startIndex, int take);
}
public class PageCalculator : IPageCalculator
{
public IEnumerable<Page> GetPages(int totalCount, int startIndex, int take)
{
List<Page> GetPagesR(int startIndexR, List<Page> acc)
{
var newStartIndex = startIndexR + take;
if (newStartIndex >= totalCount)
{
acc.Add(new Page(startIndexR, totalCount - startIndexR));
return acc;
}
acc.Add(new Page(startIndexR, take));
return GetPagesR(newStartIndex, acc);
}
return startIndex >= totalCount ? Enumerable.Empty<Page>() : GetPagesR(startIndex, Enumerable.Empty<Page>().ToList());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment