lua-users home
lua-l archive

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


On Mon, Jun 25, 2012 at 12:46 PM,  <meino.cramer@gmx.de> wrote:
> Coda Highland <chighland@gmail.com> [12-06-25 19:44]:
>> On Mon, Jun 25, 2012 at 12:34 PM,  <meino.cramer@gmx.de> wrote:
>> > Hi,
>> >
>> > (Warning! Newbie ahead!)
>> >
>> > With UNIX systems you can build chains of cammands
>> > on the commandline eacht connected via stdout/stdin.
>> >
>> > Is it possible to chain lua-functions in a similiar way, that is:
>> > To create an output stream with one function which gets the
>> > input stream of another function?
>> >
>> > Is taht possible (portable...)?
>> >
>> > Thank you very much in advance for any help!
>> > Best regards,
>> > mcc
>> >
>>
>> Why wouldn't it be?
>>
>> function a(x)
>>     return x .. "a"
>> end
>>
>> function b(x)
>>     return x .. "b"
>> end
>>
>> print(b(a("t")) -- "tab"
>>
>> Or if you like it better:
>>
>> local l = { a, b }
>> local value = "t"
>> for _, fn in pairs(l) do
>>     value = fn(value)
>> end
>> print(value) -- "tab"
>>
>> /s/ Adam
>>
>
> This is sequencing, not pipeing...
>
> Pipeing means: Function a produces a _stream_ of data
> /while/ function b consumes that stream.
>
> Is it possible (portable...) ?
>
> Best regards,
> mcc
>

Ah, look at coroutines for that. It's a bit much to drop a quick
example here, but that's your solution.

/s/ Adam