raganwald
(This is a snapshot of my old weblog. New posts and selected republished essays can be found at raganwald.com.)

Friday, October 26, 2007
  map(&"(1.._).inject(&'*')")


Some working Ruby code:

(1..3).map(&'*2') => [2, 4, 6]
(1..3).map(&'[-1, _, 0]') => [[-1, 1, 0], [-1, 2, 0], [-1, 3, 0]]
(1..3).map(&'x -> y -> x * y').map(&'[2]') => [2, 4, 6]
(1..5).select(&'>2') => [3, 4, 5]
(1..3).map(&'x -> y -> x * y').map(&'.call(2)') => [2, 4, 6]
[5].map(&"(1.._).inject(&'*')") => [120]


See String#_to_proc.

Labels: ,

 

Comments on “map(&"(1.._).inject(&'*')"):
That's some pretty cool stuff, but I can't see what's happening here:

(1..3).map(&'x -> y -> x * y').map(&'[2]') => [2, 4, 6]
 
(1..3).map(&'x -> y -> x * y').map(&'[2]') => [2, 4, 6]

x -> y -> x * y:

y -> x * y produces a function that multiplies some number, x times a parameter, y.

x -> ... produces a function with a parameter, x.

x -> y -> x * y produces a function with a parameter, x. It returns a function that multiplies x by a parameter, y.

So 1..3).map(&'x -> y -> x * y') produces an array of three functions, one that multiplies by one, one that multiplies by two, and one that multiplies by three.

[2] produces a function that takes its argument and calls the [] method passing two.

For procs in Ruby, [] is a synonym for call.

So .map(&'[2]') actually takes each function in the list and calls it with the parameter 2.

Thus... [2, 4, 6] is multiplying two by 1, 2, and 3.
 
It looks like 'x -> y -> x * y'.to_proc returns a curried function (proc) with 1...3 already applied to x (in the first map), and the second map then goes about to apply the 2 to y for each of the partial functions.

...I think.
 
Just for giggles, the new VB syntax for that.

Dim a = From i In Sequence(1, 3) Select i * 2
Dim b = From i In Sequence(1, 3) Select New Integer() {-1, i, 0}
Dim c = From i In Sequence(1, 3) Select x = Function(y) i * y Select x(2)
Dim d = From i In Sequence(1, 5) Where i > 2
Dim e = From i In Sequence(1, 3) Select x = Function(y) i * y Select x(2)
Dim f = (From i In Sequence(5, 5) Select seq = Sequence(1, i) Select Aggregate j In seq Into Product())
 
Thanks Reg and rubyruy, those are good explanations. In fact, I thought the post after this one made it more clear as well.
 




<< Home
Reg Braithwaite


Recent Writing
Homoiconic Technical Writing / raganwald.posterous.com

Books
What I‘ve Learned From Failure / Kestrels, Quirky Birds, and Hopeless Egocentricity

Share
rewrite_rails / andand / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo

History
06/04 / 07/04 / 08/04 / 09/04 / 10/04 / 11/04 / 12/04 / 01/05 / 02/05 / 03/05 / 04/05 / 06/05 / 07/05 / 08/05 / 09/05 / 10/05 / 11/05 / 01/06 / 02/06 / 03/06 / 04/06 / 05/06 / 06/06 / 07/06 / 08/06 / 09/06 / 10/06 / 11/06 / 12/06 / 01/07 / 02/07 / 03/07 / 04/07 / 05/07 / 06/07 / 07/07 / 08/07 / 09/07 / 10/07 / 11/07 / 12/07 / 01/08 / 02/08 / 03/08 / 04/08 / 05/08 / 06/08 / 07/08 /