Skip to main content
Golfed 5 bytes
Source Link
DLosc
  • 40.7k
  • 6
  • 87
  • 142

Brachylog, 1111 6 bytes

-5 bytes thanks to Fatalize

ℤᵐ|{ċ|g}ᵐc↰ℤg|↰ᵐc

Try it online!Try it online!

Explanation

Brachylog has a flatten-list builtin, but 1) it only flattens by one level and 2) it only works on a list of lists, not a mixed-type list. So we need a recursive solution.

ℤᵐ|{ċ|g}ᵐc↰
 ᵐ           Each element of the listℤg|↰ᵐc         If the argument is an integer,
     g      wrap it (andin outputa thesingleton list unchanged)
  |          OROtherwise:
   {   }ᵐ↰ᵐ   Recurse Foron each element of the list,
    ċ        it is a list
             (and return it unchanged)
     |       OR
      g      wrap it in a singleton list
         c   Flatten this list of lists once
          and recurse onflatten the result once

Brachylog, 11 bytes

ℤᵐ|{ċ|g}ᵐc↰

Try it online!

Explanation

Brachylog has a flatten-list builtin, but 1) it only flattens by one level and 2) it only works on a list of lists, not a mixed-type list.

ℤᵐ|{ċ|g}ᵐc↰
 ᵐ           Each element of the list            is an integer
             (and output the list unchanged)
  |          OR:
   {   }ᵐ    For each element of the list,
    ċ        it is a list
             (and return it unchanged)
     |       OR
      g      wrap it in a singleton list
         c   Flatten this list of lists once
          and recurse on the result

Brachylog, 11 6 bytes

-5 bytes thanks to Fatalize

ℤg|↰ᵐc

Try it online!

Explanation

Brachylog has a flatten-list builtin, but 1) it only flattens by one level and 2) it only works on a list of lists, not a mixed-type list. So we need a recursive solution.

ℤg|↰ᵐcIf the argument is an integer,
 g      wrap it in a singleton list
  |     Otherwise:
   ↰ᵐ   Recurse on each element of the list
     c  and flatten the result once
Source Link
DLosc
  • 40.7k
  • 6
  • 87
  • 142

Brachylog, 11 bytes

ℤᵐ|{ċ|g}ᵐc↰

Try it online!

Explanation

Brachylog has a flatten-list builtin, but 1) it only flattens by one level and 2) it only works on a list of lists, not a mixed-type list.

ℤᵐ|{ċ|g}ᵐc↰
 ᵐ           Each element of the list
ℤ            is an integer
             (and output the list unchanged)
  |          OR:
   {   }ᵐ    For each element of the list,
    ċ        it is a list
             (and return it unchanged)
     |       OR
      g      wrap it in a singleton list
         c   Flatten this list of lists once
          ↰  and recurse on the result