dipityPix app

Tuesday, July 22, 2014

A simple Swift program

Here is my first Swift program:

import Foundation
println ("256 256");
println ("255");

for i in 0...255 {
    for j in 0...255 {
        println("\(rand()%256) 255 \(j)");

It does an ASCII ppm file that is random in red, on in green, and ascending in blue.  It produces this image:

A few notes.  I had to add the import for it knew what rand() was.  The curly braces are required, which I like because it ends all those pointless arguments with yourself or others about whether to brace one line interiors of loops and the like.   I like that there is a "println" as I am tired of typing "\n".   This program took a while to write because xcode had a major silent hiccup complete with a misleading error message.   Stackoverflow to the rescue: when in doubt clean the code (menu option) and fully restart xcode.

Sunday, July 20, 2014

Swift had me at "hello world"

I'm trying Apple's new programming language Swift.  Here is the minimal hello world:

println("Hello, World!")

Done.  No mandatory cruft.

The languages from my childhood were simple.  FORTRAN 77 would have and extra two or three lines (program, stop, end if I recall).  Cish languages would have an #include and some braces.  Yeah a few lines is not a big deal (especially compared to an OpenGL program to display one triangle), but the aesthetic apparent in the Swift one has me very hopeful.

Next I will try a simple ray tracer and see if this is just an infatuation...