Created
January 22, 2017 18:23
-
-
Save nicky1525/20458c5d74889c13ab94f8d88e6736cd to your computer and use it in GitHub Desktop.
Functional Kata, learning functional programming.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import FunctionalKata | |
class FunctionalKataTests: XCTestCase { | |
// Create a function to count the elements contained in a list | |
func count(list: [Any?]) -> Int { | |
if let _ = list.first { | |
let tail = Array(list.dropFirst()) | |
return 1 + count(list: tail) | |
} | |
return 0 | |
} | |
func sum(_ list: [Int]) -> Int { | |
return list.reduce(0, +) | |
} | |
// Sample implementation of + | |
func plus(_ left: Int, _ right: Int) -> Int { return left + right } | |
func testListOfNilValues_Count_ReturnsNumberOfNilValues() { | |
let list: [String?] = [nil, nil] | |
XCTAssertEqual(count(list: list), 2) | |
} | |
func testEmptyList_Count_ReturnsZero() { | |
XCTAssertEqual(count(list: [String]()), 0) | |
} | |
func testListOneElement_Count_ReturnsOne() { | |
XCTAssertEqual(count(list: ["element"]), 1) | |
} | |
func testListTwoElements_Count_ReturnsTwo() { | |
XCTAssertEqual(count(list: ["element", "anotherElement"]), 2) | |
} | |
func testSumZeroToZero_ReturnsZero() { | |
XCTAssertEqual(sum([]), 0) | |
} | |
func testSum123Returns6() { | |
XCTAssertEqual(sum([1,2,3]), 6) | |
} | |
func testSumMinusOneToOne_Returns0() { | |
XCTAssertEqual(sum([-1,1]), 0) | |
} | |
func test1Plus2_Returns3() { | |
XCTAssertEqual(plus(1,2), 3) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment