Code Monkey home page Code Monkey logo

Comments (8)

RayMckaig avatar RayMckaig commented on June 28, 2024 1

Just wanted to let you know AnyExpression works a treat. Minor tweaks to my code and it now deals with String concatenation.

Nice work and thank you for your help.

from expression.

nicklockwood avatar nicklockwood commented on June 28, 2024

This is complicated. The Expression framework only deals with numeric values, so it can't directly manipulate strings, however you can cheat by using a lookup table to map Doubles to strings and manipulate them that way. I'm doing that in another project.

I'll see if I can create a minimal example for you. Do your expressions need to work with any types other than Strings and numbers?

from expression.

RayMckaig avatar RayMckaig commented on June 28, 2024

I understand how complicated it can be. Unfortunately the project I'm working on is a dumbed down programming language supporting numbers (works), booleans (works), strings and dates.

The string and date methods I think can be dealt with by just adding functions, even concatenation could just be translated to a function call before calling the expression evaluator although it would be convienient to just be able to use +, but & (vba, vb) could be an alternative. It's the handling of the values in the expression and the return type that seems to be problematic. I need to be able to return strings, dates, etc. from an expression.

from expression.

nicklockwood avatar nicklockwood commented on June 28, 2024

I have a class in the Layout Project called AnyExpression which is a wrapper for Expression that handles mapping of arbitrary types. It's fairly self-contained, so perhaps you could use that?

https://github.com/schibsted/layout/blob/master/Layout/AnyExpression.swift

You should just need to copy Optional+Layout.swift and the stringify() utility function from the project, and it should have no other dependencies IIRC.

from expression.

RayMckaig avatar RayMckaig commented on June 28, 2024

That sounds ideal.

Could you give me a couple of examples of how I would go about mapping a date type and perhaps doing something like 'This is a test concatenating: ' + variable + ' with some text'?

When I get home, I'll look into using this wrapper.

Thanks for your responses :~)

from expression.

nicklockwood avatar nicklockwood commented on June 28, 2024

AnyExpression's version of the SymbolEvaluator function is (Any) throws -> Any instead of (Double) throws -> Double, so mapping data types is pretty simple, you just need to cast the result to the type you expect it to be.

This should work out of the box:

let expression = AnyExpression("'This is a test concatenating: ' + variable + ' with some text'")
let result = try expression.evaluate() as! String

There aren't any built-in operators for dealing with strings, apart from "+" for concatenation, so if you want substring functions, etc you'll need to supply them yourself using the symbols dictionary.

The latest version of Expression supports the subscript operator, but only for integer indexes, and it doesn't really support member functions, so for now you'll have to do something like substring(string, range) rather than string[range] or string.substring(range).

from expression.

RayMckaig avatar RayMckaig commented on June 28, 2024

Sounds like a workable plan.

Thank you for your help.

from expression.

nicklockwood avatar nicklockwood commented on June 28, 2024

@RayMckaig FYI, as of 0.11.0 AnyExpression is now included as part of the Expression library.

from expression.

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.