This file contains hidden or 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
| extension RandomAccessCollection { | |
| func sample() -> Element { | |
| let random = IndexDistance(arc4random_uniform(UInt32(self.count))) | |
| return self[index(startIndex, offsetBy: random)] | |
| } | |
| } |
This file contains hidden or 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
| extension Int { | |
| func toRoman(uppercase: Bool = true) -> String? { | |
| guard self > 0, self < 4000 else { return nil } | |
| let romanDigits = | |
| [["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"], | |
| ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"], | |
| ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"], | |
| ["", "M", "MM", "MMM"]] | |
| let romanNumber = String(self).reversed().enumerated() | |
| .map { romanDigits[$0.offset][Int(String($0.element))!] } |
This file contains hidden or 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
| let fibonacci = sequence(state: (0,1)) { (state: inout (Int,Int)) -> Int in | |
| state = (state.1, state.0 + state.1) | |
| return state.1 | |
| } |
This file contains hidden or 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
| 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, *) | |
| } | |
| } |
This file contains hidden or 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
| ## 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 |
This file contains hidden or 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
| ## 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] ); | |
| } |
This file contains hidden or 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
| sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --nointeraction |
This file contains hidden or 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
| #!/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 <<<. |
This file contains hidden or 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
| 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]*)}; |
This file contains hidden or 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
| #!/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' |