Reputation: 917
I started learning Haskell and Swift. I'm wondering if it's the correct way of thinking in "functional" way? The problem is to create card deck: I need to loop through suits and ranks - for every suit create card with given suit and rank. In "imperative" way it'd be:
let suits: Array<Character> = ...
let ranks: Array<Int> = ...
var cards: [Card]
for suit in suits {
for rank in ranks {
cards.addObject(Card(suit: suit, rank: rank))
}
}
Then I tried with pure functions using recursion, it works but, can it be done with less code ? To me "functional" in Swift is less readable, or probably I'm doing it wrong ...
let cards = cardsWithSuits(suits, ranks, [Card]());
func cardsWithSuits(suits: [Character], ranks: [Int], cards: [Card]) -> [Card] {
if suits.count == 0 { return cards }
let suit: Character = head(suits)!
let acc = cardsWithRanks(ranks, suit, cards)
return cardsWithSuits(drop(1, suits), ranks, acc)
}
func cardsWithRanks(ranks: [Int], suit: Character, cards: [Card]) -> [Card] {
if ranks.count == 0 { return cards }
let acc = cards + [Card(suit: suit, rank: head(ranks)!)]
return cardsWithRanks(drop(1, ranks), suit, acc)
}
Upvotes: 1
Views: 456
Reputation: 3422
Swift isn't a functional language. However, you can write swift code in functional style.
func map<T: Collection, U>( _ transform: (T.Iterator.Element) -> U, _ xs: T) -> [U] {
return xs.reduce([U](), {$0 + [transform($1)]})
}
func concatMap<A, B> (_ process: (A)->[B], _ xs: [A]) -> [B] {
return xs.reduce([B](), {$0 + process($1)})
}
infix operator <*>
func <*><A, B>(_ xs: [A], _ ys: [B]) -> [(A, B)]{
let transform: (A, B) -> (A, B) = {($0, $1)}
return concatMap({x in map({transform(x, $0)}, ys)}, xs)
}
struct Card {
let suit : Character
let rank : Int
static func build(_ sr: (s:Character, r:Int)) -> Card {
return Card(suit: sr.0, rank: sr.1)
}
}
func test() {
let suits : [Character] = [ "C", "D", "H", "S"]
let ranks = Array(1...13)
let cards = map(Card.build, suits <*> ranks)
print(cards)
}
If you want to learn Haskell and Swift, you could refer to https://github.com/unchartedworks/HaskellSwift
Upvotes: 0
Reputation: 13316
This may not be pretty, and I don't think it is functional programming, but it is less code and it uses Swift's terrific map and reduce functions:
struct Card {
let suit: String
let rank: Int
}
let cards = ["Heart", "Diamond", "Club", "Spade"].reduce([Card]()) { (cards, suit) in
return cards + map(1...13) { rank in return Card(suit: suit, rank: rank) }
}
Upvotes: 1
Reputation: 41226
Building on the usage of Haskell's applicative concept and <$>
and <*>
you might find the following generally useful (I think I've translated correctly, although it's based on arrays not sequences):
// use <^> because <$> is already used
infix operator <^> { associativity left }
public func <^> <T, U>(left:(T)->U, right:[T]) -> [U] {
return map(right) { return left($0) }
}
public func flatten<T>(input:[[T]]) -> [T] {
return input.reduce([], +)
}
infix operator <*> { associativity left }
public func <*> <T, U>(left:[(T)->U], right:[T]) -> [U] {
return flatten(map(left) { (function) -> [U] in
return map(right) { return function($0) }
})
}
Which then allows you to use the following:
let suits : [Character] = [ "C", "D", "H", "S"]
let ranks = Array(2...14)
struct Card {
let suit : Character
let rank : Int
static func build(suit:Character)(rank:Int) -> Card {
return Card(suit: suit, rank:rank)
}
}
Card.build <^> suits <*> ranks
Upvotes: 5