Skip to content

Instantly share code, notes, and snippets.

View rupertbates's full-sized avatar

Rupert Bates rupertbates

  • Guardian News and Media
  • London
View GitHub Profile
@rupertbates
rupertbates / DataFrame.R
Created June 10, 2014 21:20
Select from R data frame
good[(good$Ozone > 31) & (good$Temp > 90), ]
@rupertbates
rupertbates / getContributorCountsFunctional.java
Last active August 29, 2015 14:10
getContributorCounts method in a functional style
public List<ContributorCount> getContributorCounts(){
List<ArticleView> articleViews = getArticleViews();
return articleViews
.map(a -> a.contributor) //Get the contributor names from the article views
.sort(Ord.stringOrd) //Need to sort before grouping
.group(Equal.stringEqual) //Group by contributor name
.filter(l -> l.length() > 1) //remove any contributors who only appear once
.map(l -> new ContributorCount(l.head(), l.length())); //Map to the return type
}
@rupertbates
rupertbates / getContributorCountsImperative.java
Last active August 29, 2015 14:10
getContributorCounts method in an imperative style
public List<ContributorCount> getContributorCounts() {
List<ArticleView> articleViews = getArticleViews();
//First iterate over the articles counting the occurrences of
//each contributor
HashMap<String, Integer> contributorCounts = new HashMap<String, Integer>();
for (ArticleView articleView : articleViews) {
Integer count = contributorCounts.get(articleView.contributor);
if (count == null)
contributorCounts.put(articleView.contributor, 1);
@rupertbates
rupertbates / ArticleWithImageImperative.java
Created November 28, 2014 14:10
ArticleWithDisplayImage
public class Article {
DisplayImage mainImage;
//Rest of the class declaration
public DisplayImage getMainImage(){
//What happens if there is no main image on this article?
return mainImage;
}
//Imperative style of dealing with an Option type
if(article.getMainImage().isSome())
setArticleImage(article.getMainImage().some());
//Functional approach using map
article.getMainImage().map(this::setArticleImage);
//Checking for null through an object graph quickly becomes unwieldy
if (article.getContributor() != null &&
article.getContributor().getImage() != null &&
article.getContributor().getImage().getSize() != null) {
Size size = article.getContributor().getImage().getSize();
setContributorImageSize(size.getWidth(), size.getHeight());
}
//Chaining multiple optional values using bind and map
findViewById(R.id.myView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick();
}
});
@rupertbates
rupertbates / remove-files.sh
Created December 16, 2014 15:56
Remove any files from directory 1 which are in directory 2. Eg. ./remove-files ~/my-first-dir ~/my-second-dir
#!/bin/bash
for i in $( ls $1 ); do
echo Removing: $2$i
rm $2$i
done
@rupertbates
rupertbates / gist:77eee1da4d63ff896d55
Created February 3, 2015 17:59
JQ starRating query
[.[]|select(.fields.starRating == "4")]
@rupertbates
rupertbates / music-reviews.json
Last active August 29, 2015 14:14
Music reviews with star rating
{
"response": {
"status": "ok",
"userTier": "developer",
"total": 15895,
"startIndex": 1,
"pageSize": 10,
"currentPage": 1,
"pages": 1590,
"orderBy": "newest",