1. What is process_lines? and general complaint on the new manual
- Posted by lionel Jan 19, 2011
- 2175 views
Hi everyone, longtime Euphoriac here just getting started with 4.0.
I'd like to know, what is process_lines() for and how to use it?
I'm confused by the example given in the manual. process_lines() is a function yet in the example there is no result assigned, as if it was a procedure.
...
In general, the new manual does not clearly show what a function returns. The Eu3.x manual showed this easily, like so:
Syntax: s2 = append(s1, x)
We see at a glance that append() takes a sequence and an object and returns a sequence, and then a detailed explanation followed.
I wish the manual could be like this. An html version rather than (or in addition to) a pdf would be nice too.
Thanks for reading.
Lionel
2. Re: What is process_lines? and general complaint on the new manual
- Posted by bill Jan 19, 2011
- 2100 views
Lionel:
Process_lines() returns an object.
This can be ignored.
IE if you do process_lines() for the side-effect of running it, in this case appemding x to s1 then it is ceratinly convenient to be able to ignore what is returned. Returning an object is handy because everything is an object.
I guess the reason why Eu4 doesn't specify return values is because whether they are atoms or sequences is irrelevant if they can be ignored.
Eitheruse the reutrn value or ignore it.
3. Re: What is process_lines? and general complaint on the new manual
- Posted by ChrisB (moderator) Jan 19, 2011
- 2163 views
Hi
It used to be that all functions required a return value, or else an error would be thrown. Now, when you call a function, you don't need a return value, so
function foo() return 1 end function integer x x = foo() -- ok eu 3.xx, and 4.xx foo() --throws and error with eu3, but is ok with 4
Kind of makes the procedure redundant, makes no real difference with speed (AFAIK), and seems to be being maintained for backwards compatability.
On the subject of the manual, I find it readable, and clear (see the section where it says 'Returns :'), I think the problem is with the explanation of the function itself. I would probably avoid it myself (personally), as, for me, it reduces clarity of the code (others may / will disagree). Perhaps a 'simpleton' (stress for the likes of me too!) explanation would be of benefit. To me, the example is not of much help.
-- Format each supplied line according to the format pattern supplied as well. function show(sequence aLine, integer line_no, object data) writefln( data[1], {line_no, aLine}) if data[2] > 0 and line_no = data[2] then return 1 else return 0 end if end function -- Show the first 20 lines. process_lines("sample.txt", routine_id("show"), {"[1z:4] : [2]", 20}) --^^^^^^^^^^^^^^^^^^ a better explanation of this
The function show(), seems to be writing to a file, rather than showing it, and where does the 'first 20 lines' come from?
Chris
4. Re: What is process_lines? and general complaint on the new manual
- Posted by lionel Jan 20, 2011
- 2055 views
It used to be that all functions required a return value, or else an error would be thrown. Now, when you call a function, you don't need a return value ... Kind of makes the procedure redundant ...
I see. May I ask the reasoning behind this change? Knowing nothing about language design, it's not obvious to me.
...
I am under the impression that process_lines() is like read_lines(), but with user-defined mojo. Like custom_sort() vs sort(). Do I assume correctly?
process_lines() and read_lines() work quite differently though and that's throwing me off. read_lines() returns a result sequence but process_lines() does not, I think.
I need an easier example. Let's say "namelist.txt" contains a list of names:
Amy Matt Robert Craig David Ali Joan Arthur Lee
and I want to extract and build a sequence of names that only begin with 'A'. Very simple. Would process_lines() be good here, and how do we do it?
Lionel
5. Re: What is process_lines? and general complaint on the new manual
- Posted by ne1uno Jan 20, 2011
- 1929 views
It used to be that all functions required a return value, or else an error would be thrown. Now, when you call a function, you don't need a return value ... Kind of makes the procedure redundant ...
I see. May I ask the reasoning behind this change? Knowing nothing about language design, it's not obvious to me.
often you will see the function return assigned to a VOID or JUNK object and then never used. sometimes it is simpler to just ignore the return of an error value.
the error return of process_list() is what the caller of the function decides it is. this would probably be used in a real program, but you can also observe the output of those 20 lines to decide if there were an error. but is just an example.
...
I am under the impression that process_lines() is like read_lines(), but with user-defined mojo. Like custom_sort() vs sort(). Do I assume correctly?
process_lines() and read_lines() work quite differently though and that's throwing me off. read_lines() returns a result sequence but process_lines() does not, I think.
...
I want to extract and build a sequence of names that only begin with 'A'. Very simple. Would process_lines() be good here, and how do we do it?
filter() or build_list() could return such a modified sequence.
6. Re: What is process_lines? and general complaint on the new manual
- Posted by raseunew Jan 20, 2011
- 1929 views
Given your text input file 'namelist.txt'
containing
Amy
Matt
Robert
Craig
David
Ali
Joan
Arthur Lee
the following will work
include std/io.e constant TERMINATE = 1 constant PROCEED = 0 function printNames(sequence aLine, integer line_no, object data) writefln( data[1], { line_no, aLine }) if data[2] > 0 and line_no = data[2] then return TERMINATE else return PROCEED end if end function function filterNames(sequence aLine, integer line_no, object data) if equal(aLine[1], data[1]) then nameList &= { aLine } end if return PROCEED end function --// print first 5 names process_lines("namelist.txt", routine_id("printNames"), {"[1z:4] : [2]", 5}) --// gather all names beginning with 'A' sequence nameList = {} process_lines("namelist.txt", routine_id("filterNames"), {'A'}) for i = 1 to length(nameList) do printf(1, "%s\n", { nameList[i] }) end for