Swift Developer News - Hacking with Swift News, tips, and tutorials from Hacking with Swift https://www.hackingwithswift.com/articles/rss (c)2017 Paul Hudson https://www.hackingwithswift.com/favicon-96x96.png Swift Developer News - Hacking with Swift https://www.hackingwithswift.com/articles/rss Write your scripts in Swift with Beak https://www.hackingwithswift.com/articles/47/write-your-scripts-in-swift-with-beak https://www.hackingwithswift.com/articles/47/write-your-scripts-in-swift-with-beak It’s a smart and simple way to run code by function. Mon, 08 Jan 2018 13:20:42 +0000 If you’re visiting here it’s because you’re already sold on Swift as a great language for apps, but have you ever considered using it for scripting? If not, a new GitHub project from Yonas Kolb is here to convince you otherwise: it’s called Beak and it lets your run Swift code straight from the command line.

To get started, run these three commands from your Mac’s terminal:

git clone https://github.com/yonaskolb/Beak.git
cd Beak
swift run beak run install

That builds and installs Beak, which means it’s ready to run any Swift scripts you need.

Let’s try it out now. Create a new Swift file on your desktop called factors.swift and give it this content:

import Foundation

public func calculate() {
    let number = 100
    let factors = (1...number).filter { number % $0 == 0 }
    print(factors.reduce("Factors: ") { $0 + String($1) + " " })

That creates a single function called calculate(), which creates an array of numbers that divide equally into 100, then prints them out as a single string.

To run that using Beak you need to write a command that does the following:

  • Specifies factors.swift as its input filename.
  • Tells it to run a single function from the file.
  • Provides the function name to run.

Run this command now:

beak --path factors.swift run calculate

You specify the function name you want to run, so a single Swift script can contain as many related functions as you want.

As well as run you can also use list to show the list of available functions:

beak --path factors.swift list

That will show you there’s only function available, which is our calculate() function.

Tip: Beak can only see functions that are marked as public. If you want a function for internal use only – i.e., not exposed by the list command – just don’t declare it as public.

Right now our function uses a hard-coded input val...

Take the Hacking with Swift 2017 Swiftmas Quiz https://www.hackingwithswift.com/articles/46/take-the-hacking-with-swift-2017-swiftmas-quiz https://www.hackingwithswift.com/articles/46/take-the-hacking-with-swift-2017-swiftmas-quiz 16 questions that will test your knowledge of Swift history and more Fri, 15 Dec 2017 12:14:32 +0000 I'm supposed to be busy finishing my new book on Server-Side Swift with Vapor, but it's the Swiftmas season and I thought you all deserved some time away from Xcode. I already posted a special Swiftmas wordsearch but you're all far too clever and solved that within a day.

So, it's time for something harder: Hacking with Swift's first annual Swiftmas Quiz. Below you'll find 16 questions that test your knowledge of Swift's history, Swift's community, and Swift's canine friends. Share your result on Twitter and see if you beat your friends!