Re: named array

new topic     » goto parent     » topic index » view thread      » older message » newer message
DerekParnell said...

Or if the 'ugliness' of showing all the detailed assignments is too much for you ...

Create a map helper library such as ...

Very nice idea. I tried to do something similar, but with the sequences. It turned out well, I'm ALMOST satisfied. Today I made ​​the feature even closer to the way of Ruby. It turned out that Ruby keeps the symbols (I remind you that a symbol in Ruby is something like a key of the map in Euphoria) within a single table, shared by entire program. This can cause problems when the table is full. However, I find it hard to imagine a scenario where this happens. If the symbols are generated in some way, this is extremely bad programming style. I tried to do something similar to symbols possible. I hope it will be helpful not only to me. Мemory usage isn't effective, but it is the price paid for the convenience. I hope such "rubinish" thing will help someone else.

narr.e

include std/map.e 
 
map Symbols = map:new() 
integer Index = 0 
 
function nextIndex() 
	Index += 1 
	return Index 
end function 
 
export function new() 
	return repeat(0, Index) 
end function 
 
export function get(sequence Narr, sequence Symbol) 
	return Narr[map:get(Symbols, Symbol)] 
end function 
 
export function put(sequence Narr, sequence Symbol, object Value) 
	integer Index = map:get(Symbols, Symbol) 
	if Index = 0 then 
		Index = nextIndex() 
		map:put(Symbols, Symbol, Index) 
	end if 
	if length(Narr) < Index then 
		Narr &= repeat(0, Index - length(Narr)) 
	end if 
	Narr[Index] = Value 
	return Narr 
end function 

t_narr.e

include std/unittest.e 
include narr.e as narr 
 
sequence Narr1 = narr:new() 
 
Narr1 = narr:put(Narr1, "X", 100) 
Narr1 = narr:put(Narr1, "Y", -200) 
Narr1 = narr:put(Narr1, "Z", 300) 
 
test_equal("", 100, narr:get(Narr1, "X")) 
test_equal("", -200, narr:get(Narr1, "Y")) 
test_equal("", 300, narr:get(Narr1, "Z")) 
 
Narr1 = narr:put(Narr1, "X", 1000) 
Narr1 = narr:put(Narr1, "Y", -2000) 
Narr1 = narr:put(Narr1, "Z", 3000) 
 
test_equal("", 1000, narr:get(Narr1, "X")) 
test_equal("", -2000, narr:get(Narr1, "Y")) 
test_equal("", 3000, narr:get(Narr1, "Z")) 
 
sequence Narr2 = narr:new() 
 
Narr2 = narr:put(Narr2, "Velocity", 543.902) 
Narr2 = narr:put(Narr2, "X", 0.1) 
Narr2 = narr:put(Narr2, "Y", -0.2) 
Narr2 = narr:put(Narr2, "Z", 0.3) 
Narr2 = narr:put(Narr2, "Name", "Bullet") 
Narr2 = narr:put(Narr2, "XYZ-1", {narr:get(Narr1, "X"), 
                                  narr:get(Narr1, "Y"), 
                                  narr:get(Narr1, "Z")}) 
test_equal("", 0.1, narr:get(Narr2, "X")) 
test_equal("", -0.2, narr:get(Narr2, "Y")) 
test_equal("", 0.3, narr:get(Narr2, "Z")) 
test_equal("", "Bullet", narr:get(Narr2, "Name")) 
test_equal("", 543.902, narr:get(Narr2, "Velocity")) 
test_equal("", {1000, -2000, 3000}, narr:get(Narr2, "XYZ-1")) 

Please give advice on how to improve ways to use narrs. I mean string "include narr.e as narr". Why I needn't use "as", when including "map.e"? Second question: How to implement calling of narr:put without assignment? For example: Narr = put(Narr, "Name", "Joe"). When I using maps, I needn't assignment.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu