-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
range
function to python and js implementations
- Loading branch information
Showing
6 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { pushRuntimeValueToStack } from "../stackManip"; | ||
import { BuiltinFunction, RuntimeValue } from "../types"; | ||
import { arity, validateType } from "../util"; | ||
|
||
const validateIsInteger = (value: RuntimeValue) => { | ||
const res = validateType("number", value); | ||
if (!Number.isInteger(value)) { | ||
throw new Error("Arguments to range must be integers"); | ||
} | ||
return res; | ||
}; | ||
|
||
const range: BuiltinFunction = arity([1, 2, 3], (args, stack, exec) => { | ||
let start = 0; | ||
let end; | ||
const target = []; | ||
if (args.length > 1) { | ||
start = validateIsInteger(exec(args[0], stack)); | ||
end = validateIsInteger(exec(args[1], stack)); | ||
} else { | ||
end = validateIsInteger(exec(args[0], stack)); | ||
} | ||
// Step size | ||
let step = 1; | ||
if (args.length > 2) { | ||
step = validateIsInteger(exec(args[2], stack)); | ||
} | ||
// Iteration Methods | ||
if (step === 0) { | ||
throw new Error("Range: Step size cannot be 0"); | ||
} else if (step > 0 && start < end) { | ||
for (let i = start; i < end; i += step) { | ||
target.push(i); | ||
} | ||
} else if (step < 0 && start > end) { | ||
for (let i = start; i > end; i += step) { | ||
target.push(i); | ||
} | ||
} else { | ||
return []; | ||
} | ||
return target; | ||
}); | ||
|
||
export default range; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters