g | x | w | all
Bytes Lang Time Link
042Juby250515T164503ZJordan
059Perl 5 a240221T000800ZXcali
016BQN240220T232207ZDLosc
019J210330T190932ZJonah
014Haskell + hgl220122T155559ZWheat Wi
011Uiua SBCS240220T173512Zchunes
215Desmos210409T015131ZAiden Ch
070Excel210402T115941ZAxuary
086Java JDK210401T130815ZOlivier
005Husk210330T221534ZDominic
064Python 2210331T103507Zqwatry
052Retina 0.8.2210331T092021ZNeil
070Red210331T070428ZGalen Iv
nan210330T184541ZUnmitiga
033Wolfram Language Mathematica210330T182736ZZaMoC
056JavaScript ES6210330T221444ZArnauld
004APL Dyalog Extended210330T214207ZAdá
055R210330T211204ZDominic
019Factor + math.unicode210330T212104Zchunes
003Pyth210330T202838Zhakr14
036Haskell210330T183936ZDelfad0r
068Python 3210330T192210ZManish K
046Scala210330T182554Zuser
062JavaScript V8210330T183054Zrydwolf
00105AB1E210330T182413ZCommand
001Jelly210330T182340Zcaird co

J-uby, 42 bytes

Takes curried arguments like F[to_trim][list].

:& &:include?|:+&:drop_while|~:|&:~|~:**&2

Attempt This Online!

Perl 5 -a, 59 bytes

$_=<>;for$i(0,-1){splice@F,$i,1while/(^| )$F[$i]\b/}say"@F"

Try it online!

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

J, 21 19 bytes

[#~1-(*/\.>.*/\)@e.

Try it online!

Haskell + hgl, 14 bytes

jn m<rv.^dW<fe

Try it online!

(.^ 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.

Uiua SBCS, 11 bytes

▽¬↥⇌∩\×⇌.∊,

Try it!

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!

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();}

Try it online!

Uses a LinkedList as input/output.

Husk, 7 5 bytes

Edit: -2 bytes thanks to Leo

‼(↔↓€

Try it online!

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.

Try it online!

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]

Try it online!

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]

Try it online!

Java, 89 bytes

a->b->a.dropWhile(b::contains).sorted((c,d)->-1).dropWhile(b::contains).sorted((c,d)->-1)

Try it online!

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);}

Try it online!

Wolfram Language (Mathematica), 33 bytes

#//.{e=#|##&@@#2,a___,e...}:>{a}&

Try it online!

-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))

Try it online!

APL (Dyalog Extended), 4 bytes (SBCS)

Anonymous infix function.

⌂deb

Try it online!

A library function: delete ending blanks using the left argument as a list of what is considered "blanks".

Notes, comments, and APL implementation.

R, 57 55 bytes

function(x,y,`+`=cumprod)x[!+x%in%y&!rev(+rev(x)%in%y)]

Try it online!

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 ]

Try it online!

Explanation:

It's a quotation (anonymous function) that takes two sequences as input and returns one sequence as output.

Pyth, 3 bytes

.sF

Try it here!

Haskell, 37 36 bytes

f v|g<-reverse.snd.span(`elem`v)=g.g

Try it online!

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

Try it online!

Scala, 46 bytes

s=>1.to(2)./:(_)((a,_)=>a.reverse dropWhile s)

Try it in Scastie!

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

Try it in Scastie!

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

05AB1E, 1 byte

Ú

Try it online!

A builtin which trims.

Jelly, 1 byte

t

Try it online!

A builtin that does exactly this. Works with multiple "falsey" values as well: Try it online!