From 53feebe0e21b7739474339ed96d4b484a76d3f6f Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Thu, 12 Nov 2015 02:20:48 +0100 Subject: [PATCH] Readme: add example. --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 80d5438..5aa2e75 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,27 @@ lazy var name: String Everything is mutable, so you can set e.g. the text encoding or reroute standard error to a file. +## Examples + +### Print line numbers + +```swift +do { + let input = try main.arguments.first.flatMap {try open($0)} ?? main.stdin + + input.read().characters.split("\n") + .enumerate().map { (linenr,line) in "\(linenr+1): " + String(line) } + .joinWithSeparator("\n").writeTo(&main.stdout) + + // add a newline at the end + print("") +} catch { + exit(error) +} +``` + +Launched with e.g. `cat long.txt | print_linenumbers.swift` or `print_linenumbers.swift long.txt` this will print the line number at the beginning of each line. + ## Installation - In the Terminal, go to where you want to download SwiftShell.