Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created September 15, 2015 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudipto80/342a8f69c93f044fe4b7 to your computer and use it in GitHub Desktop.
Save sudipto80/342a8f69c93f044fe4b7 to your computer and use it in GitHub Desktop.
Squirrel to organize books
Table books = DataAcquisition.LoadCSV(@"C:\personal\books.csv");
//genre,author,height-genre*author
books.AddColumn("GenreAsNum", books.ValuesOf("Genre"));
books.Transform("GenreAsNum", x => x == "fiction" ? "1" : x == "nonfiction" ?
"2" : x == "philosophy" ?
"3" : x == "science" ?
"4" : x == "tech" ?
"5" :
"-1");
books = books.Transform("Author", x =>
{
if (x.Contains(',')) return x.Substring(0, x.IndexOf(','));
else if (x.Contains(' ')) return x.Substring(0, x.IndexOf(' ')); else return x;
});
books.AddColumn("Shelf", "Floor([Height]/[GenreAsNum])", 0);
int max = books.ValuesOf("Shelf").Select(t => Convert.ToInt32(t)).Max();
books.Transform("Shelf", x => Math.Ceiling((6 * Convert.ToDecimal(Convert.ToDouble(x) / Convert.ToDouble(max)))).ToString());
var shelfs = books.SortBy("Height").ThenBy("Author").ThenBy("Genre").SplitOn("Shelf");
string html = shelfs.Select( t => t.Value.Pick("Title", "Author", "Genre", "Height")
.ToHTMLTable())
.Aggregate((a,b)=> a + " <h2>New Book Shelf</h2>" + b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment