Domain-specific languages vs. library of functions

前端 未结 8 1447
予麋鹿
予麋鹿 2021-02-07 08:13

This may be subjective, I don\'t know: I have this problem, which I\'m kind of equating to the \"what language for this project?\" question, since I can\'t seem to solve it.

8条回答
  •  温柔的废话
    2021-02-07 08:33

    Ultimately I would look at it this way: a DSL (like any programming language) is a set of lexical tokens and idioms that you can use to explain how to do something. Implicit within that set of tokens is a set of idioms.

    The idea of any language is that you select an appropriate one that makes it easier to express expected algorithms.

    A good example are reporting languages (eg Natural). That's because reports have common idioms like groups, breaking conditions and so on. Anyone who has done that in a general purpose language would tell you that it can be really tedious to do things like break conditions but it's certainly possible.

    So the question you should be asking is this: are there any common idioms that would be tedious, difficult or extremely verbose to express in a general purpose language? You should also be asking: is there a particular subset of functionality that you are most interested in that would benefit by making it a language concept? This could be things like vectors and matrices for linear algebra (with the appropriate operations) or the same for integral/differnetial calculus or differential equations.

    What you'd really like to have in the end with a DSL is where, say, a 500 line program could be express as 50-100 lines of your DSL and this applies for most "expected" algorithms. I guess it's a little like normalization in data modelling where the goal is to remove repeating groups. Writing the same 10-20 lines of code with a little variation suggests a common idiom.

    As for a library of functions, that is a more limited view of the same thing (ultimately) except that a library of function is merely a package of different behaviours. That ssumes you define a library of functions as just that and don't instead include complex object hierarchies, closures and so on. If you do then the line between a "library of functions" and a DSL is blurred.

    I would think of a library of functions as, say, the set of math functions that exist in most modern languages (sin, cos, square root and so forth). So I guess it comes down to:

    • Packages of code -> library of functions
    • Common idioms -> DSL

提交回复
热议问题