Practical

intermediate4 of 4

Learn about practical in fp-ts semigroup

Code Editor

04-practical.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('Semigroup practical examples', () => {
  it('merges user preferences with last value winning', () => {
    const prefs = [
      { theme: 'light', fontSize: 14, notifications: false },
      { theme: 'dark', fontSize: 2, notifications: true },
      { theme: 'light', fontSize: 0, notifications: false },
    ]
    const result = mergePreferences(prefs)
    expect(result).toEqual({ theme: 'light', fontSize: 16, notifications: false })
  })

  it('combines analytics data', () => {
    const analytics = [
      { pageViews: 100, uniqueVisitors: 50, avgTimeOnSite: 60 },
      { pageViews: 150, uniqueVisitors: 75, avgTimeOnSite: 90 },
      { pageViews: 200, uniqueVisitors: 100, avgTimeOnSite: 120 },
    ]
    const result = combineAnalytics(analytics)
    expect(result).toEqual({
      pageViews: 450,
      uniqueVisitors: 225,
      avgTimeOnSite: 90, // (60 + 90 + 120) / 3
    })
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises