Σάββατο 26 Οκτωβρίου 2024

Revision 39, Version 12

 

1) Some fixes for slice to not raise error for some situations:

a=(1,2,3): ? LEN(a#slice(1,0))=0 ' NOT RAISE ERROR, RETURN EMPTY ARRAY
TRY OK {? LEN(a#slice(2,1))=0} : IF ERROR THEN ? "HERE:"+ERROR$
? LEN(a#slice(3,))=0 ' NOT RAISE ERROR, RETURN EMPTY ARRAY

2) #fold() may produce arrays now. See the definition of a slice as fold1, where second parameter is number of many to slice and nor the ending item to slice:

a=(1,2,"aaa","bbb",5,6,7)
fold1=lambda (skip, many) -> {
=lambda t=(,), m=0, skip, many -> {
if m=0 then shift 2: drop: m=1: data t
read q, k as array
if skip<=0 then
if many>0 then append k, (q,)
data k
many--
else
data t
end if
skip--
}
}
// #slice(start, end)
// #fold(fold1(start, many))


? a#fold(fold1(2, 5))#str$(", ")
// PRINT: aaa, bbb, 5, 6, 7
// same as:
? a#slice(2, 6)#str$(", ")


? a#fold(fold1(1, 3))#str$(", ")
// PRINT: 2, aaa, bbb
// same as:
? a#slice(1, 3)#str$(", ")

3) How we get another list for doing map and produce different size array (previous the map function was 1 to 1 for input to output), So now we can change the ratio of items of input list by number of items of output list. So for these example the final list have each item a concat of each item from list1, list2 and list3 so 11019 is the frist item and 21120 the second one. There are no more items because list2 is small. This works from either small list from these three. The map function exit if we don't pass return value (pushing it to stack of values, see push statement - we read from current stack using letter$ and number)

List1=(1,2,3,4,5,6,7,8,9)
List2=(10,11)' ,12,13,14,15,16,17,18)
List3=(19,20,21,22,23,24,227)
map1=lambda (list0 as array) ->{
dim a()
a()=list0 // we get a copy
=lambda a(), n=-1 ->{
if n<len(a())-1 then
n++
if islet then
push letter$+(a(n))
else
push number+""+(a(n))
end if
else
flush ' if not return value the map function exit ' addition from 39
end if
}
}
? "["+list1#map(map1(list2), map1(list3))#str$(", ")+"]"
map2=map1(list2)
map3=map1(list3)
list2=()
list3=()
// map2 and map3 has a copy of list2, list3 before we erase it
? "["+list1#map(map2, map3)#str$(", ")+"]"

4) For map an example where we produce a bigger list from input list:

dim a(3) as long
a(0):=1,2,3
map1=lambda ->{
push random(10)+number, 100 ' two values for one
}
? a()#map(map1)#str$(", ")
// 100, 8, 100, 3, 100, 4

5) A big fix for enums. Fix Sum/Min/Max/Min$/Max$ for tuple using Enum values. Sort not work with enum values. This example use Sort after a map function, which map the enum values only to simple values (without the object). Also a Map function can get more than one lambda function to make more maps, one after the other.

About enums: they are objects, which know the name of value, the value (numeric as  number and sign or a string value, also know the order, so a variable of some enum type can advance to next using ++ operator or go back using -- operator, We can use variables which get enum values. For a variable which hold an enum object if we assign a simple value, interpreter search for this value to change an internal index value. If interpreter can't find a value from the enum set then raise error. Also we can use As NameOfEnum for an item in a parameter list to constrain the input.


enum m {a=50, b=200, c=30}
k=(a, b, c)
? k#max(), k#min(), k#val(0), k#Sum()
z=each(k)
strip = lambda->{
read t as long:data t
}
strip1 = lambda->{
read t as long:data t+100
}
k=k#map(strip,strip1)#sort()
? k#str$(", ")





Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.