Last active
March 18, 2017 23:46
-
-
Save lubien/17af35e99f944b2df9290c7e4d2485e4 to your computer and use it in GitHub Desktop.
Given an index `n` and an array, return a new array with the nth value on the head
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
const clamp = l => h => x => | |
x > h | |
? h | |
: x < l | |
? l | |
: x | |
const | |
ascend = i => xs => { | |
let | |
idx = clamp(0)(xs.length - 1)(i) | |
return [ | |
xs[idx], | |
...xs.slice(0, idx), | |
...xs.slice(idx + 1), | |
] | |
} | |
ascend(200)([1, 2, 3, 4, 5]) |
beatorizu
commented
Mar 18, 2017
Bem notado. Esqueci de checar se o índice é válido.
Edit: done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment