Archive for April 27, 2017

Thursday, April 27, 2017

One-off Text Filtering in BBEdit

Dr. Drang:

Although I’m far from a Unix wizard, I do find myself thinking of transformations in terms of command-line utilities rather than Text Factory actions. If only BBEdit would let me just type out a pipeline to run the selected text through. Surprisingly, it’s not all that hard to give it that ability.


As you can see, most of Any Filter is an AppleScript command that displays a dialog box asking for and collecting the command. The final line is just the execution of that command. Because this is saved as a Text Filter, BBEdit knows to feed it the selected text (or the entire current document) and replace that text with Any Filter’s output.

Using Swift Protocol Composition for Dependency Injection

Krzysztof Zabłocki:

Swift allows us to compose protocol requirements by using & operator, that means that our entities can now contain just a single dependency storage:

class FooViewModel {
    typealias Dependencies = HasImageProvider & HasArticleProvider
    let dependencies: Dependencies init(..., dependencies: Dependencies)

In your app controller or flow coordinator (Whatever is used to create new entities) you can store all dependencies under a single container struct:

struct AppDependency: HasImageProvider, HasArticleProvider, HasPersistanceProvider { 
    let imageProvider: ImageProvider
    let articleProvider: ArticlesProvider
    let persistanceProvider: PersistenceProvider

Now all app dependencies are stored in a simple data container, one that doesn’t have any logic, its not magical or anything, its just a plain struct.

Tail Call Elimination

Jordan Morgan:

The best part is, how it works isn’t magical: It just prevents the compiler from pushing more frames, thus preventing more stack growth. Why not let the callee reuse the frame stack of the caller instead of popping another one on?


That said, tail call elimination is not mutually exclusive to recursion — though it’s a case study for its benefits. In fact, in Apple’s sample code in their WWDC session mentioned above, drawRect: reaps its rewards even though it’s not an inherently recursive function.


In particular, in our world as iOS developers it can also happen if[…]


There are some use cases to disabling the magic here if you’d so choose. Namely, for the sake of profiling — you’ll get a true, clean stack trace by turning this off.

Kay Butter:

Tail Call Elimination is not always easy to rely on though with ARC inserting release or autorelease calls after what should be a tail call :/