Skip to content

Instantly share code, notes, and snippets.

Undocumented Swift

Operator Functions

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

var d = 40
d + 1      // 41
(+)(d, 2) // 42 
@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);
@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 / church.swift
Created June 15, 2014 09:44
Lambda Calculus in Swift
//
// main.swift
// church
//
// Created by Dan Kogai on 6/15/14.
// Copyright (c) 2014 Dan Kogai. All rights reserved.
//
/* Operators */
@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 / 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 / gist:11215062
Created April 23, 2014 13:24
bash-4.3.11_1 in FreeBSD ports
% cat /usr/ports/shells/bash/pkg-message
======================================================================
bash requires fdescfs(5) mounted on /dev/fd and procfs(5) mounted on
/proc.
If you have not done it yet, please do the following:
mount -t fdescfs fdesc /dev/fd
mount -t procfs proc /proc
@dankogai
dankogai / gist:5896195
Created June 30, 2013 18:04
bsfilter bsfilter-1.0.18.ruby1.9.rc5 workaround that dies with "ArgumentError: invalid byte sequence"
--- bsfilter.1.0.18rc5 Sun Mar 10 03:03:03 2013
+++ bsfilter Mon Jul 1 02:52:54 2013
@@ -933,24 +933,27 @@
if (str.force_encoding('ASCII-8BIT') =~ reg_gb18030_possible)
gb18030_possible = true
end
-
- if (str.encode('UTF-8', 'UTF-8') =~ reg_utf8)
- @options["message-fh"].printf("lang ja utf8\n") if (@options["debug"])
- return ["ja", "utf8"]
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g;
sub usage { exec 'pod2usage', $0 or die "pod2usage: $!" }
my $cmd = '/usr/bin/osascript';
die "$0 : $cmd nonexistent" unless -x $cmd;
my @paths = grep { -e $_ } map { File::Spec->rel2abs($_) } @ARGV;
@dankogai
dankogai / one-liner
Last active December 15, 2015 19:19
Perl hash iteration order
% perl -E 'system qw[perl -E], q[say %{{"a".."z"}}] for 1..100'