Skip to content

Inline / pipeline functions whenever possible #32

Closed

Description

@Isaac-Leonard

In Typescript we can currently do something like this:

arr
  .map(x(=) > x* 2 )
  .map(x() => x*x);

From what I understand most javascript interpreters will run the first map to completion before starting the second as javascript is too dynamic to allow for proper analysis of pure functions.
Would it be possible to investigate limiting certain dinamicness of borrowScript to allow for merging each step of the above code in compilation so the array is not iterated twice?

Activity

  1. SuperSonicHub1 commented on Oct 23, 2021

    @SuperSonicHub1

    So you want the code to become something like this?

    arr.map(x => x * 2 * x)

    Honestly, if you want something like this, it would be much better to use generators instead:

    function* map(iterable, callback) {
    	for (const x of iterable) {
    		yield callback(x)
    	}
    }
    
    map(map(arr, x => x * 2), x => x * x)

    Perhaps we should make ECMAScript generators have a greater focus in BorrowScript?

  2. Isaac-Leonard commented on Oct 23, 2021

    @Isaac-Leonard
    ContributorAuthor

    While my example is simplistic there's more complicated ones where being able to automatically pipeline pure functions would be useful.
    I don't really see the connection to generators here,.
    The transformation that I'm considering just removes the unnecessary double iteration of the array and would mainly be a compiler optimisation aided by enforcing some constraints on what code in the callbacks can do.

  3. SuperSonicHub1 commented on Oct 24, 2021

    @SuperSonicHub1

    From what I understand most javascript interpreters will run the first map to completion before starting the second

    Generators solve this exact problem; both maps are running at pretty much the same time. The first value of the array goes through the first map, and then immediately through the second.

    I guess I'm not really following then. Are there any existing languages that do the optimizations you're describing so that I can read about them?

  4. Isaac-Leonard commented on Oct 29, 2021

    @Isaac-Leonard
    ContributorAuthor

    Alan lang does it, pretty sure Julia does too along with other functional languages

  5. locked and limited conversation to collaborators on Feb 17, 2023
  6. converted this issue into a discussion #72 on Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions