From the xelerus functionlist:
map (map list variable expression)
filter (filter list variable expression)
These functions both work on lists. And the result will also be a list, the original list with something done to it.
In both cases immediately after the function name comes 'list'. So the first lot of brackets or the first word inside this function will be code that makes the list or the name of the list.
The next part of the function is the 'variable'. This is one list entry. What the functions do is take one list entry at a time and perform the 'expression' code on it. The function then takes the next list entry and does the same 'expression' code on it. It repeats this, working its way through the list until it runs out of list entries. Note that this 'variable' must always be in the 'map' or 'filter' code at least twice. Once just after the list/list code and at least once in the 'expression' code.
As an example in SOTP;
Code: Select all
(setq listOfSystemNames (map (sysGetNodes) variable (sysGetName variable)))
It does this using 'sysGetNodes' to generate a list of all the nodeIDs in SOTP. These nodeIDs are usually two or three digits long and are used by the game to identify star systems instead of using system names. (Most systems in SOTP have randomly generated names. eg the first system after Eridani always has the nodeID 'C1' but can be named either Groombridge, Lalande. 5 Indi or Foum Alhaut.)
So using 'sysGetNodes' will make the 'list' part of 'map' ~25 nodeIDs. Something like this.
(SE "C1" "C3" "C3A" BA "C4" CP SK "C5A" "C5" "C6" "C6A" CD "C7" "C8" "C9" "A1" EC "A3" "A3A" PJ "A5" "A7" "G1" "G2" Elysium Huaramarca)
Note that even though Elysium and Huaramarca come up like this they are not system names. They are still nodeIDs. More on this later.
What 'map' does is take the first nodeID, in this case "SE", and call it 'variable'. It then runs the next code, 'sysGetName', on 'variable' and gets "Eridani" which is the system name of nodeID "SE". Note that the 'variable' doesn't have to be called that. It can be called anything as long as the same name is used inside the 'expression' code. Calling it 'listEntry' or 'node' would work and would be a bit more descriptive.
It then starts a new list with "Eridani" as the first entry.
Next it takes the next nodeID, in this case "C1", calls it 'variable', runs the 'expression' code on it and gets the result, for the game I had running, "Lalande". It adds this to the new list and then goes on to do the same thing to the third list entry "C3". It will keep doing this until it runs out of list entries.
So the end result would look like this,
(Eridani Lalande Orgos Lacaille "Rigel Aurelius" "Hena's Star" Charon "St. Katharine's Star" "Mu Casser" Humboldt Acheron Junger Sanctuary Domitus Exael "Jiang's Star" "Severian's Star" "Eta Ceti" Qilin "St. Esperance's Star" "Point Juno" Vori Dantalion "HD 11901" Heretic Elysium Huaramarca)
which are the star system names in this particular game of SOTP.
Note here that because the nodeIDs 'Elysium' and 'Huaramarca' have had the 'sysGetName' code run on them they are now system names and no longer nodeIDs.
As a further example we will filter the list we just made, 'listOfSystemNames'.
Code: Select all
(filter listOfSystemNames name (eq name "Elysium"))
The 'filter' function
(filter list variable expression)
filters a list, in this case 'listOfSystemNames', and will only add a list entry to the new list if 'name' matches the 'expression' part of the code (returns True in code jargon).
So 'filter' takes the first list entry of 'listOfSystemNames', in this case 'Eridani', and calls it 'name' (instead of 'variable' this time). It then runs the 'expression' code on 'name' and if it is True, it adds it to the new list. In this case 'Eridani' is not 'eq'ual to 'Elysium' so the new list still has no entries. It then takes the second list entry from 'listOfSystemNames', here 'Lalande', calls it 'name' and checks to see if it is equal to 'Elysium'. Once again it is not, so no entry is added to the new list.
'filter' then continues through the list until the end. Only when it gets to the second to last entry 'Elysium' does the 'expression' code match. The code '(eq name "Elysium")' is True when 'name' is 'Elysium'. So the final list consists of one entry, (Elysium).
The 'filter' explanation on xelerus had this:
Filter takes the passed in list and returns a new list made up of elements of the passed in list that returned true for the boolean function.
Which is quite correct but was gibberish to me until I worked out what's going on in there.
If someone understands what the options ['excludeNil|'original|'reduceMax|'reduceMin] do, could they post that info here so the OP can be updated.Thanks.
(map list ['excludeNil|'original|'reduceMax|'reduceMin] var exp) -> list