Archive for August 23, 2014

Saturday, August 23, 2014

Swift Function Currying

Russ Bishop:

Now forget all that stuff we just discussed because Swift supports automatic function currying. Just separate the parameters inside their own parens and you’re done:

func takeFive<T:IntegerArithmeticType>(a:T)(b:T)(c:T)(d:T)(e:T) -> T {
    return a + b + c + d + e
}

[…]

A caller might ask us for a function that runs queries against a certain database. We can use a curried function (possibly private) that takes connection info as its first parameter, followed by query information as its second; the caller gets back a curried function that takes only the query parameters. The caller can then use this returned function without ever being aware of the details. We don’t have to create queue or thread specific ambient context information to track some sort of “current” connection. The storage lifetime of the connection information is tied to the lifetime of the closure’s environment. When the closure goes away, so too will the saved context. This also lets us have multiple active connections or pass them between queues and threads. In short, we can hide the details without jumping through any hoops or writing boilerplate objects just to carry some state data around.

Better Bash Scripting

Robert Muth:

I start every bash script with the following prolog:

#!/bin/bash
set -o nounset
set -o errexit

This will take care of two very common errors:

  1. Referencing undefined variables (which default to “”)
  2. Ignoring failing commands

[…]

Bash has a number of (underappreciated) ways to manipulate strings.

[…]

Some commands expect filenames as parameters so straightforward pipelining does not work. This is where <() operator comes in handy as it takes a command and transforms it into something which can be used as a filename.

Amazon vs. Hachette

Glenn Fleishman:

In May 2014, presumably to increase negotiating pressure on Hachette, Amazon removed pre-order buttons on Hachette books, eliminated discounts on Hachette books, and delayed shipments of Hachette books to purchasers. Amazon did the same with Warner Brothers DVDs around the same time for several weeks, and a few days ago pulled pre-orders for physical versions of Disney movies. Amazon is also engaged in a similar action against Bonnier Media in Germany, where antitrust laws are stronger and more strictly enforced.

[…]

The precise details of why Amazon and Hachette are at loggerheads remain unknown, although it appears the primary disagreement — at least according to insiders and public statements by both companies — is that Amazon wants to price all ebooks at no more than $9.99 while still making a profit from them, and Hachette is resisting. Under the antitrust consent degree to which Hachette agreed, Amazon was free to set retail prices for two years. That period is over, and Hachette now wants to set the retail price for ebooks, offering Amazon a percentage of the retail price. In essence, Hachette is looking to use the agency model, which gives pricing control to the publisher, whereas Amazon wants to stick with the wholesale model, in which resellers pay a fixed wholesale price and set whatever retail price they want. In many cases, the agency model provides a higher profit to a bookseller.

Christopher Wright (via John Gruber):

Amazon has pointed out that charging huge prices for ebooks—prices that, cost-wise, make them indistinguishable from paperbacks—is stupid. My apologies to all the publishers and writers who will be offended by this, but Amazon is right. They are absolutely making self-serving statements, but those self-serving statements are, from the perspective of a reader who likes buying books, and prefers buying more books with the same amount of money, 100% indisputably true. Their vision advances the market in a way that is better for customers. They have a position that has a solution.

Hachette’s position is “Amazon is trying to control the market.” Well, I actually agree with that position. But they haven’t offered a solution to that argument. They have no vision on how to create a market that effectively competes with Amazon that answers any of the claims Amazon has made about how their vision is better for consumers, or about how their vision is better for other publishers, like me. Sure, Amazon is opening the market wide only until they can sew it back up again, putting a nice little monogramed “Amazon” on the side of the duffel bag that will store the severed heads of all the publishers who dare cross them in the future, but their vision includes a description of how this change will make everything better while Hachette’s vision includes a description of how everything will remain exactly the same.

Siding with Hachette, in other words, doesn’t stop Amazon from being a monopoly.

Thinking for Programmers

Leslie Lamport had a great talk at Microsoft Build 2014:

Everyone thinks they think. If you don’t write down your thoughts, you’re fooling yourself.

[…]

No programming language, no methodology, obviates the need for specification. A specification is a separate task from coding.

[…]

If you don’t start with a spec, every piece of code you write is a patch.

[…]

Thinking doesn’t guarantee that you won’t make mistakes. Not thinking guarantees that you will.

Unfortunately, he doesn’t get into an example of what you can do, e.g. model checking, with a TLA+ specification.

Swift Name Mangling

Gwynne Raskind:

Swift’s name mangling is somewhat different from C++’s. Swift uses an encoding clearly based on the C++ scheme in principle, but containing considerably more information and expressing concepts only available in a more mature type system.

[…]

Swift will have generated over 100 more symbols, but this is the complex mangled name we’ll tear apart: __TFCCC4test1a1b1c1dfS2_FTS0_1xS1_1vFT1xSi_Si_OVS_1e1f

Swift and Debuggability

Russell Ivanovic:

Currently in Swift, those stack traces are even worse. I won’t post one until they actually release it to be fair to Apple…but why oh why when they were making a ‘modern’ programming language could they not solve this? I know, the Objective-C runtime is hailed by many the world over as being fast, and awesome. But it’s 2014, the things I actually care about are the problems Microsoft and Sun Microsystems solved, memory management and reliability. If it comes at the expense of a tiny amount of speed, I’ll happily take it.

[…]

As much as people hate Java, and to some extent I’m in that camp too, here’s an equivalent crash from our Android app […] Yes I know, ha ha Null Pointer, Java, LOL. But that’s an exact line number friends. What did the user do? They tapped the subscribe button. Which page where they on? The Podcast Dialog. Zero ambiguity. Guess how many of our Android crashes we get that for? 100%. In iOS we’d be lucky if even 30% of our crashes had stack traces we can line up to actual things we can then reproduce.

Xcode Messes With Time Machine…Too Much

Jerry Krinock:

So Xcode / xcodebuild has excluded not only the Intermediate Build Files directory from Time Machine, but the parent directory of this directory as well.

This is not too good if you have, for example, as I do, another subdirectory in that parent folder for storing other needed files such as version archives (zipped products, disk images and dSYM files).

Implementing a MyBool Type in Swift

Apple:

The boolean Bool type in Swift underlies a lot of primitive functionality, making it an interesting demonstration of how to build a simple type. This post walks through the creation of a new MyBool type designed and implemented to be very similar to the Bool type built into Swift. We hope this walk through the design of a simple Swift type will help you better understand how the language works.

Fixing the “Visual Voicemail Unavailable” Error

Paul Horowitz:

If you’ve ever encountered the “Visual Voicemail is currently unavailable” error on when going to check new voicemail on the iPhone, there are two quick tricks that almost always fix the problem. So before calling the actual voicemail number like you’re some kind of cave dweller in the stone age of cell phones, try these speedy fixes first.

I was getting this error for several days. Rebooting my phone didn’t help, but toggling Airplane Mode did.

Contractually-obligated Testing

Graham Lee:

Having a contract is a bit like having unit tests, except that the unit tests are always running whenever your object is being used. Try out Eiffel; it’s pleasant to have real syntax for this, though really the Objective-C version isn’t so bad.

Finally, the contract is implemented by some simple message interception (try doing that in your favourite modern programming language of choice, non-Rubyists!).