Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add take and drop functions #37

Merged
merged 2 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: node_js
node_js:
- 9
- 10
cache: yarn
branches:
except:
- /^no-ci.*$/
script:
- yarn test
- yarn test-coveralls
- yarn typecheck
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "./scripts/build.sh",
"typecheck": "tsc --noEmit",
"test": "TIMEOUT=2000 jest --env=node",
"test-coveralls": "TIMEOUT=2000 jest --env=node --coverage --coverageReporters=text-lcov | coveralls"
"test-coveralls": "TIMEOUT=2000 jest --env=node --coverage --coverageReporters=text-lcov --forceExit | coveralls"
},
"dependencies": {
"@ericandrewlewis/bitmap": "^1.0.0",
Expand Down
20 changes: 20 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,24 @@ function set_tail(xs,x) {
}
}

// take(xs, n) puts the first n elements of xs into a list.
function take(xs, n) {
if (n < 0) {
throw new Error("take(xs, n) expects a positive integer as " +
"argument n, but encountered " + n);
}
return (n === 0) ? [] : pair(head(xs), take(tail(xs), n - 1));
}

// drop(xs, n) removes the first n elements from xs and returns the rest (as a list)
function drop(xs, n) {
if (n < 0) {
throw new Error("drop(xs, n) expects a positive integer as " +
"argument n, but encountered " + n);
}
return (n === 0) ? xs : drop(tail(xs), n - 1);
}

global.array_test = array_test;
global.pair = pair;
global.is_pair = is_pair;
Expand All @@ -399,3 +417,5 @@ global.list_ref = list_ref;
global.accumulate = accumulate;
global.set_head = set_head;
global.set_tail = set_tail;
global.take = take;
global.drop = drop;