Giter VIP home page Giter VIP logo

Comments (2)

eduard-vasinskyi avatar eduard-vasinskyi commented on September 22, 2024

Hi, @inshua

You can create an expression for a new local variable using the Expression#let method.

To create a for cycle you can omit creating a local variable i manually and use the Expression#iterate method that generates a for cycle for you.

Let’s say you have an interface Counter:

public interface Counter {
  int countSum();
}

and you want to generate a class that implements countSum() like this:

public int countSum() {
  int sum = 0;
  for (int i = 0; i < 100; i++) {
     sum += i;
  }
  return sum;
}

You can do this with the following expression:

Counter counter = ClassBuilder.create(Counter.class)
     .withMethod("countSum",
           let(value(0), sum ->
                 sequence(
                       iterate(
                             value(0),
                             value(100),
                             i -> set(sum, add(sum, i))),
                       sum
                 )))
     .defineClassAndCreateInstance(DefiningClassLoader.create());

First, you create a local variable sum and set it to 0. That is let(value(0), sum -> …). Then you iterate from 0 to 100 and set sum to be equal to a sum of i and sum.

You need to use the sequence() expression and pass sum as the last argument in order to instruct the method that it needs to return the sum in the end.

The above example refers to the master-5.5 branch. let(), value(), sequence(), iterate(), set(), add() methods are imported from the Expressions class.

from activej.

inshua avatar inshua commented on September 22, 2024

@eduard-vasinskyi thank you for response.

Got it, I understand why it's so designed now, this will make a life scope for a variable, like sum in above. Thank you.

from activej.

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.