Skip to content

Instantly share code, notes, and snippets.

View mmar's full-sized avatar
🐈‍⬛

Matusalem Marques mmar

🐈‍⬛
View GitHub Profile
@mmar
mmar / CompassHeading.rb
Last active August 7, 2022 03:33
Converting degrees to compass heading
class Fixnum
def degrees_to_compass
%w( N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW )[((self+11.25).modulo(360)/22.5).floor]
end
end
@mmar
mmar / gist:284968
Created January 24, 2010 02:27
Tweet a fortune
# Use .netrc for credentials. See netrc(5) manpage for format.
echo status=“`fortune -n 138 -s`” | curl -n -d @- https://api.twitter.com/1/statuses/update.json >& /dev/null
@mmar
mmar / forecast.rb
Created August 30, 2011 14:16
Tweet weather forecast
#!/usr/bin/env ruby
# Tweet weather forecast
# - gets forecast from Yahoo weather API
# - reads HTTPClient config from ~/.httpclient.yml and Twitter OAuth credentials from ~/.oauth.yml
require 'yaml'
require 'xmlsimple'
require 'httpclient'
require 'oauthclient'
@mmar
mmar / gist:1181034
Created August 30, 2011 14:34
Get thumbnail for known media sharing URLs
sub url2thumb {
my $url = shift;
return "http://flic.kr/p/img/$1_t.jpg" if $url =~ qr{^http://flic\.kr/p/([a-zA-Z0-9]*)};
return "http://yfrog.com/$1.th.jpg" if $url =~ qr{^http://yfrog\.(?:com|ru|com\.tr|it|fr|co\.il|co\.uk|com\.pl|pl|eu)/([0-9a-zA-Z]*[jpbtgfzx])};
return "http://twitpic.com/show/thumb/$1" if $url =~ qr{^http://twitpic\.com/([a-zA-Z0-9])*};
return "http://img.ly/show/thumb/$1" if $url =~ qr{^http://img\.ly/([a-zA-Z0-9]*)};
return "http://pic.im/website/thumbnail/$1" if $url =~ qr{^http://pic\.im/([a-zA-Z0-9]*)};
return "http://twitgoo.com/$1/thumb" if $url =~ qr{^http://twitgoo\.com/([a-zA-Z0-9]*)};
return "http://ctr.lv/rest/small/thumb/$1" if $url =~ qr{^http://ctr\.lv/([a-zA-Z0-9]*)};
@mmar
mmar / gist:1182301
Created August 30, 2011 22:42
Calling the Facebook Graph API using Latin
#!/usr/bin/env perl
use Lingua::Romana::Perligata;
ute UserAgent intra LWP.
ute Headers intra HTTP.
adnota >>> Change the URL below if needed <<<.
meo statio da dictum sic https://graph.facebook.com/me cis.
adnota >>> Replace <oauth2-token> with your OAuth 2.0 token <<<.
@mmar
mmar / gist:7247492
Created October 31, 2013 10:27
Create OS X Mavericks install media
sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --nointeraction
@mmar
mmar / guess_name_from_email.pl
Last active August 29, 2015 14:25
Try go guess names from email addresses
## Usage: my $name = guess_name_from_email( $email );
sub guess_name_from_email {
return join q( ), map { ucfirst lc } ( sub { ( shift, pop ) } )->( split /\./, ( split /@/, shift )[0] );
}
@mmar
mmar / guess_name_from_email.rb
Last active November 9, 2017 14:31
Try to guess names from email addresses in Ruby
## Usage: "[email protected]".guess_name_from_email
class String
def guess_name_from_email
self.split('@')[0].split('.').instance_exec { [ shift, pop ] }.compact.map { |n| n.capitalize }.join(' ')
end
end
@mmar
mmar / Factorial.swift
Last active November 9, 2017 14:10
Extend integer types with the factorial operation.
extension BinaryInteger where Self.Stride: SignedInteger {
/// Calculates the factorial of this value
/// - Precondition: the value must be positive or zero
func factorial() -> Self {
precondition(self >= 0, "Can't calculate the factorial of a negative number")
return self < 2 ? 1 : (2...self).reduce(1, *)
}
}
@mmar
mmar / Fibonacci.swift
Created November 9, 2017 14:07
The Fibonacci sequence.
let fibonacci = sequence(state: (0,1)) { (state: inout (Int,Int)) -> Int in
state = (state.1, state.0 + state.1)
return state.1
}