Question about conditionals.
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 61
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
Hi Emanuel-
Looking at how to handle conditionals, as not everything maps to linear functions. I'd really like a fn (a,b,c) which returns b if a else c, or functionality equivalent to a ? b : c
I don't see anything supporting that in the framework. Lunchtime hacking turned up the below.
If this approach is good, it may be worth including in examples. If not, what is the recommended approach, and is a ternary operator a reasonable feature request?
Thanks!
Duncan
context.RegisterLogical("<=", (a, b) => a <= b, Precedence.Addition);
context.RegisterLogical(">", (a, b) => a > b, Precedence.Addition);
context.RegisterBinary("if",(a,b) => (b != 0) ? a : 0, Precedence.Addition);
MathExpr expr = new MathExpr("({x} if {x} <= 0.5) + (({x} - 0.5) if ({x} > 0.5))", context);
var fn = expr.Compile("x");
Debug.Assert(IsNear(fn(0.0), 0));
Debug.Assert(IsNear(fn(0.75), 0.25));
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MathExpr and Context registration calls shown in the issue, then inspect how operators and conditional expressions are parsed and compiled. Determine whether the project should support a ternary operator, recommend the shown custom-operator approach, or add an example; done requires a maintainer-approved direction and the corresponding implementation or example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100