60 points · by EmanueleMinotto · 13y ago · 57 comments
It's hilarious looking at how cryptic these one-liners are, particularly when I consider that with languages like Ruby and Python you can do a LOT with a tiny little line if code.
As a side-note, one of my favorites is summing an array in Ruby:
x = [1, 2, 3, 4, 5]
x.inject &:+
=> 15 array_sum([1, 2, 3, 4, 5]);For example, given a list of numbers, you can generate a bitwise OR mask easily:
[1,2,3,4,5].inject(:|)
What this does is iterate over the list and apply ($last_result | $current_element) and return the result, which is passed on as $last_result to the next iteration. $last_result is 0 by default. This is equivalent to (as of PHP 5.4): array_reduce([1,2,3,4,5], function($v, $e) { return $v | $e; }, 0);
Or prior to PHP 5.3: function or_mask($v, $e) { return $v | $e; }
array_reduce(array(1,2,3,4,5), "or_mask", 0);
It's doable in both languages, but Ruby's functional language lineage and its object-oriented nature results in an exceptionally clean approach. array_reduce(array(1,2,3,4,5), create_function('$v, $e', 'return $v | $e;'), 0);The problem with array_sum is not that it's too long but instead that it's combined two rather specific bits of functionality into a fixed (if common) form, while the Ruby code separates the ideas of "folding" and "addition" allowing for many orthogonal creations.
To pick another favorite, in Haskell you could write
Foldable.foldl1 (+)
which sums any foldable thing—anything which has elements which can be combined together one-by-one. So it'll apply to Trees or Sets or Dictionaries (well, Maps) just as easily while also allowing things like (+) to be replaced by other binary operators.Edit: Screw it, haven't self promoted on a while. In Pharen (https://github.com/scriptor/pharen), which compiles to PHP, you could do:
(reduce (+) 0 [1 2 3 4 5]) foreach(array(1,2,3,4,5) as $x) $i=$i+$x; echo $i;2. ?
3. Profit.
(Added in PHP 6)
irb(main):001:0> x = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> x.inject &:*
=> 120 [+] ^6
Actually cheated slightly because ^6 creates list from 0..5 :)So correct match would be...
[+] 1..5
Here's how you would do it in Io: list(1, 2, 3, 4, 5) reduce(+)
In Rebol there isn't a functional fold/reduce built in. So one way to do the one-liner would be: x: [1 2 3 4 5] forskip x 2 [insert x '+] x: do next x
Above changes x to 15. Alternatively approach which doesn't clobber x: x: [1 2 3 4 5] y: [] forall x [repend y [x/1 '+]] take/last y do y
I'm sure someone with more Rebol chops can see other (better!) ways of doing this :) x ← 1 2 3 4 5
+/ x
http://ngn.github.io/apl/web/index.html#code=x%20%u2190%201%... x.inject(0, &:+)
That's one of the disadvantages of not having a powerful type system to infer what's the "zero" of some type.Still, the Ruby code is pretty readable and a good example of orthogonal concepts that compose well. E.g., getting the product of the numbers instead: x.inject(1, &:*). Or the least common multiple: x.inject(&:lcm).
I'm sorry i couldn't fight the urge to FIFY :( ...
here are some great ones:
http://fredrikolofsson.com/f0blog/?q=node/478
play{a=SinOscFB;sum({|i|a.ar(a.ar(a.ar(a.ar(i+1,1/9,999),1/9,a.ar(1/9,1,1/9)),a.ar(0.1,3),i+2999),a.ar(1/9,1/9),1/9)}!9)!2}//#SuperCollider
r{loop{x={GVerb.ar(MoogFF.ar(ClipNoise.ar
0.4,LFPar.kr({0.3.rand}!2,0,600,990)),9,9,1)}.play(s,0,19);3.wait;x.release}}.play//#SuperCollideraudio examples on the page above
play{a=LFPulse;b=(1..4);Mix(a.ar(a.ar(a.ar(a.ar(b/32)+1/8)+1b)+(Mix(a.ar(b/64))+a.ar(4/b)(a.ar(a.ar(b/8))2+b))100))/8!2}//#SuperCollider
audio:
http://fredrikolofsson.com/f0blog/files/audio/tweet0020.mp3
"this tweet is also totally deterministic and without any randomness. here a lot of nested square wave oscillators creates the complexity. basically there are 4 channels/voices mixed down to one and then duplicated in left and right channel. there are three levels deep nesting of frequency modulation with another set of square waves mixed and added."
One of the sc people released an iPhone app that let's you write bitshift based melodies.
The early stuff that supercolliders creator James McCartney did really set the style for short, mathematically elegant music. It's really a great language for it. You can do operations on arrays, and do interesting multichannel expansion.
A while ago, I put together a dirt-simple URL shortener https://twitter.com/eksith/statuses/275709375665037312
A bit of background: http://eksith.wordpress.com/2012/12/03/id-obfuscation
Here's a fun(!) presentation of Rebmu given by Dr. Rebmuthalamonious Golfstanipetrovitch (!!) given at the recent Rebol/Red conference - http://www.youtube.com/watch?v=iDKaz1iB9wQ
Enjoy! :)
Well, maybe someone could aim to get many sequential keys and then reverse-engineer the state of the cipher, but that's also possible with your method.