-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\$
1
  • \$\begingroup\$ Comments are not for extended discussion. This conversation has been moved to chat. \$\endgroup\$
    – DJMcMayhem
    Apr 23, 2019 at 21:54

6 Answers 6

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\$
4
  • \$\begingroup\$ @Arnauld lol, you are right. Thanks! \$\endgroup\$ Apr 23, 2019 at 12:59
  • 3
    \$\begingroup\$ 21 bytes \$\endgroup\$
    – Arnauld
    Apr 23, 2019 at 13:20
  • 1
    \$\begingroup\$ @Arnauld: Or 17 bytes \$\endgroup\$
    – Shaggy
    Apr 23, 2019 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, 2019 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\$
3
  • \$\begingroup\$ Python 2, 26 \$\endgroup\$ Apr 23, 2019 at 17:42
  • \$\begingroup\$ @Neil the f= is necessary for a recursive function. \$\endgroup\$ Apr 23, 2019 at 17:43
  • \$\begingroup\$ <[] can be <1 \$\endgroup\$
    – Jo King
    Apr 24, 2019 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.