MakeMap expands the input call arguments into a comprehension that transforms each element in the input to produce an output list. There are two call patterns supported by map: .map( , ) .map( , , ) In the second form only i
(eh ExprHelper, target ast.Expr, args []ast.Expr)
| 495 | // In the second form only iterVar values which return true when provided to the predicate expression |
| 496 | // are transformed. |
| 497 | func MakeMap(eh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 498 | v, found := extractIdent(args[0]) |
| 499 | if !found { |
| 500 | return nil, eh.NewError(args[0].ID(), "argument is not an identifier") |
| 501 | } |
| 502 | accu := eh.AccuIdentName() |
| 503 | if v == accu || v == AccumulatorName { |
| 504 | return nil, eh.NewError(args[0].ID(), "iteration variable overwrites accumulator variable") |
| 505 | } |
| 506 | |
| 507 | var fn ast.Expr |
| 508 | var filter ast.Expr |
| 509 | |
| 510 | if len(args) == 3 { |
| 511 | filter = args[1] |
| 512 | fn = args[2] |
| 513 | } else { |
| 514 | filter = nil |
| 515 | fn = args[1] |
| 516 | } |
| 517 | |
| 518 | init := eh.NewList() |
| 519 | condition := eh.NewLiteral(types.True) |
| 520 | step := eh.NewCall(operators.Add, eh.NewAccuIdent(), eh.NewList(fn)) |
| 521 | |
| 522 | if filter != nil { |
| 523 | step = eh.NewCall(operators.Conditional, filter, step, eh.NewAccuIdent()) |
| 524 | } |
| 525 | return eh.NewComprehension(target, v, accu, init, condition, step, eh.NewAccuIdent()), nil |
| 526 | } |
| 527 | |
| 528 | // MakeFilter expands the input call arguments into a comprehension which produces a list which contains |
| 529 | // only elements which match the provided predicate expression: |
no test coverage detected