| Bytes | Lang | Time | Link |
|---|---|---|---|
| 042 | Juby | 250515T164503Z | Jordan |
| 059 | Perl 5 a | 240221T000800Z | Xcali |
| 016 | BQN | 240220T232207Z | DLosc |
| 019 | J | 210330T190932Z | Jonah |
| 014 | Haskell + hgl | 220122T155559Z | Wheat Wi |
| 011 | Uiua SBCS | 240220T173512Z | chunes |
| 215 | Desmos | 210409T015131Z | Aiden Ch |
| 070 | Excel | 210402T115941Z | Axuary |
| 086 | Java JDK | 210401T130815Z | Olivier |
| 005 | Husk | 210330T221534Z | Dominic |
| 064 | Python 2 | 210331T103507Z | qwatry |
| 052 | Retina 0.8.2 | 210331T092021Z | Neil |
| 070 | Red | 210331T070428Z | Galen Iv |
| nan | 210330T184541Z | Unmitiga | |
| 033 | Wolfram Language Mathematica | 210330T182736Z | ZaMoC |
| 056 | JavaScript ES6 | 210330T221444Z | Arnauld |
| 004 | APL Dyalog Extended | 210330T214207Z | Adá |
| 055 | R | 210330T211204Z | Dominic |
| 019 | Factor + math.unicode | 210330T212104Z | chunes |
| 003 | Pyth | 210330T202838Z | hakr14 |
| 036 | Haskell | 210330T183936Z | Delfad0r |
| 068 | Python 3 | 210330T192210Z | Manish K |
| 046 | Scala | 210330T182554Z | user |
| 062 | JavaScript V8 | 210330T183054Z | rydwolf |
| 001 | 05AB1E | 210330T182413Z | Command |
| 001 | Jelly | 210330T182340Z | caird co |
J-uby, 42 bytes
Takes curried arguments like F[to_trim][list].
:& &:include?|:+&:drop_while|~:|&:~|~:**&2
BQN, 16 bytes
(⌽(∨`·∧˝≠⌜)/⊢)⍟2
Takes the list as its right argument and the elements to trim as its left argument. Try it at BQN online!
Explanation
(⌽(∨`·∧˝≠⌜)/⊢)⍟2
≠⌜ Make an inequality table of the two lists: 0 when elements are
equal, 1 when they aren't
∧˝ Fold each column on logical AND: 1 for elements in the list
that aren't one of the elements to trim, 0 for those that are
∨`· Scan with logical OR: keep leading run of 0s but turn everything
after the first 1 into 1s
( )/⊢ Remove elements of the original list that correspond to 0s in
that list; keep elements that correspond to 1s
⌽ Reverse
( )⍟2 Do that twice
Haskell + hgl, 14 bytes
jn m<rv.^dW<fe
(.^ has been renamed to << since this was posted)
Explanation
To start we want to do the much simpler:
f x y=rv$dW(fe x)$rv$dw(fe x)y
This drops all things in x off the front, reverses it, then drops all things in x off the new front and reverses it back.
From here we can start by removing the y.
f x=rv<dW(fe x)<rv<dw(fe x)
Now we have the expression rv<dW(fe x) twice so let's fix that. jn will take a function on two arguments and pass the same value as both of its arguments.
f x=(rv<dW(fe x))<(rv<dW(fe x))
f x=jn m(rv<dW(fe x))
Now x is at the end so we can eta-reduce it away as well:
f x=jn m<rv.^dW<fe
Reflection
Usually I would use Rv to reverse a list however .^ next to Rv breaks the Haskell parser, so I had to use rv instead. I've been aware of this danger for a while now and this was a pretty close call. I will probably rename .^ to <<.
"Compose a function with itself" achieved here with jn m is something that should probably have it's own name. It would have saved a byte here at least.
Desmos, 227 215 bytes
z(A)=length(A)
h(l,a,k)=\left\{l[a]=k:0,1\right\}
f(a,b)=\sum_{n=1}^{z(b)}(1-h(b,n,a))
s=f(A,B)
j(A,B)=A[\sum_{N=1}^{z(s)}N(1-h(s,N,0))\prod_{a=1}^{N-1}h(s,a,0)...z(A)]
p=j(A,B)
q=j(p[z(p)...1],B)
m(A,B)=q[z(q)...1]
Try It On Desmos!(Prettified Version)
Final function is \$m(a,b)\$, where \$a\$ is the list that is being trimmed, and \$b\$ is the list of "falsey" values that are trimmed from \$a\$.
Much harder than I initially anticipated because Desmos has limited list functionality. Hopefully it's fine if I output an one-element list with the one element being undefined([undefined]) when the output is supposed to be an empty list([]). If it isn't fine then tell me and I can fix(but it would cost more bytes ☹️)
Note: Not too sure why, but you can't directly paste the code into Desmos for some reason. You have to paste each expression one at a time in order for it to work.
Excel, 70 bytes
=LET(x,A1#,r,ROW(x),y,IF(x>-1,r,""),FILTER(x,(r>=MIN(y))*(r<=MAX(y))))
Java (JDK), 86 bytes
l->b->{while(b.contains(l.peek()))l.pop();while(b.contains(l.getLast()))l.pollLast();}
Uses a LinkedList as input/output.
Husk, 7 5 bytes
Edit: -2 bytes thanks to Leo
‼(↔↓€
How?
↔↓€ removes the falsy elements from one end of the list, and then flips it:
↓ # drop the longest prefix of arg2 with elements that satisfy:
€ # are present in arg1
↔ # then reverse the result
So we just need to do this twice (to remove from both ends of the list, and to restore the list to its original orientation):
( groups ↔↓€ together (the parentheses are automatically closed at the end of the program), and the higher-order function ‼ runs a function provided as an argument twice.
Python 2, 64 bytes
a,f=input()
for i in-1,0:
while a and a[i]in f:a.pop(i)
print a
The a and is necessary if the falsey list, f, contains all the elements of a and the completely stripped list will be empty. Without it, the program errors with IndexError: list index out of range.
Retina 0.8.2, 52 bytes
m`^|$
,
(^)?,(([^,]+,)(?=.*¶.*,\3))*(¶.*)?(?(1)|$)
Try it online! Explanation:
m`^|$
,
Wrap both lists in additional commas.
(^)?,
Record whether this match is at the very start, but at the very least start at a comma.
(([^,]+,)(?=.*¶.*,\3))*
Match any number of terms which exist in the second array.
(¶.*)?
Optionally match the second array.
(?(1)|$)
If the match did not begin at the start, then it must end at the end.
Delete the matching values.
Red, 70 bytes
func[b t][while[find t b/1][take b]while[find t last b][take/last b]b]
Using parse, 123 bytes
func[b t][r: copy[]foreach n t[append r reduce['quote n '|]]take/last r
parse b[remove some r to[any r end]remove some r]b]
Java, 89 bytes
a->b->a.dropWhile(b::contains).sorted((c,d)->-1).dropWhile(b::contains).sorted((c,d)->-1)
Java, 125 bytes
a->b->{int i=-1,j=a.length;for(;b.contains(a[++i]););for(;b.contains(a[--j]););return java.util.Arrays.copyOfRange(a,i,j+1);}
Wolfram Language (Mathematica), 33 bytes
#//.{e=#|##&@@#2,a___,e...}:>{a}&
-6 bytes and correction from @att
JavaScript (ES6), 56 bytes
Expects (a)(b), where a is the array to trim, using the values in the set b.
a=>b=>(g=a=>a.reverse().filter(i=c=>i|=!b.has(c)))(g(a))
APL (Dyalog Extended), 4 bytes (SBCS)
Anonymous infix function.
⌂deb
A library function: delete ending blanks using the left argument as a list of what is considered "blanks".
R, 57 55 bytes
function(x,y,`+`=cumprod)x[!+x%in%y&!rev(+rev(x)%in%y)]
Finds matching elements (x %in% y) as vector of TRUE/FALSE values, and calculates the cumulative product (cumprod, re-assigned to + here to save bytes) to convert everything after the first FALSE to zero. Then does the same from the other end (by reversing x and the cumprod). Finally, negates (!) to select only the non-terminal-matching elements of x.
Factor + math.unicode, 19 bytes
[ '[ _ ∈ ] trim ]
Explanation:
It's a quotation (anonymous function) that takes two sequences as input and returns one sequence as output.
'[ _ ∈ ]Push a fried quotation to the data stack to be used later bytrim. Whatever is on top of the data stack gets slotted into the quotation at the_. This will be the list of elements to trim.trimTake a sequence and a quotation and apply the quotation to elements on both ends of the sequence, removing elements for which the quotation returnst. Stop whenfis encountered.∈Take an object and a sequence and returntif the object is in the sequence. Shorthand formember?.
Haskell, 37 36 bytes
f v|g<-reverse.snd.span(`elem`v)=g.g
- -1 byte thanks to xnor.
How?
f v -- define a function f that takes as input a list v of falsey values
|g<- -- inside the definition, define a new function g that...
reverse -- reverses...
.snd -- the second element of...
.span -- a tuple (say (x,y)) where x is the longest prefix of elements that...
(`elem`v) -- belong to v...
-- and y is the rest of the list
=g.g -- f is defined as the composition g.g (basically apply g twice)
Python 3, 68 bytes
f=lambda a,b:a[-1]in b and f(a[:-1],b)or a[0]in b and f(a[1:],b)or a
Scala, 46 bytes
s=>1.to(2)./:(_)((a,_)=>a.reverse dropWhile s)
s => //The Set of falsy items
1.to(2) //Make a range [1, 2] to trim twice
./: //Fold over it,
(_) //starting with the list to trim
((a, _) => //The list a and the number, which is ignored
a.reverse //Reverse the list
dropWhile //And drop while
s) //Each item is present in s
Same length, but more boring
s=>_.dropWhile(s).reverse.dropWhile(s).reverse
Boring.
JavaScript (V8), 78 62 bytes
-16 bytes thanks to @Original Original Original VI
(a,s,g=x=>s.includes(x[0])?g(x.slice(1)):x.reverse())=>g(g(a))
Old:
a=>n=>(t=z=>z.findIndex(d=>!n.includes(d)),a.slice(t(a),-t([...a].reverse())))
Can probably be shortened by a ton
Jelly, 1 byte
t
A builtin that does exactly this. Works with multiple "falsey" values as well: Try it online!