Semigroup

intermediate5 of 6

Learn about semigroup in fp-ts these

Code Editor

05-semigroup.exercise.ts

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

Test Results

Requirements

describe('These Semigroup', () => {
  it('combines two rights', () => {
    const result = combineThese(Th.right(10), Th.right(20))
    expect(result).toEqual(Th.right(30))
  })

  it('combines two lefts', () => {
    const result = combineThese(Th.left('error1'), Th.left('error2'))
    expect(result).toEqual(Th.left('error1error2'))
  })

  it('combines left and right', () => {
    const result = combineThese(Th.left('error'), Th.right(42))
    expect(result).toEqual(Th.both('error', 42))
  })

  it('combines two boths', () => {
    const result = combineThese(Th.both('warn1', 10), Th.both('warn2', 20))
    expect(result).toEqual(Th.both('warn1warn2', 30))
  })

  it('accumulates warnings', () => {
    const result = combineThese(
      Th.both('warning1, ', 10),
      combineThese(Th.both('warning2, ', 20), Th.right(30))
    )
    expect(result).toEqual(Th.both('warning1, warning2, ', 60))
  })
})
🧪

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