User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Swift RPi

Sun Nov 17, 2019 8:17 am

Swift RPi

I was able to install Swift as a pre-built binary distribution for the RPi. This LINK is the project repository. You can install the binaries or build it yourself.

I took a peek at Swift for Ubuntu about 5 years ago. It was at version 2.1 at that time. It is now at 5.1.

The Swift Programming Language

Swift GPIO

Using Legacy C APIs with Swift

Code: Select all

[email protected]:~/swift-dev $ swift --version
Swift version 5.1.1 (swift-5.1.1-RELEASE)
Target: armv7-unknown-linux-gnueabihf
[email protected]:~/swift-dev $ cat hello.swift
print("Hello Swift")
[email protected]:~/swift-dev $ swift hello.swift
Hello Swift
[email protected]:~/swift-dev $ swiftc hello.swift -o hello
[email protected]:~/swift-dev $ ./hello
Hello Swift
[email protected]:~/swift-dev $ ls -l
total 16
-rwxr-xr-x 1 pi pi 10032 Nov 15 19:38 hello
-rw-r--r-- 1 pi pi    21 Nov 15 19:35 hello.swift
[email protected]:~/swift-dev $ 
Swift Value Diff

Code: Select all

func value_diff(nums: [Int]) -> Int {
    var min_num = nums[0]
    var max_num = nums[0]
    
    for x in 0..<nums.count 
    {
        min_num = min(nums[x], min_num)
        max_num = max(nums[x], max_num)
    }
    
    return max_num - min_num
}

print(value_diff(nums: [-5, -3, -7, 0]))
print(value_diff(nums: [8, 2, 14, 24]))  
print(value_diff(nums: [1, 0, 6, 3]))
Output

Code: Select all

[email protected]:~/swift-dev $ time swift value_diff.swift

7
22
6

real	0m0.471s
user	0m0.373s
sys	0m0.100s
[email protected]:~/swift-dev $ 
[email protected]:~/swift-dev $ swiftc value_diff.swift -o vd
[email protected]:~/swift-dev $ time ./vd
7
22
6

real	0m0.026s
user	0m0.008s
sys	0m0.018s
[email protected]:~/swift-dev $ 
Last edited by John_Spikowski on Tue Nov 19, 2019 2:06 am, edited 5 times in total.

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Sun Nov 17, 2019 5:56 pm

My comment was about programming with Swift. Totally on topic.

I don't think a plan by one person, Apple, can be called a conspiracy. Does that not take two or more?

But OK, carry on, I'll keep out of it.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Sun Nov 17, 2019 9:47 pm

Here is a recursive fibo 24 in Swift. There is a nice BIGINT and GMP Swift extensions available if one would like to take a shot at the 1 mil fibo challenge.

Code: Select all

/* Basic fibonacci function in swift.
   Demostrates func calls and recursion.
*/
 
func fibonacci(i: Int) -> Int {
    if i <= 2 {
        return 1
    } else {
        return fibonacci(i:i - 1) + fibonacci(i:i - 2)
    }
}
 
print(fibonacci(i:24))
Output

Code: Select all

[email protected]:~/swift-dev $ time swift fiborecursive.swift
46368

real	0m0.434s
user	0m0.333s
sys	0m0.102s
[email protected]:~/swift-dev $ swiftc fiborecursive.swift -o fibos24
[email protected]:~/swift-dev $ time ./fibos24
46368

real	0m0.029s
user	0m0.010s
sys	0m0.019s
[email protected]:~/swift-dev $ 

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Sun Nov 17, 2019 10:21 pm

Be aware that the Million Digit Fibonacci Challenge to which John refers specifies that solutions use only a single language and do not make use of non-standard libraries.

https://github.com/ZiCog/fibo_4784969/b ... /README.md


It would be interesting though to see the big number arithmetic required for the challenge done in Swift. The Karatsuba multiplication for example.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Sun Nov 17, 2019 10:31 pm

I will posting a SQLite3 Swift example AIR did for Ubuntu. Using C libraries with Swift isn't that difficult.
Be aware that the Million Digit Fibonacci Challenge to which John refers specifies that solutions use only a single language and do not make use of non-standard libraries.
I believe the BIGINT extention is written in Swift.

Swift BigInt

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Sun Nov 17, 2019 10:50 pm

That does indeed include a multiplication using the Karatsuba algorithm.

It would be interesting to see a Swift Million Digit Fibo solution using that.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Sun Nov 17, 2019 11:07 pm

Are you interested enough to write the code to do it or hoping I or someone else will satisfy your curiosity?

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Sun Nov 17, 2019 11:12 pm

As you have taken the trouble to start a Swift thread and brought up the Fib challenge I assumed you would be chomping at the bit to get on with it.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Sun Nov 17, 2019 11:21 pm

I'm the Swift messenger not its maintainer.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Sun Nov 17, 2019 11:50 pm

As a reference, here is the same in C BASIC.

C BASIC is C dressed in BASIC.

Code: Select all

#include <stdio.h>
#include "cbasic.h"
 
FUNCTION int Fibonacci(int i)
BEGIN_FUNCTION
  IF (i <= 2) THEN
    RETURN_FUNCTION(1);
  ELSE
    RETURN_FUNCTION(Fibonacci(i - 1) + Fibonacci(i - 2));
  END_IF
END_FUNCTION
 
MAIN
BEGIN_FUNCTION
  PRINT ("%i\n",Fibonacci(24));
END_FUNCTION
Output

Code: Select all

[email protected]:~/c-basic $ time ./fibocb
46368

real	0m0.008s
user	0m0.008s
sys	0m0.001s
[email protected]:~/c-basic $ 

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Mon Nov 18, 2019 4:12 pm

AIR was kind enough to get me pointed in the right direction with current C package building. I was able to build a cool web server in Swift which I will be posting today.

Swift deprecates code faster than any language I have used. They are definitely fast tracking development. The bright side is their syntax and error reporting is some of the best I've run into.

Swift isn't a language to be ignored and if I were Python, I would be worried.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25906
Joined: Sat Jul 30, 2011 7:41 pm

Re: Swift RPi

Mon Nov 18, 2019 4:28 pm

Had a quick tidy up of this thread.

Some of the post were practically asking for a temporary ban. People should know better. I've had my hot chocolate for the day, in a good mood, so count yourselves lucky.

That is all.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Mon Nov 18, 2019 4:47 pm

Honest, I wasn't looking for strike 3. I'm trying to keep this thread focused on the benefits of programming in Swift. Opinions should be posted somewhere else.

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Mon Nov 18, 2019 5:20 pm

Your Swift fibo gives the wrong answer for n = 0. You could fix that using Swift's pattern matching syntax.

It also seems a bit slow. But it's a bit too small of a test case to tell. How about something we can measure properly, all the fibos from 0 to 42?

Rust on my Pi 3:

Code: Select all

fn fibonacci(n: i32) -> i32 {
    match n {
        0     => 0,
        1..=2 => 1,
        _     => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

fn main () {
    for n in 0..=42 {
        println!("fibo({}) = {}", n, fibonacci(n));
    }
}

Code: Select all

$ rustc  -C opt-level=3   fibo.rs
[email protected]:~ $ time ./fibo
fibo(0) = 0
fibo(1) = 1
fibo(2) = 1
fibo(3) = 2
fibo(4) = 3
fibo(5) = 5
...
...
fibo(37) = 24157817
fibo(38) = 39088169
fibo(39) = 63245986
fibo(40) = 102334155
fibo(41) = 165580141
fibo(42) = 267914296

real    0m18.543s
user    0m18.529s
sys     0m0.010s
Last edited by Heater on Mon Nov 18, 2019 5:23 pm, edited 1 time in total.
Memory in C++ is a leaky abstraction .

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25906
Joined: Sat Jul 30, 2011 7:41 pm

Re: Swift RPi

Mon Nov 18, 2019 5:22 pm

John_Spikowski wrote:
Mon Nov 18, 2019 4:47 pm
Honest, I wasn't looking for strike 3. I'm trying to keep this thread focused on the benefits of programming in Swift. Opinions should be posted somewhere else.
You were not the standout...
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Mon Nov 18, 2019 5:37 pm

Shocked!

A good Monday.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Tue Nov 19, 2019 1:34 am

To build the SQLite package for Swift, I used the PerfectSoft template method.

1, Clone THIS repo to a directory of choice.

2. cd to the Perfect-SQLite directory.

3. swift build

An added benefit of the PerfectSoft template is it adds a CRUD library as well.
CRUD is an object-relational mapping (ORM) system for Swift 4+. CRUD takes Swift 4 Codable types and maps them to SQL database tables. CRUD can create tables based on Codable types and perform inserts and updates of objects in those tables. CRUD can also perform selects and joins of tables, all in a type-safe manner.

CRUD uses a simple, expressive, and type safe methodology for constructing queries as a series of operations. It is designed to be light-weight and has zero additional dependencies. It uses generics, KeyPaths and Codables to ensure as much misuse as possible is caught at compile time.

Database client library packages can add CRUD support by implementing a few protocols. Support is available for SQLite, Postgres, and MySQL.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Tue Nov 19, 2019 5:12 pm

I'm able to create C library package interfaces without issue. What I can't seem to figure out is how to install and use them. My IMPORT statement fails saying it can't find the package. The PerfectSoft site makes it sound like that this should be obvious. (to everyone but me)

@amesh,

Thanks for turning down the heat.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Tue Nov 19, 2019 9:12 pm

I have been searching for what GUI options there may be for Swift on the RPi. Unless I'm missing something, you're on your own.

I was able to get IUP working with Swift on Ubuntu a few years back when it made its appearance on the open source scene. I ported IUP to the RPi recently and posted a few examples with my ScriptBasic binding.

I will see if I can make the IUP connection with Swift work on the RPi. The combo should be pretty sweet.

If I can get IUP working with Swift on the RPi, I may be able to get the IUP editor to work as an IDE. Getting ahead of myself but I thought I would share my thoughts and current direction with this effort.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Thu Nov 21, 2019 8:43 pm

Anyone with a Swift interest out there?

I could use a mentor for this thread.

If I can figure out how to install these C library packages so that an IMPORT finds them, I will create a project in the sandbox for them.

shaymanjohn
Posts: 7
Joined: Tue Nov 12, 2019 8:32 pm

Re: Swift RPi

Fri Nov 22, 2019 10:11 am

Hi John,

By day I'm a Swift dev - iOS apps mainly, but also done some server side stuff using Kitura...

Would be happy to try help out - never done anything with C library packages in Swift, so this is all new to me too..

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Swift RPi

Fri Nov 22, 2019 5:05 pm

Welcome aboard!

Do you have Swift installed on your RPi?

Swift seems to have come a long way on Linux since I took a peek at it a few years ago when it was at version 2.1.

hippy
Posts: 7097
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Swift RPi

Sat Nov 23, 2019 6:21 pm

Heater wrote:
Mon Nov 18, 2019 5:20 pm
Your Swift fibo gives the wrong answer for n = 0. You could fix that using Swift's pattern matching syntax.
Or just correct the incorrect -

Code: Select all

    if i <= 2 {
        return 1
to, correcting the comparison and returning 'i' rather than '1' -

Code: Select all

    if i < 2 {
        return i
There's a reason I started using 'n' rather than 'i' as the default single letter variable name :P

Heater
Posts: 15318
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swift RPi

Sat Nov 23, 2019 6:57 pm

hippy,
Not sure where you are drawing that conclusion from as, for Fibo(24)=46368 which seems to have become an adopted de-facto benchmark, Swift appears to be pretty quick -
I get it from measuring it.

The time reported by John_Spikowski for fibo(24) above on a Pi 4 is 0.029s.

The time I get in Rust 0.011 seconds. And that is on a Pi 3B !

Code: Select all

$ cat fibo.rs
fn fibonacci(n: i32) -> i32 {
    match n {
        0 => 0,
        1..=2 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

fn main () {
    let n = 24;
    println!("fibo({}) = {}", n, fibonacci(n));
}

[email protected]:~ $ rustc  -C opt-level=3   fibo.rs
[email protected]:~ $ time ./fibo
fibo(24) = 46368

real    0m0.011s
user    0m0.011s
sys     0m0.000s
C on my Pi 3 does it in 0.008 seconds:

Code: Select all

 $ cat fibo.c
#include "stdio.h"

int fibonacci(int n) {
    if (n <2) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main () {
    int n = 24;
    printf("fibo(%d) = %d\n", n, fibonacci(n));
}

[email protected]:~ $ gcc -Wall -O3 -o fibo  fibo.c
[email protected]:~ $ time ./fibo
fibo(24) = 46368

real    0m0.008s
user    0m0.008s
sys     0m0.000s
Give that a Pi 4 is two or more times faster than a Pi 3 that makes John's Swift about 5 times slower.

The question then is, why are you getting a very different result than John?

Of course doing this is very silly. With such a short run time the figures are almost meaningless. It can happen for example that the processor does not have enough time to think about cranking it's clock rate up to do the job. Which has caught me out before.
Memory in C++ is a leaky abstraction .

hippy
Posts: 7097
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Swift RPi

Sat Nov 23, 2019 7:16 pm

Heater wrote:
Sat Nov 23, 2019 6:57 pm
hippy,
Not sure where you are drawing that conclusion from as, for Fibo(24)=46368 which seems to have become an adopted de-facto benchmark, Swift appears to be pretty quick -
I get it from measuring it.

The time reported by John_Spikowski for fibo(24) above on a Pi 4 is 0.029s.
Yes; I deleted that from my earlier post when I realised I wasn't comparing like with like.
Heater wrote:
Sat Nov 23, 2019 6:57 pm
By the way, your correction for n = 0 is incorrect.
Are you sure ? It seems to work for me, giving 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 etc, and is the equivalent of your fibo.c, though with the, possibly redundant, 'else' left in ...

Code: Select all

[email protected]:~/swift $ cat fibo.swift
func fibonacci(n: Int) -> Int {
    if n < 2 {
        return n
    } else {
        return fibonacci(n:n - 1) + fibonacci(n:n - 2)
    }
}

for n in 0...9 { 
  print("Fibo(\(n)) = \(fibonacci(n:n))")
}

[email protected]:~/swift $ ./usr/bin/swift fibo.swift
Fibo(0) = 0
Fibo(1) = 1
Fibo(2) = 1
Fibo(3) = 2
Fibo(4) = 3
Fibo(5) = 5
Fibo(6) = 8
Fibo(7) = 13
Fibo(8) = 21
Fibo(9) = 34

Return to “Other programming languages”