Created
June 27, 2017 10:21
-
-
Save lkaczanowski/71b9cfc777733eb45a075e608bd28384 to your computer and use it in GitHub Desktop.
Calculator for paging
This file contains 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
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