Rust, 29 bytes
|n,i,j|&n[i..<[_]>::len(n)-j]
Call it as follows:
let a = &[1, 2, 3, 4, 5, 6];
let f = |n,i,j|&n[i..<[_]>::len(n)-j];
f(a, 2, 1)
I had a lot of fun fighting with the borrow checker figuring out what the shortest approach was in order to have it infer the lifetime of a returned slice. Its behavior around closures is somewhat erratic, as it will infer the lifetimes, but only if you do not actually declare the parameter as a reference type. Unfortunately this conflicts with being required to define the argument type in the signature as the n.len method call needs to know the type it's operating on.
Other approaches I tried working around this issue:
fn f<T>(n:&[T],i:usize,j:usize)->&[T]{&n[i..n.len()-j]} // full function, elided lifetimes
let f:for<'a>fn(&'a[_],_,_)->&'a[_]=|n,i,j|&n[i..n.len()-j] // type annotation only for lifetimes. Currently in beta.
|n:&[_],i,j|n[i..n.len()-j].to_vec() // returns an owned value
|n,i,j|&(n as&[_])[i..(n as&[_]).len()-j] // casts to determine the type
|n,i,j|&(n:&[_])[i..n.len()-j] // type ascription (unstable feature)
|n,i,j|{let b:&[_]=n;&b[i..b.len()-j]} // re-assignment to declare the type
2What, exactly, does it mean to "remove" values from an array—especially to remove them from the end? In languages like C, where an array is just a pointer to the first element and a length, can we just change the length to truncate the array? That's what would normally be done in real-world programming, but the challenge is unclear to me. – Cody Gray – 2017-06-26T16:40:56.837
@CodyGray Removing values from the array is what it should look like, but not necessarily what goes on behind the scenes. – Okx – 2017-06-26T16:42:45.927
4What do you mean by "look like"? Arrays don't have a look – it is all behind the scenes! – Cody Gray – 2017-06-26T16:43:19.537
@CodyGray It doesn't matter. – Okx – 2017-06-26T16:43:38.603
Due to the multitude of answer, you can maybe add a leaderboard snippet to your question? – Michthan – 2017-06-27T04:56:32.200
1
@Michthan Try installing the PPCG userscript
– Okx – 2017-06-27T10:59:11.8232@Okx Nope, that's very buggy, I'd recommend adding a leaderboard. – Erik the Outgolfer – 2017-06-27T11:01:30.677
@EriktheOutgolfer It's been working pretty well for me. – Okx – 2017-06-27T11:03:56.920
@Okx Try to use TIO provided by the userscript. – Erik the Outgolfer – 2017-06-27T11:05:37.380
@EriktheOutgolfer Well that doesn't work, but the leaderboard does. – Okx – 2017-06-27T11:11:08.260
@Okx Also, please stop being lazy...:P...btw the userscript also has unwanted side-effects. – Erik the Outgolfer – 2017-06-27T11:11:54.467
@EriktheOutgolfer You do know that there's nothing stopping you from using the edit button, right? ;)
– Sanchises – 2017-06-27T13:28:30.087@Sanchises I'm not sure if OP wants a leaderboard though. – Erik the Outgolfer – 2017-06-27T13:30:28.263
Does there need to be a space between the returned values? – BruceWayne – 2017-06-27T18:34:35.163
@BruceWayne No, it just needs to be some sort of integer list. – Okx – 2017-06-27T18:44:52.570