Skip to content

Commit

Permalink
any sequence, no black
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhumber committed May 28, 2022
1 parent c461982 commit 71cc93e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 1 addition & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="marc",
version="22.5.0",
version="22.5.1",
url="https://github.com/maxhumber/marc",
description="Markov chain generator",
long_description=long_description,
Expand All @@ -20,11 +20,6 @@
],
packages=[""],
package_dir={"": "python/src"},
extras_require={
"dev": [
"black>=22.3.0",
],
},
python_requires=">=3.9",
setup_requires=["setuptools>=62.1.0"],
)
4 changes: 2 additions & 2 deletions swift/Sources/Marc/MarkovChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class MarkovChain<Element: Hashable> {
/// ```
/// let chain = MarkovChain(["R", "P", "S"])
/// ```
public init(_ sequence: [Element]) {
public init<S: Sequence>(_ sequence: S) where S.Element == Element {
// reducing per: https://developer.apple.com/documentation/swift/dictionary/3127177-reduce
store = zip(sequence, sequence[1...]).reduce(into: Store()) { result, pair in
store = zip(sequence, sequence.dropFirst(1)).reduce(into: Store()) { result, pair in
result[pair.0, default: [:]][pair.1, default: 0] += 1
}
}
Expand Down
13 changes: 13 additions & 0 deletions swift/Tests/MarcTests/MarcTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import XCTest
final class MarcTests: XCTestCase {
var chain: MarkovChain<String>!

typealias PerfStore = [Int: [Int: Int]]
let perfSeq = (0..<10_000).map { _ in Int.random(in: 1...5) }
var perfStore: PerfStore!

override func setUp() {
let playerThrows = "RRRSRSRRPRPSPPRPSSSPRSPSPRRRPSSPRRPRSRPRPSSSPRPRPSSRPSRPRSSPRP"
let sequence = playerThrows.map { String($0) }
chain = MarkovChain(sequence)
perfStore = PerfStore()
}

func testSubscript() {
Expand All @@ -28,4 +33,12 @@ final class MarcTests: XCTestCase {
let contains = ["R", "P", "S"].contains(next)
XCTAssertTrue(contains)
}

func testStorePerformance() {
measure {
perfStore = zip(perfSeq, perfSeq.dropFirst(1)).reduce(into: PerfStore()) { result, pair in
result[pair.0, default: [:]][pair.1, default: 0] += 1
}
}
}
}

0 comments on commit 71cc93e

Please sign in to comment.