a thoughtful web.
Good ideas and conversation. No ads, no tracking.   Login or Take a Tour!
comment by rob05c
rob05c  ·  3443 days ago  ·  link  ·    ·  parent  ·  post: Dynamically Declaring Functions and Methods

C# has lambdas which can be 'dynamically declared.'

  delegate int del(int i);
  static void Main(string[] args)
  {
      del myDelegate = x => x * x;
      int j = myDelegate(5); //j = 25
  }
You can also pass around the lambda/delegate to other functions, for them to call it.

    define parts of the function once in the database assemble it as a string at run time and execute it on the fly

That is technically possible in C#. But I don't recommend it. It's very dangerous. What would happen if your database somehow got "Directory.Delete("C:\Windows\system32")" in it?

Alternatively, consider adding a scripting language to your program (I recommend Lua). That would let you store scripts as strings in the database, and execute them in a controlled environment. You can ensure scripts don't have the ability to do bad things like delete files, and that they only have access to the C# functions you expose.

    "Does this have anything to do with the 'enlightenment' that I see all over the place when Lisp developers talk about loving their language so much?"

Yes. LISP does what you want fantastically. Everything you're asking for: homoiconicity, lambdas, first-class functions, partial application. But that is a deep rabbit hole. I definitely recommend diving down it someday. But for now, to actually accomplish your project, I'd stick with C#, and either embed a scripting language or use C# lambdas (possibly both).





paxprose  ·  3443 days ago  ·  link  ·  

Thank you so much, rob05c. My co-worker got a huge kick out of:

| Everything you mention but don't know the terms for:

"He's schooling you, brah!"

"Yeah. Obviously I fucking need it. I can't even articulate in the proper vernacular what I'm trying to do."

psudo  ·  3443 days ago  ·  link  ·  

If you're interested in exploring functional programming in the CLR you can give F# a try.

rob05c  ·  3443 days ago  ·  link  ·  

Heh, yeah, sorry, I was trying to come up with a way to phrase it without sounding condescending >_>.

paxprose  ·  3443 days ago  ·  link  ·  

I didn't interpret it like that at all. Thanks a ton. I believe I'm going to attempt to learn and implement lambdas in C# and see how it goes.