Skip to content

Instantly share code, notes, and snippets.

View stevensona's full-sized avatar

Adam Stevenson stevensona

View GitHub Profile
@stevensona
stevensona / parsexml.rb
Last active February 25, 2016 14:48
Consume RSS with Ruby
require 'rss'
require 'open-uri'
url = 'http://www.ruby-lang.org/en/feeds/news.rss'
open(url) do |rss|
feed = RSS::Parser.parse(rss)
puts "Title: #{feed.channel.title}"
feed.items.each do |item|
puts "Item: #{item.title}"
end
class Parameter
attr_accessor :value
def initialize(value, update)
@value = value
@update = update
end
def update
@value = @update.call
end
class Parameter
property value
def initialize(value, update)
@value = value
@update = update
end
def update
@value = @update.call
end
end
@stevensona
stevensona / SpriteSheet.java
Created February 7, 2016 20:49
Load and access sprites from Kenney.nl spritesheets with libgdx
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;
import java.util.HashMap;
/**
* Created by adam on 2/7/2016.
@stevensona
stevensona / podcasts.rb
Last active February 29, 2016 10:34
Get Podcast Feed from iTunes URL
require 'httparty'
require 'nokogiri'
def get_id url
#extract the id from the url
url.scan(/id(\d+)/).first.first
end
def get_page url
#download the html content of the podcast page using itunes user-agent
@stevensona
stevensona / plants.pde
Last active May 10, 2016 19:23
Leafless trees growing on a grid
int COLS = 6;
int ROWS = 6;
Tile[][] grid;
float wind = 0;
PGraphics pick_buffer;
class Tile {
float age, mass, water, food;
Tile() {
age = 0;
@stevensona
stevensona / sentiment.rb
Last active March 6, 2016 21:07
Use IBM Watson AlchemyAPI to display the sentiment of us stock market news
require 'httparty'
key = @apikey
target = 'http://feeds.reuters.com/news/usmarkets?format=html'
url = "http://gateway-a.watsonplatform.net/calls/url/URLGetTextSentiment?apikey=#{key}&url=#{target}&outputMode=json"
response = HTTParty.post(url, headers: {'Content-Type' => 'application/x-www-form-urlencoded'})
puts response.body
@stevensona
stevensona / graph.cpp
Last active April 21, 2016 11:42
Simple graph with bidirectional, constant length edges
#pragma once
#include <vector>
#include <list>
#include <queue>
#include <memory>
#include <sstream>
class Graph {
std::vector<std::list<int>> adj;
<!DOCTYPE html><html><head></head><body></body></html>
@stevensona
stevensona / benchmark.cpp
Last active May 10, 2016 19:21
c++ timing helper
#include <chrono>
#include <functional>
template<class T>
long long benchmark(std::function<void()> op) {
auto start = std::chrono::steady_clock::now();
op();
return std::chrono::duration_cast<T>(std::chrono::steady_clock::now() - start).count();
}