Skip to content

Instantly share code, notes, and snippets.

@dankogai
dankogai / fetch.swift
Created June 7, 2014 20:24
fetch url and dump its content to stdout
import Foundation
if C_ARGC < 2 {
println("\(C_ARGV[0]) url")
exit(-1)
}
let url = NSURL.URLWithString(String.fromCString(C_ARGV[1]))
var enc:NSStringEncoding = NSUTF8StringEncoding
var err:NSError?
let content =
NSString.stringWithContentsOfURL(url, usedEncoding:&enc, error:&err)
@dankogai
dankogai / json.swift
Last active August 29, 2015 14:02
fetch json via URL and dumps it
import Foundation
if C_ARGC < 2 {
println("\(C_ARGV[0]) url")
exit(-1)
}
let url = NSURL.URLWithString(String.fromCString(C_ARGV[1]))
var enc:NSStringEncoding = NSUTF8StringEncoding
var err:NSError?
let content =
NSString.stringWithContentsOfURL(url, usedEncoding:&enc, error:&err)
@dankogai
dankogai / dictionary-map.swift
Last active August 29, 2015 14:02
Working version of Dictionary#map in Swift
//
// cf. http://nomothetis.svbtle.com/smashing-swift
//
extension Dictionary {
func map<R>(transform:(ValueType)->R)->Dictionary<KeyType, R> {
var result:Dictionary<KeyType, R> = [:]
for (k, v) in self {
result[k] = transform(v)
}
return result
@dankogai
dankogai / math.h.md
Last active August 29, 2015 14:02
Portable? isNan() and isFinite()
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdio.h>
#include <ctype.h>
int isNaN(double d) {
char buf[32];
sprintf(buf, "+%f", d);

Undocumented Swift

Operator Functions

Just like Haskell, operators can be used as a function.

var d = 40
d + 1      // 41
(+)(d, 2) // 42 
extension String {
static func fromUTF16Chars(utf16s:UInt16[]) -> String {
var str = ""
for var i = 0; i < utf16s.count; i++ {
let hi = Int(utf16s[i])
switch hi {
case 0xD800...0xDBFF:
let lo = Int(utf16s[++i])
let us = 0x10000
+ (hi - 0xD800)*0x400 + (lo - 0xDC00)
{print($0+$0.debugDescription+")")}("{print($0+$0.debugDescription+\")\")}(")
@dankogai
dankogai / gist:c1a9320600b9438edca6
Created January 19, 2015 02:16
sudo ddrescue -v /dev/rdisk2 intel335.dmg intel335.log
GNU ddrescue 1.19
About to copy an unknown number of Bytes from /dev/rdisk2 to intel335.dmg.
Starting positions: infile = 0 B, outfile = 0 B
Copy block size: 128 sectors Initial skip size: 128 sectors
Sector size: 512 Bytes
Press Ctrl-C to interrupt
Initial status (read from logfile)
rescued: 240057 MB, errsize: 57344 B, errors: 7
@dankogai
dankogai / gist:db5d01e3b7cee658aab0
Created July 1, 2015 10:30
Apple Music is DRM'ed

Seems like all songs downloaded via Apple Music are DRM'ed w/ Fairplay. You can see it for yourself via ffprobe available as a part of ffmpeg.

~/Music/iTunes/iTunes Media/Apple Music/${Artist}/${Album}/${Song}.m4p

  Metadata:
    […]
  Duration: 00:03:51.11, start: 0.000000, bitrate: 286 kb/s
 Stream #0:0(eng): Audio: aac (LC) (drms / 0x736D7264), 44100 Hz, stereo, fltp, 265 kb/s (default)
=head1 NAME
Plack::Loader - (auto)load PSGI/Plack Servers
=head1 SYNOPSIS
#!/usr/bin/env perl
# printenv-like app in PSGI, F?CGI, AnyEvent, Coro, POE or Danga::Socket
use strict;
use warnings;