lazy evals + parameter passing

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Mon Sep 4 09:19:11 PDT 2006


Hi, I've been away for a while working with Ruby.

It seems the D spec is now extended with lazy evaluation of function
arguments. Very nice indeed.

I tried to search the ng but haven't seen any mention about 'yield' in
the context of lazy evals. Currently I think the implementation is only
half ready or then I've forgotten how to program in D :) See, some Ruby here

def inject(list, n)
 list.each { |v| n = yield(n, v) }
 return n
end

def sum(list)
 return inject(list, 0) { |n, v| n + v }
end

sum( [1,2,3] )

=> 6

If I understand this correctly, currently D only supports yield's
without arguments when used with anonymous delegates (Ok, it's lacking
multiple return values too, but those can be simulated easily). The
point is that the caller _can_ actually send parameters to the delegate,
but there's no way to receive arguments from the sender. Is it too
expensive to extend current lazy evals with arguments in a systems
programming language like D? Does it even require the use of tuples?

I think it's fully possible by introducing |par1, ..., parn | syntax so that

{ | a, b | a+b }

would be the same as

int _anonymous(inout a, inout b) {
  return a+b;
}

What do you think? Does it look ugly? Doesn't it offer a great deal of
new functionality. I think we really NEED this - please Walter, make it
pre-1.0 :)



More information about the Digitalmars-d mailing list