Skip to main content
rewrote paragraph about ⍨
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switchcommute operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argumentmodifies its behaviour, so it either swaps the arguments (A f⍨ AB ←→ AB f A. (It can also swap arguments:, hence "commute") or provides the same argument on both sides (A f⍨ BA ←→ BA f⍨f A, hence "switch", but that's irrelevant toa "selfie"). The latter form is used in this solution.)


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the commute operator. When it occurs to the right of a function, it modifies its behaviour, so it either swaps the arguments (A f⍨ B ←→ B f A, hence "commute") or provides the same argument on both sides (f⍨ A ←→ A f A, a "selfie"). The latter form is used in this solution.


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

removed now irrelevant paragraph about function trains
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)

So, /⍨ is a (derived) function and is a function. A "train" of two functions in isolation forms a so-called atop. An atop with a single argument is equivalent to composition: (f g)A ←→ f g A, so g is applied to A, then f is applied to the result.


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)

So, /⍨ is a (derived) function and is a function. A "train" of two functions in isolation forms a so-called atop. An atop with a single argument is equivalent to composition: (f g)A ←→ f g A, so g is applied to A, then f is applied to the result.


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)


Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

improved solution to bonus challenge
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)

So, /⍨ is a (derived) function and is a function. A "train" of two functions in isolation forms a so-called atop. An atop with a single argument is equivalent to composition: (f g)A ←→ f g A, so g is applied to A, then f is applied to the result.

 

Bonus: the shortest I could come up with

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is (∊⌽-1-⍳¨)⍳⎕1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), 11 charactersa vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)

So, /⍨ is a (derived) function and is a function. A "train" of two functions in isolation forms a so-called atop. An atop with a single argument is equivalent to composition: (f g)A ←→ f g A, so g is applied to A, then f is applied to the result.

Bonus: the shortest I could come up with is (∊⌽-1-⍳¨)⍳⎕, 11 characters.

APL, 4 characters

/⍨⍳⎕

How it works:

reads user input. As for output, APL by default prints the result from every line.

⍳n is the integers from 1 to n. Example: ⍳3 ←→ 1 2 3

/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC' ←→ 'AACCC'

is the switch operator. When it occurs to the right of a function invoked with a single argument, the switch operator provides it as both left and right argument, so f⍨ A ←→ A f A. (It can also swap arguments: A f⍨ B ←→ B f⍨ A, hence "switch", but that's irrelevant to this solution.)

So, /⍨ is a (derived) function and is a function. A "train" of two functions in isolation forms a so-called atop. An atop with a single argument is equivalent to composition: (f g)A ←→ f g A, so g is applied to A, then f is applied to the result.

 

Bonus:

6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)

⍳5 (iota five) is 1 2 3 4 5.

⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.

reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).

is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.

removed named function wrapper and reduced it to 4 chars, see comments
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87
Loading
named the function, as the author of this challenge requested in the comments
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87
Loading
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87
Loading