0

I'm being intruduced to functional programming and I'm avoiding as much loop as I can but I'm having some issues filling arrays. I'm learning the basics of map, sort and reduce, but I cannot apply any of those in the following code:

function generateDecks(amount){
    //the set of cards
    var cards = [
    {suit:"spades",value:"2"},
    {suit:"spades",value:"3"},
    {suit:"spades",value:"4"},
    {suit:"spades",value:"5"},
    {suit:"spades",value:"6"},
    {suit:"spades",value:"7"},
    {suit:"spades",value:"8"},
    {suit:"spades",value:"9"},
    {suit:"spades",value:"10"},
    {suit:"spades",value:"jack"},
    {suit:"spades",value:"queen"},
    {suit:"spades",value:"king"},
    {suit:"spades",value:"ace"},
    {suit:"hearts",value:"2"},
    {suit:"hearts",value:"3"},
    {suit:"hearts",value:"4"},
    {suit:"hearts",value:"5"},
    {suit:"hearts",value:"6"},
    {suit:"hearts",value:"7"},
    {suit:"hearts",value:"8"},
    {suit:"hearts",value:"9"},
    {suit:"hearts",value:"10"},
    {suit:"hearts",value:"jack"},
    {suit:"hearts",value:"queen"},
    {suit:"hearts",value:"king"},
    {suit:"hearts",value:"ace"},
    {suit:"clouds",value:"2"},
    {suit:"clubs",value:"3"},
    {suit:"clubs",value:"4"},
    {suit:"clubs",value:"5"},
    {suit:"clubs",value:"6"},
    {suit:"clubs",value:"7"},
    {suit:"clubs",value:"8"},
    {suit:"clubs",value:"9"},
    {suit:"clubs",value:"10"},
    {suit:"clubs",value:"jack"},
    {suit:"clubs",value:"queen"},
    {suit:"clubs",value:"king"},
    {suit:"clubs",value:"ace"},
    {suit:"diamonds",value:"2"},
    {suit:"diamonds",value:"3"},
    {suit:"diamonds",value:"4"},
    {suit:"diamonds",value:"5"},
    {suit:"diamonds",value:"6"},
    {suit:"diamonds",value:"7"},
    {suit:"diamonds",value:"8"},
    {suit:"diamonds",value:"9"},
    {suit:"diamonds",value:"10"},
    {suit:"diamonds",value:"jack"},
    {suit:"diamonds",value:"queen"},
    {suit:"diamonds",value:"king"},
    {suit:"diamonds",value:"ace"}
    ];
    var deck = [];

    //here it's the loop
    for (var i = 0; i < amount; i++){
        deck = deck.concat(cards);
    }
}

I want to do so without using any loop, is that possible?

1
  • Yes, it is possible. In functional programming, a loop is equivalent to a tail recursive function. Commented May 7, 2017 at 1:56

2 Answers 2

2

Can I repeat the values in an array of objects n times without using loop in javascript?

Are you just looking for Array.fill ?

let x = Array(5).fill('a')
console.log(x)
// [ 'a', 'a', 'a', 'a', 'a' ]

You could also use Array.from

let K = x => y => x
let x = Array.from(Array(5), K('a'))
console.log(x)
// [ 'a', 'a', 'a', 'a', 'a' ]


Despite what some people might say, JavaScript is terrific for functional programming

const rand = x => Math.floor(Math.random() * x)

const suits = ['♤', '♡', '♧', '♢']

const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

const Card = id => ({
  id,
  suit: suits[id % suits.length],
  rank: ranks[id % ranks.length],
})

const Deck = (suits, ranks) =>
  Array.from(Array(suits.length * ranks.length), (_,id) => Card(id))

Deck.deal = n => d =>
  [d.slice(0,n), d.slice(n)]

Deck.print = d =>
  console.log(d.map(({rank, suit}) => `${rank}${suit}`).join(','))

// create a new deck, d
const d = Deck(suits, ranks)

// deal a new hand of 5 cards, get a new deck back with the 5 cards removed
let [newHand, newDeck] = Deck.deal(5) (d)

Deck.print(newHand) // A♤,2♡,3♧,4♢,5♤
Deck.print(newDeck) // 6♡,7♧,8♢,9♤,10♡,J♧,Q♢,K♤,A♡,2♧,3♢,4♤,5♡,6♧,7♢,8♤,9♡,10♧,J♢,Q♤,K♡,A♧,2♢,3♤,4♡,5♧,6♢,7♤,8♡,9♧,10♢,J♤,Q♡,K♧,A♢,2♤,3♡,4♧,5♢,6♤,7♡,8♧,9♢,10♤,J♡,Q♧,K♢


Add a cool shuffling function or something

Deck.shuffle = d => {
  let acc = []
  for (let i = 0, j; j = rand(i), i < d.length; acc[j] = d[i], i++)
    if (j !== i)
      acc[i] = acc[j]
  return acc
}

// make a new Deck and shuffle it
const d = Deck.shuffle(Deck(suits, ranks))

// same demo as last time
let [newHand, newDeck] = Deck.deal(5) (d)

Deck.print(newHand) // 8♡,9♡,3♤,9♧,K♢
Deck.print(newDeck) // 9♤,10♤,A♡,K♡,J♢,8♧,J♧,7♤,9♢,6♡,4♤,5♡,7♢,2♤,2♧,6♢,2♢,2♡,3♢,10♢,5♤,Q♡,J♡,6♤,5♢,K♤,3♡,10♧,4♧,Q♧,7♡,10♡,A♢,8♢,8♤,4♢,J♤,K♧,Q♢,Q♤,A♧,6♧,3♧,4♡,A♤,5♧,7♧
Sign up to request clarification or add additional context in comments.

Comments

0

JavaScript isn't exactly a great language for functional programming, but you can mimic it with this:

function generateDecks(amount){
    const template_cards = /* your cards */;

    function repeat(n, cards){
        if(n < 1)    return cards;
        else         return repeat(n - 1, template_cards.concat(cards));
    }

    return repeat(amount, []);
}

In functional programming, a loop is equivalent to a tail recursive function.

1 Comment

Maybe for the first sentence :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.