+1
And what about bash? Talk about a ubiquitous language. There's probably more shell scripts than js out there. ;)
+1
That's basic concepts of concatenating and reducing array that are used in JS every time. You don't need to go that deep in code unless something works wrong. These three simple "return" ".concat" ".reduce" tells you what's there. On the other hand your Lua codesorcerykid wrote: ↑Mon Jan 04, 2021 22:18Just from a cursory glance, that looks like spaghetti code with all kinds of obtuse JS-specific notations.Code: Select all
return this.#data = [{}] .concat(Object.keys(this.#data).sort((a,b) => {return this.#data[b][field]-this.#data[a][field]})) .reduce((acc,key) => { acc[key] = this.#data[key]; return acc })
Code: Select all
local sorted_list = { }
for k, v in pairs( data ) do
table.insert( sorted_list, k )
end
table.sort( sorted_list, function ( a, b )
return data[ a ][ field ] > data[ b ][ field ]
end )
Still a deal though. It's sad when you start writing a program but instead find yourself engaged with implementing/managing libraries of basic table.sort() and math.round() algorithms every time like if there's 1980. And then dragging it from project to project because lang authors decided that you "will never need it".
although I'm not quite sure what is meant here. But I'm sure it's something worth at least several days of that gentleman's life and something that could and should be implemented in Lua by default a long time ago.
That's quite the opposite: lacking of will to argue about that. When I wrote scoreboard.js I decided to write it in Lua also but the perspective of pain of dealing with it made me so depressive that I just decided to see what code will you come with. And you didn't see the purpose of using the hashtable throwing out the id: property. The cache is needed here to demonstrate the purpose of using a sortable hashtable in a first place. Because when you code something like this you always find later to be necessary to reuse the data object with some caching or things like that. And it's uncool also when you have to rewrite the code because it doesn't provide you with enough redundancy of options by default like JS hashtables do.sorcerykid wrote: ↑Mon Jan 04, 2021 22:18so a cache isn't needed anyway. It sounds as if you are determined to find anything to complain about
Just to be accurate, I applied this diff with patch to your previous code and got this lua codesorcerykid wrote: ↑Mon Jan 04, 2021 22:18Here's a diff that shows the required changes to add caching as described.
Code: Select all
------------------------------------------------------------
-- function table.sort_by( )
--
-- Sorts a table on the given numeric field and returns a
-- list of the sorted keys accordingly.
------------------------------------------------------------
function table.sort_by( data, field, is_reverse )
local sorted_list = { }
for k, v in pairs( data ) do
table.insert( sorted_list, k )
end
table.sort( sorted_list, is_reverse and
function ( a, b ) return data[ a ][ field ] > data[ b ][ field ] end or
function ( a, b ) return data[ a ][ field ] < data[ b ][ field ] end
)
return sorted_list
end
function printf( str, ... )
print( string.format( str, ... ) )
end
function Scoreboard( sort_field )
local self = { }
-- private properties
local data = { }
local sorted_list
local sort_count = 0
assert( ( { rating = true, hp = true, mana = true } )[ sort_field ], "Invalid field for sorting!" )
-- public methods
self.sort = function ( field )
sort_count = sort_count + 1
printf( "Sorting by %s... called %d time(s)", field or sort_field, sort_count )
sorted_list = table.sort_by( data, field or sort_field, true )
end
self.print = function ( )
print( "*****" )
printf( "%-4s %-10s %6s %6s %6s", "RANK", "NAME", "RATING", "HP", "MANA" )
for i, v in ipairs( sorted_list ) do
printf( "#%-3d %-10s %6d %6d %6d", i, v,
data[ v ].rating, data[ v ].hp, data[ v ].mana )
end
print( "*****" )
end
self.insert = function ( name, def )
assert( not data[ name ], "Entry already exists!" )
data[ name ] = {
rating = def.rating or 0,
hp = def.hp or 0,
mana = def.mana or 0,
}
self.sort( )
self.print( )
end
self.delete = function ( name, rating )
assert( data[ name ], "Entry not found!" )
data[ name ] = nil
self.sort( )
self.print( )
end
self.add_rating = function ( name, rating )
assert( data[ name ], "Entry not found!" )
data[ name ].rating = data[ name ].rating + rating
end
self.add_mana = function ( name, mana )
assert( data[ name ], "Entry not found!" )
data[ name ].mana = data[ name ].mana + mana
end
self.add_hp = function ( name, hp )
assert( data[ name ], "Entry not found!" )
data[ name ].hp = data[ name ].hp + hp
end
self.get_best_player = function ( field )
return table.sort_by( data, field or sort_field, true )[ 1 ]
end
-- export object
return self
end
local scoreboard = Scoreboard( "rating" )
scoreboard.insert( "tom", { hp = 1, mana = 100, rating = 100 } )
scoreboard.insert( "jerry", { hp = 2, mana = 100, rating = 200 } )
scoreboard.insert( "he-man", { hp = 10, mana = 300, rating = 1000 } )
scoreboard.add_rating( "jerry", 9000 )
scoreboard.add_mana( "tom", 600 )
scoreboard.add_hp( "he-man", 100 )
scoreboard.delete( "tom" )
scoreboard.insert( "somebody", { hp = 90, mana = 320, rating = 400 } )
print( "Best player by rating:", scoreboard.get_best_player( "rating" ) )
print( "Best player by hp:", scoreboard.get_best_player( "hp" ) )
print( "Best player by mana:", scoreboard.get_best_player( "mana" ) )
Code: Select all
me@me:~/Desktop$ lua5.3 score.patched.lua
Sorting by rating... called 1 time(s)
*****
RANK NAME RATING HP MANA
#1 tom 100 1 100
*****
Sorting by rating... called 2 time(s)
*****
RANK NAME RATING HP MANA
#1 jerry 200 2 100
#2 tom 100 1 100
*****
Sorting by rating... called 3 time(s)
*****
RANK NAME RATING HP MANA
#1 he-man 1000 10 300
#2 jerry 200 2 100
#3 tom 100 1 100
*****
Sorting by rating... called 4 time(s)
*****
RANK NAME RATING HP MANA
#1 jerry 9200 2 100
#2 he-man 1000 110 300
*****
Sorting by rating... called 5 time(s)
*****
RANK NAME RATING HP MANA
#1 jerry 9200 2 100
#2 he-man 1000 110 300
#3 somebody 400 90 320
*****
Best player by rating: jerry
Best player by hp: he-man
Best player by mana: somebody
Code: Select all
me@me:~/tst/js$ node scoreboard4.js
Sorting by rating... called 1 time(s)
*****
RANK NAME RATING HP MANA
#0 tom 100 1 100
*****
Sorting by rating... called 2 time(s)
*****
RANK NAME RATING HP MANA
#0 jerry 200 2 100
#1 tom 100 1 100
*****
Sorting by rating... called 3 time(s)
*****
RANK NAME RATING HP MANA
#0 he-man 1000 10 300
#1 jerry 200 2 100
#2 tom 100 1 100
*****
Sorting by rating... called 4 time(s)
*****
RANK NAME RATING HP MANA
#0 jerry 9200 2 100
#1 he-man 1000 10 300
#2 tom 100 1 100
*****
*****
RANK NAME RATING HP MANA
#0 jerry 9200 2 100
#1 he-man 1000 10 300
#2 tom 100 1 700
*****
*****
RANK NAME RATING HP MANA
#0 jerry 9200 2 100
#1 he-man 1000 110 300
#2 tom 100 1 700
*****
*****
RANK NAME RATING HP MANA
#0 jerry 9200 2 100
#1 he-man 1000 110 300
*****
Sorting by rating... called 5 time(s)
*****
RANK NAME RATING HP MANA
#0 jerry 9200 2 100
#1 he-man 1000 110 300
#2 somebody 400 90 320
*****
Sorting by rating... called 6 time(s)
Best player by rating: [ 'jerry', { hp: 2, mana: 100, rating: 9200 } ]
Sorting by hp... called 7 time(s)
Best player by hp: [ 'he-man', { hp: 110, mana: 300, rating: 1000 } ]
Sorting by mana... called 8 time(s)
Best player by mana: [ 'somebody', { hp: 90, mana: 320, rating: 400 } ]
Never was going to challenge your experience. Might be that you're so smart and experienced that you don't feel discomfort working with Lua. But even if you are ok walking everyday up to the 15th floor by the stairs it doesn't mean that having a lift in a building means something bad.sorcerykid wrote: ↑Mon Jan 04, 2021 22:18As someone that's come from a background of many different programming languages
Tried it about 10 years ago, got weird problems with parallelism. If they're adopted promises might be a good language to try it one more time. RoR spoiled the Ruby though. That would be better for Ruby is RoR never existed. Bash is good for code obfuscation. Significant space symbols is the "best" part about it.
If it's such "basic concepts" used every time, then it's strange that the JS language authors were so lazy that they didn't provide all of that redundant functionality as a single API. Instead JS requires a difficult-to-read copypasta whenever someone wants to implement a basic table sort in JS. That's ironic.That's basic concepts of concatenating and reducing array that are used in JS every time. You don't need to go that deep in code unless something works wrong.
If you honestly think that code snippet is qualified as "simple", then you really shouldn't be criticizing Lua.These three simple "return" ".concat" ".reduce" tells you what's there.
Ah, so you need overly-convoluted code so you can catch your eye on something specific. And that is why you want everyone to switch to Javascript to make it easier on your eyes, because you personally dislike "sparse and monotone" code.On the other hand your Lua code (snip)
looks so sparse and monotone that it is hard to catch an eye on something specific.
Strange, I never have problems understanding simple Lua constructs like this. It's just a run-of-the-mill loop and sort function. I maintain tens of thousands of lines of code for my server, and I can honestly say that even when I'm dealing with poorly written mods, I can still generally ascertain out what the mod author was intending to do.When I look at a code like that I see that there's just going something. Especially when you leave it for a couple of months and then return to it and need to quickly understand it's structure.
As I already wrote above Lua is an embedded scripting language, and it began as a configuration language. So if you want a feature that is not builtin then you "require" it. The fact that you find libraries difficult to manage, and think a scripting language is supposed to provide training wheels for every possible use-case is wholly unrealistic.Still a deal though. It's sad when you start writing a program but instead find yourself engaged with implementing/managing libraries of basic table.sort() and math.round() algorithms every time like if there's 1980. And then dragging it from project to project because lang authors decided that you "will never need it".
I only left out of the ID field because you didn't use it in your original example at all. Also trying to comprehend that JS code was already giving me a headache since you were directly manipulating a private data structure within a class, which is an anti-pattern. The whole point of classes is abstraction. So it is no wonder you're having difficulty maintaining a code base if you are designing a custom API that way.That's quite the opposite: lacking of will to argue about that. When I wrote scoreboard.js I decided to write it in Lua also but the perspective of pain of dealing with it made me so depressive that I just decided to see what code will you come with. And you didn't see the purpose of using the hashtable throwing out the id: property.
Riight, so because Lua doesn't support sortable hashtables, the entire Minetest modding community is supposed to stop what they are doing and migrate to JS to make your life easier because you don't like writing a few extra lines of code to implement and maintain a cache, which is only needed as a thought experiment. That's hardly a reasonable expectation.The cache is needed here to demonstrate the purpose of using a sortable hashtable in a first place.
Fact of life: No scripting language is going to provide every single builtin feature that you want. At least Lua is easily extensible.Because when you code something like this you always find later to be necessary to reuse the data object with some caching or things like that. And it's uncool also when you have to rewrite the code because it doesn't provide you with enough redundancy of options by default like JS hashtables do.
I actually said that there is no need to go behind these three keywords to understand the code unless something is broken and you really need to go deep into.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49If you honestly think that code snippet is qualified as "simple", then you really shouldn't be criticizing Lua.
There is a place to improve JS. Unfortunately they don't consider it also. For example not having a sprintf() for such a long time is a shame. It's again the same mentality "nobody needs it" despite the fact that it was productively and heavily used long time before JS even existed. Having a standard function to multisort a hashtable by its properties' values would have been a blessing (if an implementation of it would be good enough which is unfortunately not always the case too).sorcerykid wrote: ↑Tue Jan 05, 2021 11:49If it's such "basic concepts of concatenating" then it's strange that the JS language authors were so lazy that they didn't provide all of this redundant functionality as a single builtin function.
It's just somewhat a conveyor of one-liners where each line express a solid abstraction.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49Ah, so you need overly-convoluted code so you can catch your eye on something specific.
As I said before I understand why everyone has no pleasure in switching to it. Probably when I created the thread I thought that it could be easily done (which turned out to be not true or this would have been done already) and just to express my frustration towards the everybody's general content with Lua. Now I don't think everyone should switch to JavaScript even more because JS started heading in a wrong direction.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49And that is why you want everyone to switch to Javascript to make it easier on your eyes, because you personally dislike "sparse and monotone" code.
I don't want to insult people who generously gifted to everyone their hard work for free, that would be unfair regards to them, but I can't share the delight of reading most mods code I've seen. Even not poorly written ones is still a monotone mess and I think Lua contributed to this significantly as a language to start with.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49and I can honestly say that even when I'm dealing with poorly written mods, I can still generally ascertain out what the mod author was intending to do.
Please don't. But having at least a discussion about this is a good thing. Maybe just maybe in the distant bright future when/if the Minetest 2.0 will be born the choice of Lua would be reconsidered in favor of some better scripted language that hopefully will emerge. This thread should exist as a statement of the fact that Lua is not an ideal scripting language everyone is content with. It has issues.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49the entire Minetest modding community is supposed to stop what they are doing and migrate to JS to make your life easier
JS is as extensible as a Lua. Both are merely equivalent to each other regards that feature.sorcerykid wrote: ↑Tue Jan 05, 2021 11:49Fact of life: No scripting language is going to provide every single builtin feature that you want. At least Lua is easily extensible.
That's a library issue, not an issue with the language. There's nothing stopping you from writing your own concat() and reduce() functions in Lua and using them to do functional programming. In fact, Lua can implement them slightly more elegantly than JS because Lua has tail-call optimization, which ES2016 was supposed to have, but neither Chrome nor Firefox have implemented it to date. Arrow syntax is nice, but it's syntactic sugar; function/end is just as readable. And (my pet peeve, I know) this/self in Lua isn't nearly as much black magic as it is in JS; in Lua, it's just another function parameter. Saves you so many calls to bind().
Perhaps you shouldn't judge me! you aren't my bossrubenwardy wrote: ↑Mon Jan 18, 2021 15:41Lua can run in browsers. Having JavaScript modding won't make it any easier to port to a web browser, we just don't want to support that. Perhaps you shouldn't be playing games at work or on work computers
I think if you're saying that those things can help us more than browsers, you might not understand what they actually are...
if minetest supported JS we could use "react native" to port it to the iphone . I have an iphone as a symbol of my social status but it cannot install minetest. someone might start making connections related to that for every day it does not workphilipbenr wrote: ↑Thu Feb 11, 2021 06:32I think if you're saying that those things can help us more than browsers, you might not understand what they actually are...
Minetest already has a native app for all distributions, so there is no need to bother with the native app functionality that those frameworks provide. Not to mention at that point you're already working with HTML, CSS and JS so you might as well just write the web app anyway. There is little to no added benefit to using those frameworks in the current state of Minetest
It would be better to use WASM instead.
Again, that's excess work, as you could just use WASM (Web assembly) and make C++ and Lua code that can run in browser and bypass everything altogether. Granted, there are downsides to this, but the same (performance) downsides would probably exist if you were to attempt to port Minetest to Javascript. Not to mention the headache of Javascript's asynchronicity. Coming from a C++ perspective, probably nobody wants to deal with that.
sorry but I dont want to code my "tests" in assembly . I like the high level (C). unless you give me a nice macro engine. javascript is the fastest scripter language in invented . it has so much money behind it from google. they have to make it fast. you could embed a javascript enigne into minetest. maybe it can be converted into an electron app? I think this would be very promisingphilipbenr wrote: ↑Thu Feb 11, 2021 16:47Again, that's excess work, as you could just use WASM (Web assembly) and make C++ and Lua code that can run in browser and bypass everything altogether. Granted, there are downsides to this, but the same (performance) downsides would probably exist if you were to attempt to port Minetest to Javascript. Not to mention the headache of Javascript's asynchronicity. Coming from a C++ perspective, probably nobody wants to deal with that.
If you want a Minetest iOS App, you would want to compile it natively, otherwise performance would probably be bad. Granted, I've never programmed specifically for iOS, but the principle probably holds true.
my dads's friend knows steve jobs . I can take care of this part . they did ketamine togetherv-rob wrote: ↑Thu Feb 11, 2021 18:31Minetest can run on iOS, but licensing in the app store is the main issue there. Running Minetest in a browser is theoretically possible, but I don't know anyone who has that kind of time or dedication. Regardless, using JavaScript as a scripting language would have no benefit for either of these.
How do I doHybrid Dog wrote: ↑Sat Feb 13, 2021 17:49How does MoonScript compare to JavaScript?
https://moonscript.org/
Code: Select all
setTimeout(function(){console.log("Hello World")},1000)
Code: Select all
function Thing( self )
self.name= "unknown"
return self
end
function Person( self )
Thing( self ) -- extend base class
self.say_name = function ( )
print( "Hello, I am " .. self.name .."!" )
end
return self
end
local p = Person( { } )
p.name = "MoonScript"
p.say_name( )
Why would anyone use a stand-alone Microsoft scripting language for embedded scripting of free software?
Because Typescript is licensed under Apache License 2.0 and it makes writing bad JavaScript code harder. And because it is a superset of JavaScript in the first place, you could compile it down to JavaScript anyway.
They could (and eventually will) re-license it to a proprietary license. I wonder even if a community will be able to support V8 engine when Google will drop it's support as it dropped a support for a lots of another projects.philipbenr wrote: ↑Mon May 10, 2021 18:36Because Typescript is licensed under Apache License 2.0 and it makes writing bad JavaScript code harder. And because it is a superset of JavaScript in the first place, you could compile it down to JavaScript anyway.
I get that Microsoft bad and all, but there's no good reason to snub good software that is licensed under an open source license.
I disagree that they will re-license it. I think TypeScript, like many other things in the JS development world, lives and dies by the open source community. That's why so many of these kinds of frameworks and projects are hosted on GitHub and are driven by open source community members.
TypeScript only lives from Microsoft seeing it as a tool to drag users to their environment (like the Linux subsystem for Windows) to lure them into paying for their services. All this "Microsoft Loves Linux" bullshit solely exists for this one reason. TypeScript is no exception here. Over time it will be made more and more incompatible with the "outside world" just to lock-in developers.philipbenr wrote: ↑Tue May 11, 2021 06:19I think TypeScript, like many other things in the JS development world, lives and dies by the open source community.
The projects are hosted only on MS GitHub because developers would cause a lot of disturbance when moving to another Git hosting provider. Oh, and same as with TypeScript in the future, MS currently makes Github slightly incompatible to the outside world using bullshit reasons for justification.philipbenr wrote: ↑Tue May 11, 2021 06:19That's why so many of these kinds of frameworks and projects are hosted on GitHub
Ignoring Microsoft's past and current business basing on insults, lies, hostile acquisitions, intellectual property theft, EEE business practice, vendor lock-in, abuse of their monopoly, etc., etc. ... ... is silly.philipbenr wrote: ↑Tue May 11, 2021 06:19Also, thinking Facebook / Microsoft / Amazon / whoever's business group is the same as their engineering group these days is silly.
Users browsing this forum: No registered users and 7 guests