An open API service indexing awesome lists of open source software.

https://github.com/danvc/theorem


https://github.com/danvc/theorem

Last synced: over 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# theorem

## The problem
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].

## Prerequisits for testing
Make sure that all dependencies for tests are available. Run:
```
yarn install
```

## How to test
```
yarn test
```

## The implementation
There are `2` implementations for this problem. One (`lazyFlatArray`) is using the very familiar method called `reduce` from the `Array` which reduces each elements to a initial value that is an empty `[]`.

The second (`fasterFlatArray`) method is a function that runs through the array which could be calling itself (recursively) to do a deeper flatten in the array.

The `fastFlatArray` is faster than the `reducer` method in almost 100%;

There is a `third` method called `createsNestedArray` which creates random nested arrays (that may contain numbers or, array of (numbers||array)

## The tests covering
The test cover the following scenarios:
- [x] pass theorem test using lazyFlatArray();
- [x] pass theorem test using fastFlatArray();
- [x] lazyFlatArray() pass over 1000 tests;
- [x] fasterFlatArray() pass over 1000 tests;
- [x] fasterFlatArray() is faster than lazyFlatArray() method;