Skip to content

(Idea) Trick for converting Ruby blocks to SymEngine formulae #41

Description

@zverok

OK, it is not the time for it currently, just an idea for future.

Usage:

puts SymEngine.formula {|x, y| x**2 + y**2}
# => x**2 + y**2

Naive implementation:

def SymEngine.formula &block
  block.parameters.empty? and fail(ArgumentError, "Block should list all expected symbols")
  block.parameters.map(&:first).uniq == [:opt] or fail(ArgumentError, "Only plain parameters are supported (no defaults, no *rest, no keyword args)")
  symbols = block.parameters.map(&:last).map(&SymEngine::Symbol.method(:new))
  block.call(*symbols)
end

There's almost no "dangerous" magics (with method_missing, instance_eval and stuff), and seems pretty useful?..

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions