Literate Swift

Originator:rix.rob
Number:rdar://17894791 Date Originated:02-Aug-2014 10:26 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6-Beta4 (6A267n)
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
I would like to be able to do literate-style Swift programs in e.g. .lswift files.

The structure of these would be arbitrary text (tho Markdown by convention would be reasonable), with the Swift code occurring as e.g. any block indented by one level, or any blocks of text whose lines are prefixed with >

Xcode would ideally provide syntax highlighting only for the Swift code, leaving the rest as plain text. Bonus points if documentation comments can occur in the 

The rest of the Summary section is an example:


A naïve, primitive recursive implementation of the factorial function is quite inefficient, because it recomputes the results it needs at every step.

	func naïveFactorial(n: Int) -> Int {
		return n > 0 ? n * naïveFactorial(n - 1) : 1
	}

A memoizing implementation is much more efficient, at the cost of some space.

	let memoizingFactorial = memoize { recur, n in
		n > 0 ? n * recur(n - 1) : 1
	}

We use the memoizing combinator given in the Advanced Swift session of WWDC2014.

	func memoize<T : Hashable, U>(body: (T -> U, T) -> U) -> T -> U {
		var memo = [ T : U ]()
		var recursive: (T -> U)!
		recursive = { x in
			if let found = memo[x] { return found }
			let result = body(recursive, x)
			memo[x] = result
			return result
		}
		return recursive
	}

To-do: wax philosophical about memoization and fixpoints…


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
cf http://www.haskell.org/haskellwiki/Literate_programming

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!