Skip to main content

News

Topic: Link.js v0.4.2 (Read 95605 times) previous topic - next topic

0 Members and 2 Guests are viewing this topic.
  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.8b
Reply #105
@Radnen:
Here ya go. :)

Code: (javascript) [Select]
	Link.create = function() {
var args = arguments,
stop = args.length - 1,
v    = args[stop],
isFn = (typeof v == "function"),
indices = [];

function CreateArray(n, i0) {
if (n == stop) return (isFn) ? v.apply(this, indices) : v;
var a = [], l = args[n], n = n + 1;
for (var i = 0; i < l; ++i) {
indices[n - 1] = i;
a[i] = CreateArray(n, i);
}
return a;
}

return CreateArray(0, 0);
}


As I said before, great for lookup tables:
Code: (javascript) [Select]
var timesTable = Link.create(11, 11, function(a, b) { return a * b; });  // JS really needs a lambda operator :'(
Abort(timesTable[5][5]);  // should be 25


Edit: Just sent a pull request on GitHub with the changes.
  • Last Edit: February 11, 2014, 01:37:07 pm by Lord English
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.8b
Reply #106
Sweet, that's really nice. Thanks! :)


Code: (javascript) [Select]
var timesTable = Link.create(11, 11, function(a, b) { return a * b; });  // JS really needs a lambda operator :'(
Abort(timesTable[5][5]);  // should be 25



In Moz-land (JS 1.8+), you do have short-form lambda expressions in JS:
Code: (javascript) [Select]
var timesTable = Link.create(11, 11, function(a, b) a * b); // here.
Abort(timesTable[5][5]);  // should be 25


Edit:
I'm surprised how easy that looked. Just a single array incrementing a single value throughout the recurring chain. I like it. :)
  • Last Edit: February 11, 2014, 04:43:00 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.8b
Reply #107

Edit:
I'm surprised how easy that looked. Just a single array incrementing a single value throughout the recurring chain. I like it. :)


Haha, yeah, I'm almost embarrassed how simple that turned out to be to implement.  It took some mental gymnastics on my part to come up with that solution only for it to turn out to be just two extra lines of code! :-[  Closures are awesome, aren't they?
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.8b
Reply #108
Hey, out of curiosity, what exactly do the .run() methods do?  I think you mentioned earlier in the thread how they were a cheat to enhance performance, but looking at the code for a few of them I don't really understand what exactly they're doing.  My best guess is that they're running parts of the query in advance and caching stuff to be reused later (for example, if the same chain is called twice), but as I said I'm not sure.  Care to clarify?
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.8b
Reply #109
It stops one less node traversal from happening. I don't know why, but it really ends up speeding things up considerably. On a filter-map-filter run, it would first go to filter, run it's node and move on. With a .run() method it'll instead start at map and run with a filtered result first thing. So in that case it's particularly fast.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.9
Reply #110
I released 0.2.9 it adds SQL-like features:


  • Join (inner only)

  • Select

  • Where (already been in it but an overload was added)

  • Update



Now you can do this:
Code: (javascript) [Select]

// updating the coins of player named "Radnen":
Link(players).where('name', 'Radnen').update('coins', 15);

// a separated inventory table and player table getting player radnen's items:
var items = Link(players).where('name', 'Radnen').join(items, function(a, b) { a.name == b.name; }).select('name', 'items').toArray();

// retrieving only certain "columns" from a table:
var stats = Link(players).select('name', 'speed', 'strength', 'wisdom').toArray();


This is *not* an SQL implementation. It is rather SQL-like functionality built into Link. When you form queries you form them almost backwards than what you'd expect from actual SQL: you do your "selects" last and "wheres" first.

How is it *like* SQL you may ask? Well if you picture the rows as array entries and columns as object properties then you have basically the shim needed to do basic SQL-like transforms on the data. The 'players' table from above may look like this:
Code: (javascript) [Select]

var players = [
{name: "Radnen" , coins: 0  , strength: 1, wisdom: 10, speed: 5},
{name: "DaVince", coins: 20 , strength: 2, wisdom: 12, speed: 4},
{name: "Jest"   , coins: 100, strength: 1, wisdom: 11, speed: 6}];


And the items table could look like this:
Code: (javascript) [Select]

var items = [
{name: "Radnen" , items: ["helmet"]},
{name: "DaVince", items: ["bag", "jeans"]},
{name: "Jest"   , items: ["staff", "outfit"]}];


Join and select would work exactly as you would expect and update will modify the list.
  • Last Edit: March 09, 2014, 05:45:59 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Link.js v0.2.9
Reply #111
Speaking of SQL-like queries, what's the likelihood of getting wildcards?

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.9
Reply #112
Such as select('*')? Yeah, don't use select and instead go straight to toArray(). in Link things are usually filtered out, so unless you limit what you take, select('*') is really the default behavior. For the LIKE clause, I could just employ a regex for all the other wildcards.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Link.js v0.2.9
Reply #113
My god, Radnen, that is awesome. Makes things so much easier.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.9
Reply #114

My god, Radnen, that is awesome. Makes things so much easier.


I particularly like the new .join method. Now I don't need super huge messy player data structures. I can strip out battle stats, inventory, "world stats", and other data to create more lightweight structures.

Now I can have player entities like this:
Code: (javascript) [Select]

// player-stats objects which are easily json-able:
function PlayerStats(params) {
    this.name = params.name || "unnamed";
    this.atk = params.atk || 0;
    this.def = params.def || 0;
    this.strength = params.strength || 0;
    this.wisdom = params.wisdom || 0;
    this.speed = params.speed || 0;
}

// player position objects:
function PlayerPosition(params) {
    this.name = params.name || "Unnamed";
    this.direction = "north";
    this.map = "";
    this.x = 0;
    this.y = 0;
}

// and a player-items object:
function PlayerItems(params) {
    this.name = params.name || "Unnamed";
    this.items = params.items || [];
    this.coins = params.coins || 0;
}


Then I can do join queries against several tables, if I were doing a multiple-entity game:
Code: (javascript) [Select]

// a player can only hold so many items based on their strength, we now have an easy way of getting this disjoint data:
Link(player_stats).join(player_items, function(a, b) { a.name == b.name; }).select('name', 'strength', 'items').toArray();


I'm sure there are plenty of other uses too. I mean, this really is best used with the fact that you have multiple party members.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.9
Reply #115
Guys, here's a neat trick with Link. Did you know Link can unroll strings too? Accessing string characters is no different than accessing any standard array. So, here's a neat trick for finding a particular character in a set of lines. This is useful for playing a speech blip when certain characters appear on screen in a textbox.

Code: (javascript) [Select]

var ch = Link(lines).unroll().get(position);
if (ch != " " && ch != "\"" && ch != "\t") { PlayBlip(); }


It'll return the character at that position within the separated lines of text. Now, this is only useful if the lines were split from wordWrapString and you wanted to get what I call the global position of a character in the text. It's best used when 'position' is incremented upwards, starting at 0.

Basically I made this post to say, hey if you got lines of text Link works on that stuff too! ;)
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Link.js v0.2.9
Reply #116
That is awesome. I may very well rewrite NTML using this...

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.9
Reply #117
Fir your NTML it might be useful to run markup through link like so:

Code: (javascript) [Select]

function CreateWord(word, array) {
    var type = 0;

    var ch0 = word[0];
    var ch1 = word[word.length - 1];

    if (ch0 == "*" && ch1 == "*") type = 1;
    if (ch0 == "_" && ch1 == "_") type = 2;
    if (ch0 == ":" && ch1 == ":") type = 3;

    if (type != 0) word = word.substr(1, word.length - 1);

    array.push({ word: word, type: type });
    return array;
}

var str = "This *is* a _marked up_ piece of :text:.";

var words = Link(str.split(" ")).reduce(CreateWord, []);


Now, once Link get's it's own split method we can go faster since it can split as it goes (currently the string is split at the beginning). So, this may not be ideal for large strings. Also, the above 'parser' is very simplified to get an idea of what one could do.

The operation with a Link.split method:
Code: (javascript) [Select]

Link(str).unroll().split(" ").reduce(CreateWord, []);
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Link.js v0.2.9
Reply #118
Oh, I think you slightly misunderstood the domain of NTML; it's not conveniently aliased markup like Markdown or Wikitext, but instead more like HTML or BBCode.

I did restore NTML's wiki page a while back, so anyone who wants to can download the old, hopefully still working, version of the tech demo. NTML and the handler script are in dire need of updating to be cleaner and easier to extend in the future.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.9
Reply #119
Wait, why is the unroll() necessary?  A string is a 1D array of characters, it shouldn't even be possible to unroll it seeing as unrolling is for multidimensional arrays...

So yeah, I is comfuzed.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub