Giter VIP home page Giter VIP logo

Comments (3)

antonmedv avatar antonmedv commented on August 11, 2024

This is a nice idea, I'm also thinking about adding sort and reduce functions. Right now predicates takes only one arg via # symbol. I think something like #1 #2, will be cool.

sort(Tweets, #1.Len < #2.Len)

from expr.

antonmedv avatar antonmedv commented on August 11, 2024

It was more complicated than I thought.

For sort package we need to provide a comparison function Less. In Expr there are no closures and #1 > #2 predicates are not true functions. We can compile such predicates separately, but this lead to more question: how to do binding on other variables? What if in predicate someone uses a variable defined in an expression? Adding proper function to Expr is not a solution, as it will make the language turn complete and break always terminating promise.

As we can't create a function from a predicate we can implement a sorting algorithm inside the Expr language itself! I started programming this approach with the quicksort algorithm. All only utilizing opcodes from expr VM. It got complicated very quickly. I started to research the "sort" problem and here are a few conclusions I ended up with:

  • Sort function should not mutate incoming array (side-effect-free promise), but always create a new copy of the array.
  • Predicates actually make understanding ordering more complicated. Compare #1 > #2, #2 < #1. What order is going to be? asc and desc are much more easy to understand.
  • 90% of the time, people just want to sort an array of objects (structs or maps) by a field.
    So writing #1.Value > #2.Value requires duplicating the field name twice.

I've added two functions: sort() and sortBy(field string).

Here are a few examples (using the pipe operator) of them:

values | sort()
values | sort('desc')
tweets | sortBy('Len')
tweets | sortBy('Len', 'desc')

from expr.

wvell avatar wvell commented on August 11, 2024

I tried looking into it but also got stuck in the opcodes. Thank you! I will test it.

from expr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.