Skip to main content

News

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

0 Members and 4 Guests are viewing this topic.
  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #210
Hmm, it is rather difficult since random must compile the array before executing, but yet you want to strip out multiple occurrences of weaponID after having done that, while maintaining the random count. This is possible if indeed random was not an endpoint but rather a Lazy executed function, of which there are no good algorithms for.

A randomWithFilter is something I'll have to look into.

Edit: for the time being you can add/use this:

Code: (javascript) [Select]

function Random(link, filter, times) {
if (!times) times = 1;
var a = link.toArray();
var samples = [];
while (times > 0) {
var i = Math.floor(Math.random() * a.length);
if (filter(a[i])) {
samples.push(a[i]);
times--;
}
}
return samples;
}


The only issue is that this will loop indefinitely if it does not pass the filter 'times' times. So with the above in mind:
Code: (javascript) [Select]

var found = false, count = 0;
Random(Link(array), function(item) {
    if ("weaponID" in item && !found) { found = true; return true; }
    else {
        count++;
        return item.phase <= phase && count < 6;
    }
}, 5);


It's yucky code and it assumes two things: that the array is at least 5 elements that pass the test, and to pass the test it has to have at least one item with a weaponID and n-1 items that pass the phase check.

The only other option is to programmatically build an array by trying random samples until your requirements are met. It's not so easy to do something like that in a single Link query, but I'll try to think on this some more. It may end up taking n queries best case scenario, and q queries worst case where q = size of list and n = number to receive, where n <= q.
  • Last Edit: July 04, 2014, 03:57:48 am 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

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #211
Were you able to solve your problem?
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.15
Reply #212
Sorry, didn't see the edit.  I hate that the notification system doesn't account for edits.  This is why I'll usually make a new post in a case like this--less likely to be missed.

Anyway, I haven't actually touched the Specs code since I asked the question, I'll have to test this out next time I'm on an actual computer and not my phone.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #213
"Warning: this topic has not been posted in for at least 120 days."

Lol, anyways,
I thought of a neat feature that would be perfect for link.

Link can be used on objects, it may read their properties (if it doesn't I'll add it) anyways, I run into this from time to time where I'm reading values in from a database and I get string values in return, but want to treat them as numbers. Or I get a text-dump of numbers and I need to convert strings to numbers.

Code: (javascript) [Select]

Link(object).select(['num1', 'num2', 'num3', 'num4']).convert(Number).toObject();
Link(array_of_strings).convert(Number).toArray();


So, what do you think? Neat or not?

... wait I might just get away with:
Code: (javascript) [Select]

Link(array_of_strings).map(Number).toArray();


Which is neat, huh?
  • Last Edit: November 26, 2014, 05:14:37 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.15
Reply #214
Is it faster to use the built-in Number constructor or to use a custom toInt that essentially returns (n|0)? I haven't jsperf'ed it yet, but if not for the fact that Number is native I'm inclined to think the latter.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #215
Here's a link to the test cases.
http://jsperf.com/link-number-conversion

Number seems to win. But feel free to play around with it.
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

Re: Link.js v0.2.15
Reply #216
http://jsperf.com/link-number-conversion/2

In Firefox at least, n<<0 seems to win (even over n>>0). Which makes sense to me, since I thought that's the JSM way of doing it.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #217
Tested it in android firefox (GS5), 3.38 ops/s for Number vs. 3.61 ops/s for n|0.

Edit: Just tested on Safari on my G1 iPad mini: The tests took forever, but Number won by a large margin--0.86 ops/s while everything else was in the 0.6-0.7 ops/s range.  Apparently which method is fastest is browser-specific.
  • Last Edit: November 27, 2014, 10:05:30 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.15
Reply #218

http://jsperf.com/link-number-conversion/2

In Firefox at least, n<<0 seems to win (even over n>>0). Which makes sense to me, since I thought that's the JSM way of doing it.



In Chrome, Number still beats everything. :o
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.15
Reply #219
Yeah, Number beat everything on iOS Safari as well, as mentioned above.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Link.js v0.2.15
Reply #220
http://jsperf.com/link-number-conversion/3

What happens when you don't use anonymous functions in the loop. Number is the slowest of the bunch, but I'm only using OS X Safari 7.1. I'm pretty sure I'd get the same results as you guys if I were using other browsers.

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

What happens when you don't use anonymous functions in the loop.


In Chrome there are no changes. I think most JIT's will cache anonymous functions, and besides an initial speed hit, subsequent calls to the function wouldn't have much of an impact. It averages out quite nicely. But I guess one bonus of pulling out the anonymous functions is a better understanding to optimize by the compiler. I think you see it most with |0, >>, and << in FF.
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

Re: Link.js v0.2.15
Reply #222
I think you see it most with |0, >>, and << in FF.


I expect a large part of why bitshift-by-zero and other jsm-isms are faster in FF is that Chrome didn't for a long time (and still doesn't?) accept JSM as a thing, so these have special optimizations in FF and not Chrome.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.16
Reply #223
Version 0.2.16 has been released. The last official release was 0.2.13, but some of you might have just downloaded 0.2.15 anyways in which case this really only fixes the .toArray() method.
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.3.0
Reply #224
So I've put out v0.3.0, it's still in beta since I'm still testing it. I'm upping the minor version count and not the patch count because I added a new feature that changes the behavior of Link. Now it will recognize the array index at every step of the way. This is a minor, minor speed hit but now you have access to an arrays index in each function call:

Code: (javascript) [Select]

Link([1, 2, 3]).map(function(item, i) { return item + i; }).toArray(); // [1, 3, 5] (new array)


Because of this newly added feature I've now added something I've thought of for a very long time. I originally didn't want this behavior because I found it "destructive" but now I think it has a use and so I'm adding it at your own risk. :P

Code: (javascript) [Select]

Link([1, 2, 3]).map(function(item, i) { return item + i; }).fuse(); // [1, 3, 5] (same array)


It'll modify the original array. I also call the feature '.coalesce()' but fuse is probably easier to type and remember. It synergizes well with .pluck:

Code: (javascript) [Select]

Link(array).pluck('apples').map(worms).fuse('apples');


Before this, there was no easy way to access the originating array after a pluck. Now you can pluck something out, do work and fuse it back by using the same property you plucked from, or you can use a different property and store results of the Link expression into another property of the object in the array.

Currently it's only an end point, but I will soon add a non-end point variants so we can jump around the array at will and puck/fuse/pluck other properties at will. This is why the version is at 0.3.0 and I thank you all so far for using it. :)
  • Last Edit: February 17, 2015, 12:44:29 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