Home > OS >  How to write a recursive anonymous function in Dart
How to write a recursive anonymous function in Dart

Time:09-04

Lets say I wanted to write a recursive anonymous function to calculate factorial values.

print(((int a) => a == 1? 1 : a * this(a - 1))(4)); I would expect this to print 24, which is 4! (this function is obviously prone to issues with negative numbers, but that's beside the point)

The problem is that this doesn't refer to the anonymous function in order to make a recursive call.

Is this something that's possible in dart? I've seen it in python before, where a function is assigned to a variable with the walrus operator ( := ) and is also recursive.

Here is an example that creates a list of the average value on each level of a binary tree: return (get_levels := lambda l: ([mean(node.val for node in l)] get_levels([child for node in l for child in [node.left, node.right] if child])) if l else [])([root])

As you can see, the lambda is called get_levels. It calculates the average of the current level, then makes a recursive call on the next level of the binary tree and appends it to the list of previous level averages.

The closest that I could come up with is this:

var getLevels;
List<double> averageOfLevels(TreeNode? root) {
  return root == null ? [] : (getLevels = (List<TreeNode> level) => level.isNotEmpty ? <double>[level.map((node) => node.val).fold(0, (int l, int r) => l r) / level.length]   getLevels([for(var node in level) ...[node.left, node.right]].whereType<TreeNode>().toList()) : <double>[])([root]);
}

But, as you can see, this required an additional line where the variable is defined ahead of time. Is it possible to achieve something more similar to the python example using callable classes?

CodePudding user response:

There's a classic Lisp/Scheme problem of how to create a recursive lambda. The same technique of creating one anonymous function that takes itself as an argument and then using another anonymous function to pass the first anonymous function to itself can be applied to Dart (albeit by sacrificing some type-safety; I can't think of a way to strongly type a Function that takes its own type as an argument). For example, a recursive factorial implementation:

void main() {
  var factorial = (Function f, int x) {
    return f(f, x);
  }((Function self, int x) {
    return (x <= 1) ? 1 : x * self(self, x - 1);
  }, 4);

  print('4! = $factorial'); // Prints: 4! = 24
}

All that said, this seems like a pretty contrived, academic problem. In practice, just create a named function. It can be a local function if you want to avoid polluting a global namespace. It would be far more readable and maintainable.

Is it possible to achieve something more similar to the python example using callable classes?

I'm not sure where you're going with that since Dart neither allows defining anonymous classes nor local classes, so even if you made a callable class, it would violate your request for being anonymous.

  •  Tags:  
  • dart
  • Related