Skip to content

Instantly share code, notes, and snippets.

@lubien
Last active March 18, 2017 23:46
Show Gist options
  • Save lubien/17af35e99f944b2df9290c7e4d2485e4 to your computer and use it in GitHub Desktop.
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
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
Copy link

function ascend(head_index, list) {
  if (head_index < list.length && head_index >= 0) {
    var new_list = [];
    return new_list.concat(list[head_index], list.slice(0, head_index), list.slice(head_index + 1));
  }
}

@lubien
Copy link
Author

lubien 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