Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created March 23, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/c5adac26bba63c376b0a to your computer and use it in GitHub Desktop.
Save jonelf/c5adac26bba63c376b0a to your computer and use it in GitHub Desktop.
Mandelbrot in Swift
for line in 0...32 {
println("".join((0...78).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
do {
x = x * x - y * y + Double(col) / 20.0 - 2.3
y = 2 * x * y + Double(line) / 10.0 - 1.5
i++
} while (x * x + y * y) < 4 && i < 78
return "\(UnicodeScalar(0x1f35b + i))"
})
)
}
@jonelf
Copy link
Author

jonelf commented Jun 9, 2016

In Swift 2.2.1

for line in 0...32 {
    print((0...78).map { (col: Int) -> String in
        var x = 0.0, y = x, i = 0
        repeat {
            x = x * x - y * y + Double(col) / 20.0 - 2.3
            y = 2 * x * y + Double(line) / 10.0 - 1.5
            i = i + 1
        } while (x * x + y * y) < 4 && i < 78
        return "\(UnicodeScalar(0x1f35b + i))"
    }.joinWithSeparator(""))
}

@jonelf
Copy link
Author

jonelf commented Jun 9, 2016

In Swift 3.0

for line in 0...32 {
    print((0...65).map { (col: Int) -> String in
        var x = 0.0, y = x, i = 0
        repeat {
            x = x * x - y * y + Double(col) / 20.0 - 2.1
            y = 2 * x * y + Double(line) / 10.0 - 1.5
            i = i + 1
        } while (x * x + y * y) < 4 && i < 78
        return "\(UnicodeScalar(0x1f35b + i))"
    }.joined(separator: ""))
}

https://swiftlang.ng.bluemix.net/#/repl/5759332a83847baf29642974

@jonelf
Copy link
Author

jonelf commented Feb 15, 2024

for line in 0...42 {
    print((0...65).map { (col: Int) -> String in
        var x = 0.0, y = x, i = 0
        repeat {
            x = x * x - y * y + Double(col) / 20.0 - 2.1
            y = 2 * x * y + Double(line) / 13.0 - 1.5
            i = i + 1
        } while (x * x + y * y) < 4 && i < 78
        return String(UnicodeScalar(i + 0x1f35b)!)
    }.joined(separator: ""))
}

This one works in Swift 5.9.2. Save and run with xcrun swift filename.swift

@jonelf
Copy link
Author

jonelf commented Feb 15, 2024

image

@jonelf
Copy link
Author

jonelf commented Feb 16, 2024

Open a big macOS terminal and run
echo 'for l in 0...42{print((0...65).map{(col: Int)->String in var x=0.0,y=x,i=0;repeat{x=x*x-y*y+Double(col)/20.0-2.1;y=2*x*y+Double(l)/13.0-1.5;i=i+1}while(x*x+y*y)<4&&i<78;return String(UnicodeScalar(i+0x1f35b)!)}.joined(separator:""))}'|swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment