Fold

beginner2 of 6

Learn about fold in fp-ts these

Code Editor

02-fold.exercise.ts

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

Test Results

Requirements

describe('These fold', () => {
  it('folds left', () => {
    const result = toString(Th.left('error'))
    expect(result).toBe('Error: error')
  })

  it('folds right', () => {
    const result = toString(Th.right(42))
    expect(result).toBe('Value: 42')
  })

  it('folds both', () => {
    const result = toString(Th.both('warning', 42))
    expect(result).toBe('Warning: warning, Value: 42')
  })

  it('gets value from right', () => {
    const result = getValue(Th.right(42), 0)
    expect(result).toBe(42)
  })

  it('gets default from left', () => {
    const result = getValue(Th.left('error'), 0)
    expect(result).toBe(0)
  })

  it('gets value from both', () => {
    const result = getValue(Th.both('warning', 42), 0)
    expect(result).toBe(42)
  })
})
🧪

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