From Array

beginner2 of 6

Learn about from array in fp-ts nonemptyarray

Code Editor

02-from-array.exercise.ts

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

Test Results

Requirements

describe('NonEmptyArray from array', () => {
  it('converts non-empty array', () => {
    const result = fromArraySafe([1, 2, 3])
    expect(O.isSome(result)).toBe(true)
    if (O.isSome(result)) {
      expect(result.value).toEqual([1, 2, 3])
    }
  })

  it('returns None for empty array', () => {
    const result = fromArraySafe([])
    expect(O.isNone(result)).toBe(true)
  })

  it('gets first or default', () => {
    expect(getFirstOrDefault([5, 10, 15], 0)).toBe(5)
    expect(getFirstOrDefault([], 99)).toBe(99)
  })
})
🧪

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