lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>> Second, support for 'string lambdas' would be cool:
>>
>> _.({1,2,3,4}):chain():map '|i| i + 1' :select '|i| i%2==0': value()
>>
>> There's a slight hit to compile them, but thereafter they can be
>> easily memoized. They don't require any syntax enhancements so David K
>> can only have style objections ;)
>
> This is an interesting idea, I'd have to either:
> 1) insert a 'return' so it would limit the string lambda to a single
> expression
> 2) have a complex parser
> 3) require the 'return' to be explicit (e.g. map '|i| return i + 1')
> I'll play around with this idea, if anybody wants to submit any code I'd be
> happy to take a look.


I have code:

-------
function f(str)
	return assert(loadstring(
		("return function %s return %s end")
		:format(str:match("([^=]+)=(.*)")
		))())
end

g = f "(x) = x*2"

print(g(2))
-------

It's a fairly simple string transformation. Of course, you lose local
scoped variables ... but for your cause, it could help. Further
transformations could be to insert values from variables via table
declaration, but that might be overly confusing. It should be quite
useful for filtering, ie.

...:filter(f "(x) = x < 0"):...

Eike