Three under:
keys%{{map{$_,$_}@list}}
Update:
keys%{{@A,reverse@A}} # Then again...
keys%{{map{$_,$_}@A}} # Maybe it's a tie with tye.
Update 2:
keys%{{map{$_,1}@A}} # Oh duh! OK. One under. :-)
-sauoq
"My two cents aren't worth a dime.";
| [reply] [Watch: Dir/Any] [d/l] [select] |
Actually, that was about what I started with. I wasn't really 'golfing', more like 'goofing' -- wanting something short (sure), but also unusual/idiomatic, and either really fast or really slow. It went something like this:
keys%{map{$_=>1}@list}
"Way too conventional. Almost clear. And I don't like the 'map{}'."
keys%{map $_,0, @list}
"Better, except it doesn't work. That comma is a problem. How to get rid of it... Oh!"
keys%{map$_=>0,@list}
"Hrm, that's almost back were I started. D'oh! And it doesn't work, either. Ooh, I'll use 'x' instead!"
keys%{map$_ x2,@list}
"Oops, I mean..."
keys%{map ($_)x2,@list}
"Uhh... no. I hate that rule! I liked it better back when we didn't try to pretend that whitespace didn't sometimes have syntactic significance."
keys%{map+($_)x2,@list}
"Not bad. But I don't like 'func+(' much. Heck, I don't like 1-to-many map, anyway (it still feels 'slow' to me and yet isn't slow enough). Maybe I'll go for maximum speed, maximum functionality (preserve order and references)..."
do{my%h;grep!$h{$_}++,@list}
(Actually, I didn't get it nearly that tight before giving up) "Hrm, don't like that '@list' in the middle. Maybe..."
sub{my%h;grep!$h{$_}++,@_}->(@list)
"Er, that didn't help. Okay, forget 'maximum functionality'!"
keys%{@list[...]}
"But what to put for '...'? Too bad things like @list[0..-1] don't work..."
keys%{@x[0..$#x,1..$#x]}
"Hrm... And doesn't work for odd sizes..."
keys%{@x,reverse@x}
"'reverse' is such a long name for a function that can be so useful in golf. Oh, I'm not golfing. Yeah, that's not bad. It works for even and odds sizes but kinda 'for different reasons'. It might even be faster than a 1-to-many map version... maybe. Heck, let's see..."
...just type in a benchmark script directly into 'perl' since I have console line editing...
"Wow! It's 75% faster than even using the usually fastest @hash{@list}= () trick! Maybe it is worth posting."
...compose and preview post...
"Oh, if I'm going to publicly claim 'fastest', I should verify that the two solutions produce the same results... Huh? Why is that the empty list? Oh, '%{}' isn't enough, I need '%{{}}'!"
...rerun benchmark...
"D'oh! 50% slower... Ah, heck, it is still kinda goofy. I'll post it anyway."
And, oh, congratulations and ++sauoq! Nice golfing! (:
- tye (but my friends call me "Tye")
| [reply] [Watch: Dir/Any] [d/l] [select] |
Doesn't keys %{{@list}} only check every other item in the list (a hash being a list like key1, val1, key2, val2...)?
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] [d/l] [select] |
A hash needs both keys and values. the reverse ensures each element appears as a key, somewhere. Otherwise you lose half the elements.
--Dave
| [reply] [Watch: Dir/Any] [d/l] |