mReverse

mReverse

  • (anArray: array) => anArray
  1. Reverse an array

    Consider reverse instead.
    This is for the uncommon case where mutating anArray is required.

    const arr = ['a', 'b', 'c']
    const mutatedArr = mReverse(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['c', 'b', 'a']
    const arr = ['a', 'b', 'c']
    const mutatedArr = mReverse(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['c', 'b', 'a']
    
    const arr = ['a', 'b', 'c']
    const mutatedArr = mReverse(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['c', 'b', 'a']
    const arr = ['a', 'b', 'c']
    const mutatedArr = mReverse(arr)
    
    console.log(mutatedArr === arr) // is true
    console.log(arr) // is ['c', 'b', 'a']
    
  2. Sometimes we want to mutate an array by reversing it. Below, we run an email provider. Some customers prefer their newest emails to be displayed last. Let's implement this preference by reversing the emails.

    const emailFolders = {
      work: [
        { time: '01:15pm', subject: 'Follow up about review' },
        { time: '10:03am', subject: 'Lunch at Arbys?' },
        { time: '08:52am', subject: 'Meeting at 9 cancelled' },
      ],
      daycare: [
        { time: '03:01pm', subject: 'Emma has been great!' },
        { time: '07:39am', subject: 'Bill adjustment approval' },
      ],
    }
    
    const preferMostRecentLast = forEach(mReverse)
    preferMostRecentLast(emailFolders)
    
    console.log(emailFolders)
    // is {
    //   work: [
    //     {
    //        time: 08:52am
    //        subject: Meeting at 9 cancelled
    //     }, {
    //        time: 10:03am
    //        subject: Lunch at Arbys?
    //     }, {
    //        time: 01:15pm
    //        subject: Follow up about review
    //     }
    //   ]
    //   daycare: [
    //     {
    //        time: 07:39am
    //        subject: Bill adjustment approval
    //     }, {
    //        time: 03:01pm
    //        subject: Emma has been great!
    //     }
    //   ]
    // }
    type Email = {
      time: string
      subject: string
    }
    type EmailFolders = Record<string, Email[]>
    const emailFolders: EmailFolders = {
      work: [
        { time: '01:15pm', subject: 'Follow up about review' },
        { time: '10:03am', subject: 'Lunch at Arbys?' },
        { time: '08:52am', subject: 'Meeting at 9 cancelled' },
      ],
      daycare: [
        { time: '03:01pm', subject: 'Emma has been great!' },
        { time: '07:39am', subject: 'Bill adjustment approval' },
      ],
    }
    
    const preferMostRecentLast = forEach(mReverse<Email[]>)<EmailFolders>
    preferMostRecentLast(emailFolders)
    
    console.log(emailFolders)
    // is {
    //   work: [
    //     {
    //        time: 08:52am
    //        subject: Meeting at 9 cancelled
    //     }, {
    //        time: 10:03am
    //        subject: Lunch at Arbys?
    //     }, {
    //        time: 01:15pm
    //        subject: Follow up about review
    //     }
    //   ]
    //   daycare: [
    //     {
    //        time: 07:39am
    //        subject: Bill adjustment approval
    //     }, {
    //        time: 03:01pm
    //        subject: Emma has been great!
    //     }
    //   ]
    // }
    
    const emailFolders = {
      work: [
        {
          time: '01:15pm',
          subject: 'Follow up about review',
        },
        {
          time: '10:03am',
          subject: 'Lunch at Arbys?',
        },
        {
          time: '08:52am',
          subject: 'Meeting at 9 cancelled',
        },
      ],
      daycare: [
        {
          time: '03:01pm',
          subject: 'Emma has been great!',
        },
        {
          time: '07:39am',
          subject: 'Bill adjustment approval',
        },
      ],
    }
    
    const preferMostRecentLast = forEach(mReverse)
    preferMostRecentLast(emailFolders)
    
    console.log(emailFolders)
    // is {
    //   work: [
    //     {
    //        time: 08:52am
    //        subject: Meeting at 9 cancelled
    //     }, {
    //        time: 10:03am
    //        subject: Lunch at Arbys?
    //     }, {
    //        time: 01:15pm
    //        subject: Follow up about review
    //     }
    //   ]
    //   daycare: [
    //     {
    //        time: 07:39am
    //        subject: Bill adjustment approval
    //     }, {
    //        time: 03:01pm
    //        subject: Emma has been great!
    //     }
    //   ]
    // }
    type Email = {
      time: string
      subject: string
    }
    type EmailFolders = Record<string, Email[]>
    const emailFolders: EmailFolders = {
      work: [
        {
          time: '01:15pm',
          subject: 'Follow up about review',
        },
        {
          time: '10:03am',
          subject: 'Lunch at Arbys?',
        },
        {
          time: '08:52am',
          subject: 'Meeting at 9 cancelled',
        },
      ],
      daycare: [
        {
          time: '03:01pm',
          subject: 'Emma has been great!',
        },
        {
          time: '07:39am',
          subject: 'Bill adjustment approval',
        },
      ],
    }
    
    const preferMostRecentLast = forEach(
      mReverse<Email[]>
    )<EmailFolders>
    preferMostRecentLast(emailFolders)
    
    console.log(emailFolders)
    // is {
    //   work: [
    //     {
    //        time: 08:52am
    //        subject: Meeting at 9 cancelled
    //     }, {
    //        time: 10:03am
    //        subject: Lunch at Arbys?
    //     }, {
    //        time: 01:15pm
    //        subject: Follow up about review
    //     }
    //   ]
    //   daycare: [
    //     {
    //        time: 07:39am
    //        subject: Bill adjustment approval
    //     }, {
    //        time: 03:01pm
    //        subject: Emma has been great!
    //     }
    //   ]
    // }