From 092087fc540337ff964d76885d823d3a6a5a9c58 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Oct 2015 14:15:21 -0400 Subject: [PATCH] Extract row-major ordering into a private function. --- prototype/Doubt/Matrix.swift | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/prototype/Doubt/Matrix.swift b/prototype/Doubt/Matrix.swift index b87029f77..c983aaf97 100644 --- a/prototype/Doubt/Matrix.swift +++ b/prototype/Doubt/Matrix.swift @@ -5,16 +5,7 @@ /// Values are retrieved by subscripting with row/column indices. Out-of-bound indices produce `nil` values, rather than asserting. public struct Matrix { public init(width: Int, height: Int, compute: (Int, Int) -> A) { - var values: [Memo] = [] - values.reserveCapacity(width * height) - - for j in 0.. { compute(i, j) }) - } - } - - self.init(width: width, height: height, values: values) + self.init(width: width, height: height, values: constructRowMajor(width, height: height, forEach: compute >>> Memo.init)) } public let width: Int @@ -43,3 +34,14 @@ public struct Matrix { self.values = values } } + +private func constructRowMajor(width: Int, height: Int, @noescape forEach: (Int, Int) -> A) -> [A] { + var values: [A] = [] + values.reserveCapacity(width * height) + for j in 0..