good chemistry is complicated,and a little bit messy -LW PerlMonks

### Golf: Indici to all sectors of an xD space

by jeroenes (Priest)
 on Oct 10, 2001 at 21:28 UTC ( #118061=perlmeditation: print w/ replies, xml ) Need Help??

I hope you like this one, as it is my ever-first attempt at a golf.

The challenge is to create a 2D-array that contains indici of all sectors in a multi-dimensional space. The indici are given as unitary vectors in that space.

Eg., in a flat plane (2D), there are four sectors, so we have 4 vectors:

```  -1   -1
-1    1
1   -1
1    1
that point each to a quadrant:
```          |
(-1, 1)  |  ( 1, 1)
|
-------------------
|
(-1,-1)  |  ( 1,-1)
|
This of course comes handy when you want mirror some image or curve.

The constraints are: (i) strict compliant, (ii) return a reference to an 2D-array and (iii) only the code inside the sub is counted (believe the last is standard, isn't it?). Furthermore: the vectors must be unitary vectors in the inner dimension. The function takes as input a scalar holding the number of dimensions disired.

Here is a starting point at 79 chars:

```sub iterate_dim{
[map[map\$_*2-1,split"",\$_],map{substr unpack("B*",chr\$_-1),-\$_[0]}1..2
+**\$_[0]]
}

#and call it:
my \$AoA = iterate_dim( 2 );

Have fun,

Jeroen
"We are not alone"(FZ)

Comment on Golf: Indici to all sectors of an xD space
Replies are listed 'Best First'.
Re (tilly) 1: Golf: Indici to all sectors of an xD space
by tilly (Archbishop) on Oct 10, 2001 at 21:55 UTC
46 characters.
```sub h {
@;=[];@;=map{[@\$_,1],[@\$_,-1]}@;for 1..pop;\@;
}
Note that strict compliance bought absolutely no safety here, I just got around it with using a global punctuation variable. But I would have used a global punctuation variable anyways, it allows me to save a space in front of the for. Therefore I consider strict compliance in golf to be a useless constraint. It is too easily satisfied in point while disregarded in spirit.
I can't beat this, but here's strict in spirit at 46:  ```sub h { \$_[0]?[map{[@\$_,1],[@\$_,-1]}@{h(-1+pop)}]:[[]] } [download]```
```   MeowChow
s aamecha.s a..a\u\$&owag.print```
OK, strict in spirit at 44.
```sub h{
#23456789_123456789_123456789_123456789_1234
eval'['.('map{[@\$_,1],[@\$_,-1]}'x pop).'[]]'
}
UPDATE
Well strict and warnings compliant in 43:
```sub h{
#23456789_123456789_123456789_123456789_123
[eval(('map{[@\$_,1],[@\$_,-1]}'x pop).'[]')]
}
and then strict alone in 39:
```sub h{
#23456789_123456789_123456789_123456789
[eval'map{[@\$_,1],[@\$_,-1]}'x pop.'[]']
}
UPDATE 2
Warnings as well in 40:
```sub h{
#23456789_123456789_123456789_123456789_
[eval'map{[@\$_,1],[@\$_,-1]}'x\$_[0].'[]']
}
Re: Golf: Indici to all sectors of an xD space
by trantor (Chaplain) on Oct 11, 2001 at 02:47 UTC

35 characters, both warnings and strict compliant:

```#        1         2         3
#2345678901234567890123456789012345
[map{eval"[\$_],"}glob'{-,}1,'x pop]

I used the approach discussed in Web-Safe Color Chart

-- TMTOWTDI

Really cute. Unportable under 5.005, but portable on 5.6. However one technical note, you can reduce it to 29:
```sub h {
#        1         2
#2345678901234567890123456789
[map[eval],glob'{-,}1,'x pop]
}
Nice... since I've had a hand passing that glob trick around perhaps I should attempt to shave a few chars off this tricky entry....

30 chars... passes warnings and strict: (though not necessarily taint compliant)

```#        1         2         3
#23456789012345678901234567890
[map{[eval]}glob'{-,}1,'x pop]

Doh! tilly was quicker, and beat me by a char (of course, I originally learned the glob trick from him, so perhaps thats how it should be...)

-Blake

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://118061]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2016-05-04 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (79 votes). Check out past polls.