Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Expression Trees [closed]

Tags:

Is there an equivalent of .net's Expression Trees that underly LINQ for the JVM? I would like to implement some LINQ like code structures in Scala and I am wondering if I have to roll my own expression tree library also.

Update: I am not interested in a linq equivalent itself. .net has a large set of expression tree tools that make it easy to dynamically compile code at runtime can have it be callable from your code. The project I want to undertake has no relation to databases. Expression tree's provide an easy way to describe code that operates on data.

If there is no library my other option I think is to create one that emits byte code.

like image 272
Steve Severance Avatar asked Sep 25 '09 20:09

Steve Severance


People also ask

How do you evaluate an expression tree in Java?

We can evaluate an expression tree by applying the operator at the root to values obtained by recursively evaluating left and right subtrees. This can be easily done by traversing the expression tree using postorder traversal. The algorithm can be implemented as follows in C++, Java, and Python: C++

What are the rules to construct expression trees?

If we get an operand in the given expression, then push it in the stack. It will become the root of the expression Tree. If an operator gets two values in the expression, then add in the expression tree as its child, and push them in the current node.

What is expression tree in Java?

The expression tree is a tree used to represent the various expressions. The tree data structure is used to represent the expressional statements. In this tree, the internal node always denotes the operators. The leaf nodes always denote the operands. The operations are always performed on these operands.

How do you solve expression tree?

Solution Approach: A simple solution to the problem is by performing one operation each from root, for operends we will solve the subtree. As all operations are binary the nodes of a tree either have two childrens or none. We will use recursion to solve each node's binary operation.


1 Answers

Since 2.10, Scala has macros, which give you access to abstract syntax trees of their arguments at compile-time: http://scalamacros.org/. Here are some examples of their usage including a sketch of LINQ: http://scalamacros.org/paperstalks/2013-12-02-WhatAreMacrosGoodFor.pdf.

like image 180
Eugene Burmako Avatar answered Oct 13 '22 01:10

Eugene Burmako