Author Topic: Link.js v0.4.2  (Read 35088 times)

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #285 on: November 15, 2016, 02:38:23 am »
Just going to leave this here ;)

Code: Javascript
  1. from.Array(threads)
  2.         .where(function(t) { return t.id == threadID })
  3.         .besides(function(t) { t.isValid = false; })
  4.         .remove();
  5.  
  6. // you could also do `from.Object(obj)` to query keys, or just plain `from()` to determine
  7. // the enumeration mode from the type of the argument.

That reads back very nicely I think: "From Array 'threads', where an item's id matches the thread id, besides setting 'isValid' to false, remove it."
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Radnen

  • Wise Warrior
  • Senior Staff
  • Legendary Poster
  • *****
  • Posts: 1404
  • Sphere Studio Developer
    • View Profile
    • Radnen's Hub
Re: Link.js v0.4.2
« Reply #286 on: November 15, 2016, 09:15:38 pm »
I'm experimenting with simplifying things some more for my experimental "from" script.  One thing I notice that link does is to call the points recursively.  While this is in line with the chain metaphor, it ends up duplicating effort because every single point (except endpoints) needs to have a call to this.next.exec().  My idea was to instead have points return true to continue, and false to drop the current value, filtering it out.

Endpoints would work similarly - return true to continue with the next value, or false to short-circuit.  I think this is overall a better separation of concerns.

I actually had this exact implementation in Link, but for some reason I got rid of it... Not sure why though... I think the performance was better to not return a value?

Also what does coalesce do?  It says it merges modified results back into the array, but I'm not sure what that means in practical terms.  Link already has update()...

It is similar, yes. But with update you can specify a value. Coalesce uses the current computed value in the Link list and sticks it back into the array at the index value, but it's still experimental because some operations might change the index value.
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

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #287 on: November 16, 2016, 12:12:31 am »
Okay, so in other words:

Code: Javascript
  1. var nums = Link.range(5);
  2. Link(nums)
  3.     .map(function(n) { return n * n; })
  4.     .coalesce();
  5.  
  6. // 'nums' is now -> [ 1, 4, 9, 16, 25 ]

That's pretty convenient, basically transforming the entire array in one step.  Or you could add where clauses to only remap certain items... hm... I'll have to try to find a use for this now :)
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #288 on: November 16, 2016, 10:48:15 pm »
Link.js idea: make an option for it to go backwards... Could be interesting...

Just saw this bit.  That would indeed be interesting to see.  Among other things, it would allow last to be able to short-circuit the same way first can.  When going forwards you always have to run the entire sequence to find the last match(es), if you start from the end you can just stop after you have X items and then reverse them.
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #289 on: November 18, 2016, 11:57:03 am »
Hm, it occurs to me that one can take advantage of lazy evaluation to implement random chances for battle engines:

Code: Javascript
  1. const link   = require('link');
  2. const random = require('random');
  3.  
  4. // insta-kill attack
  5. link(battlers)
  6.     .where(function(b) { return b.isEnemyOf(attacker) })
  7.     .where(function(b) { return b.level % 5 == 0; })    // level is multiple of 5
  8.     .where(function() { return random.chance(0.35); })  // 35% hit rate
  9.     .each(function(b) { b.die(); });
  10.  
« Last Edit: November 18, 2016, 11:59:36 am by Lord English »
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #290 on: November 21, 2016, 12:40:22 pm »
Feature request: Partial application, or in other words reusing a partial query with different operators.  Link can already kind of do this, but only with endpoints.  In C#, you can do stuff like this:

Code: C#
  1. // get all items for the first party member:
  2. itemQuery = items.Where(v => v.owner == partyChars[0]);
  3.  
  4. // get items to show on the current page
  5. itemsOnPage = itemQuery
  6.     .Skip(pageSize * pageIndex)
  7.     .Take(10)  // 10 items per page
  8.     .ToArray();
  9.  

The above doesn't work in Link because adding points just keeps adding to the same query.  LINQ (and soon, Sphere v2 from()) returns a new enumerable for each point, which allows this to work.
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #291 on: February 04, 2017, 03:31:26 am »
Let me just say that Link and from() are so much more fun to use now that minisphere supports arrow functions. :D
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

Offline Fat Cerberus

  • miniSphere Developer
  • Verified
  • Legendary Poster
  • *
  • Posts: 2129
  • *MUNCH*
    • View Profile
Re: Link.js v0.4.2
« Reply #292 on: February 28, 2017, 10:57:42 am »
I found the first actual use case for from() over objects:
https://github.com/fatcerberus/spectacles-i/blob/master/src/party.js#L47-L50

Party management. :D
« Last Edit: March 01, 2017, 06:37:57 am by Fat Cerberus »
miniSphere 4.5.10 - Cell compiler - SSJ debugger
Forum Thread | GitHub Repo

 

anything