Skip to content

Commit 3358492

Browse files
Add array/pop (#64)
* Add pop method for arrays * Add return type for pop method * Add some meanigful questions * Correct question Co-authored-by: Krystian Boruciński <krystian.borucinski@sandstream.pl>
1 parent 03772d4 commit 3358492

File tree

9 files changed

+130
-0
lines changed

9 files changed

+130
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,35 @@ Partitions the given array to the ones that pass the given predicate function an
375375
```
376376
<!-- prettier-ignore-end -->
377377

378+
#### pop
379+
380+
Returns the given array without the last element.
381+
382+
##### Type signature
383+
384+
<!-- prettier-ignore-start -->
385+
```typescript
386+
(xs: any[]) => any[]
387+
```
388+
<!-- prettier-ignore-end -->
389+
390+
##### Examples
391+
392+
<!-- prettier-ignore-start -->
393+
```javascript
394+
pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
395+
```
396+
397+
```javascript
398+
pop([]); // ⇒ []
399+
```
400+
<!-- prettier-ignore-end -->
401+
402+
##### Questions
403+
404+
- How to get an array without the last element?
405+
- How to remove the last element from an array?
406+
378407
#### range
379408

380409
Generates an array of numbers from 0 to n - 1.

array/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,35 @@ Partitions the given array to the ones that pass the given predicate function an
358358
```
359359
<!-- prettier-ignore-end -->
360360

361+
# pop
362+
363+
Returns the given array without the last element.
364+
365+
## Type signature
366+
367+
<!-- prettier-ignore-start -->
368+
```typescript
369+
(xs: any[]) => any[]
370+
```
371+
<!-- prettier-ignore-end -->
372+
373+
## Examples
374+
375+
<!-- prettier-ignore-start -->
376+
```javascript
377+
pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
378+
```
379+
380+
```javascript
381+
pop([]); // ⇒ []
382+
```
383+
<!-- prettier-ignore-end -->
384+
385+
## Questions
386+
387+
- How to get an array without the last element?
388+
- How to remove the last element from an array?
389+
361390
# range
362391

363392
Generates an array of numbers from 0 to n - 1.

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import minMax from "./minMax.js";
2323
import multiple from "./multiple.js";
2424
import none from "./none.js";
2525
import partition from "./partition.js";
26+
import pop from "./pop.js";
2627
import range from "./range.js";
2728
import removeAt from "./removeAt.js";
2829
import repeat from "./repeat.js";
@@ -71,6 +72,7 @@ export {
7172
multiple,
7273
none,
7374
partition,
75+
pop,
7476
range,
7577
removeAt,
7678
repeat,
@@ -120,6 +122,7 @@ export default {
120122
multiple,
121123
none,
122124
partition,
125+
pop,
123126
range,
124127
removeAt,
125128
repeat,

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import minMax from "./minMax";
2323
import multiple from "./multiple";
2424
import none from "./none";
2525
import partition from "./partition";
26+
import pop from "./pop";
2627
import range from "./range";
2728
import removeAt from "./removeAt";
2829
import repeat from "./repeat";
@@ -71,6 +72,7 @@ export {
7172
multiple,
7273
none,
7374
partition,
75+
pop,
7476
range,
7577
removeAt,
7678
repeat,
@@ -120,6 +122,7 @@ export default {
120122
multiple,
121123
none,
122124
partition,
125+
pop,
123126
range,
124127
removeAt,
125128
repeat,

array/pop.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import take from "./take.js";
2+
3+
export default xs => take(xs.length - 1)(xs);

array/pop.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "pop",
3+
"description": "Returns the given array without the last element.",
4+
"signature": "(xs: any[]) => any[]",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]"
9+
},
10+
{
11+
"language": "javascript",
12+
"content": "pop([]); // ⇒ []"
13+
}
14+
],
15+
"questions": [
16+
"How to get an array without the last element?",
17+
"How to remove the last element from an array?"
18+
]
19+
}

array/pop.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# pop
2+
3+
Returns the given array without the last element.
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(xs: any[]) => any[]
10+
```
11+
<!-- prettier-ignore-end -->
12+
13+
## Examples
14+
15+
<!-- prettier-ignore-start -->
16+
```javascript
17+
pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
18+
```
19+
20+
```javascript
21+
pop([]); // ⇒ []
22+
```
23+
<!-- prettier-ignore-end -->
24+
25+
## Questions
26+
27+
- How to get an array without the last element?
28+
- How to remove the last element from an array?

array/pop.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import pop from "./pop";
4+
5+
describe("pop", () => {
6+
it("returns the given array without the last element", () => {
7+
expect(pop([1, 2, 3, 4])).toEqual([1, 2, 3]);
8+
});
9+
10+
it("returns an empty array when the given array was empty", () => {
11+
expect(pop([])).toEqual([]);
12+
});
13+
});

array/pop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import take from "./take";
2+
3+
export default (xs: any[]): any[] => take(xs.length - 1)(xs);

0 commit comments

Comments
 (0)