-2
\$\begingroup\$

Codegolf challenge - mapping values in a multidimensional array

[
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

This 2D array is sick of being so false, and wants to find the truth about life, once and for all. Your task is to write a map function to act as a highway to all the values in this array.

An example implementation of this super function would be if the above array is defined as arr below:

arr.multimap(x => {
  return 1;
}

Which would result in the array being:

[
  [1, 1, 1],
  [1, 1, 1],
  [1, 1, 1]
]

This is a 2D number example, yours has to work in any dimension, and not be dimensionally-bias.


The rules

  • Must work in any type of dimensional array (1D, 2D, 3D, etc..)
  • Least bytes, wins!
  • Must only have one callback function which accounts for any level of dimensional interaction
  • Meta-languages get fewer points
  • Can simply be a function which applies the logic to an array given
  • Can be any arbitrary initial value, as long as you can replace it on-command
\$\endgroup\$

closed as unclear what you're asking by Jonah, Arnauld, mbomb007, xnor, HyperNeutrino Apr 23 at 19:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

3
\$\begingroup\$

JavaScript (Node.js), 35 27 17 bytes

-18 thanks to @Arnauld and @Shaggy

f=a=>a?a.map(f):1

console.log(f([
  [0, 0, 0],
  [0, [0, 0, 0], 0],
  [[0, 0, [0]], 0, 0]
]))

\$\endgroup\$
  • \$\begingroup\$ @Arnauld lol, you are right. Thanks! \$\endgroup\$ – Luis felipe De jesus Munoz Apr 23 at 12:59
  • 3
    \$\begingroup\$ 21 bytes \$\endgroup\$ – Arnauld Apr 23 at 13:20
  • 1
    \$\begingroup\$ @Arnauld: Or 17 bytes \$\endgroup\$ – Shaggy Apr 23 at 16:36
  • 1
    \$\begingroup\$ @Shaggy Yup, but it's still unclear if the initial values are guaranteed to be falsy (see the last rule). \$\endgroup\$ – Arnauld Apr 23 at 16:42
2
\$\begingroup\$

Jelly, 1 byte

Try it online!

Replaces n with n + 1.

Because Jelly things.

Note that [x] displays as x in Jelly (single-element lists don't show as lists); the internal data is still a single-element list. You can make it show using Python's str: Try it online!.

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 4 bytes

!<<*

Try it online!

Replaces 0s with True. Extremely short for a non-esolang.

   *  # From the input
!     # Boolean not 
 <<   # Each element recursively
\$\endgroup\$
1
\$\begingroup\$

Python 2, 29 26 25 bytes

-3 bytes thanks to @JonathanAllan

-1 byte thanks to @JoKing

f=lambda a:a<1or map(f,a)

Replaces 0 with True

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Python 2, 26 \$\endgroup\$ – Jonathan Allan Apr 23 at 17:42
  • \$\begingroup\$ @Neil the f= is necessary for a recursive function. \$\endgroup\$ – Jonathan Allan Apr 23 at 17:43
  • \$\begingroup\$ <[] can be <1 \$\endgroup\$ – Jo King Apr 24 at 11:43
0
\$\begingroup\$

Japt, 6 bytes

?¡ßX:1

Try it

¬Íª¡ßX

Try it

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 36 bytes

t=>JSON.stringify(t).replace(/0/g,1)

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.