How to write this recursive function in Swift?
Ad
I have this recursive function that works fine in JavaScript:
var distributeSpacings = function (cols, maxSpacing) {
if (cols == 1) {
return [maxSpacing];
}
var results = [];
for (var i = 0; i <= maxSpacing; i++) {
var subSpacings = distributeSpacings(cols - 1, maxSpacing - i);
for (var j = 0; j < subSpacings.length; j++) {
var subSpacing = subSpacings[j];
results.push([i].concat(subSpacing));
}
}
return results;
}
console.log(distributeSpacings(3, 3));
// prints:
// [ [ 0, 0, 3 ],
// [ 0, 1, 2 ],
// [ 0, 2, 1 ],
// [ 0, 3, 0 ],
// [ 1, 0, 2 ],
// [ 1, 1, 1 ],
// [ 1, 2, 0 ],
// [ 2, 0, 1 ],
// [ 2, 1, 0 ],
// [ 3, 0, 0 ] ]
I want to write it in Swift. The type safety makes this difficult - this is what I have so far:
extension Array {
func concat(toAdd: Any) -> [Element] {
if let _ = toAdd as? Int {
return self + ([toAdd as! Element])
} else {
return self + (toAdd as! [Element])
}
}
}
func permutateSpacings (columns: Int, maxSpacing: Int) -> Any {
if columns == 1 {
return [maxSpacing]
}
var results = [Any]()
for (var i = 0; i <= maxSpacing; i++) {
var subSpacings = permutateSpacings(columns - 1, maxSpacing: maxSpacing - 1) as! [Int] // I suspect the problem is with this line
for (var j = 0; j < subSpacings.count; j++) {
let subSpacing = subSpacings[j]
results.append([i].concat(subSpacing))
}
}
return results
}
print(permutateSpacings(3, maxSpacing: 3) as! [[Int]])
// prints:
// Could not cast value of type 'Swift.Array<protocol<>>' (0x1175ef0d8) to 'Swift.Array<Swift.Int>' (0x1175ef028).
Ad
Answer
Ad
With thanks to sunshine for finding 1
->i
. I got a different result and thought I messed up the algorithm.
No need for your Array extension, but it would be correctly written like so:
extension Array {
func concat(toAdd: [Element]) -> [Element] {
return self + toAdd
}
func concat(toAdd: Element) -> [Element] {
return self + [toAdd]
}
}
No need for Any
at all. The type is always known.
for spacing in 0...maxSpacing
for subSpacing in subSpacings
Swiftier syntax for iterations.
func permutateSpacings(columns: Int, maxSpacing: Int) -> [[Int]] {
if columns == 1 {
return [[maxSpacing]]
}
var results : [[Int]] = [] // var/let : Type = Value
for spacing in 0...maxSpacing {
let subSpacings = permutateSpacings(columns - 1, maxSpacing: maxSpacing - spacing)
for subSpacing in subSpacings {
results.append(([spacing] + subSpacing))
}
}
return results
}
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad