Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Haskell library for interpolated strings

There are many different libraries on Hackage dealing with interpolated strings. Some have poor quality while other vary with number of features they support.

Which ones are worth using?

Examples of libraries (in no particular order): shakespeare, interpolatedstring-qq, Interpolation

like image 293
Tener Avatar asked Jan 21 '12 21:01

Tener


People also ask

What is the syntax for string interpolation?

Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.

Can I use string interpolation?

Beginning with C# 10, you can use string interpolation to initialize a constant string. All expressions used for placeholders must be constant strings. In other words, every interpolation expression must be a string, and it must be a compile time constant.

Why is string interpolation used?

String Interpolation is a process in which the placeholder characters are replaced with the variables (or strings in this case) which allow to dynamically or efficiently print out text output. String Interpolation makes the code more compact and avoids repetition of using variables to print the output.

What is interpolation in flutter?

GestureForceInterpolation interpolation. The function used to convert the raw device pressure values into a value in the range 0.0 to 1.0. The function takes in the device's minimum, maximum and raw touch pressure and returns a value in the range 0.0 to 1.0 denoting the interpolated touch pressure.


1 Answers

I took a look at all the interpolation quasiquoter libraries I could find on Hackage.

Interpolation libraries worth using:

  • interpolatedstring-perl6: Supports interpolating arbitrary Haskell code with reasonable syntax, but requires haskell-src-exts. If you just want a general string interpolation syntax, I'd use this.

  • shakespeare-text: Based on the Shakespeare family of templates, and has minimal dependencies; most other interpolated string packages depend on haskell-src-exts, which is quite a heavy package and can take a lot of time and resources to compile. If you use any other Shakespeare templates, I'd suggest going with this one.

    However, it doesn't support interpolating arbitrary Haskell code, although it supports more than simple variable expansion; it also does function application, operators, etc. I think it also uses Text rather than String; I'm not sure whether it can be used with Strings looking from the source code, though there is support code to suggest it can be.

  • Interpolation: Supports arbitrary expressions (again with haskell-src-exts), and also has built-in looping facilities. If you want more "template"-like features than just plain interpolation, it's worth considering, although I personally find the syntax quite ugly.

Interpolation libraries probably not worth using:

  • interpolatedstring-qq: Seems to be based on interpolatedstring-perl6; it hasn't been updated for over a year, and seems to have less functionality than interpolatedstring-perl6. Unless you're really attached to the #{expr} syntax, I wouldn't consider this one.

  • interpol: Implemented as a preprocessor, gives {foo} special meaning in strings; IMO too heavyweight a solution, and with such lightweight syntax, likely to break existing code.

In summary, I'd suggest interpolatedstring-perl6 if you don't mind the haskell-src-exts dependency, and shakespeare-text if you do (or are already using Shakespeare templates).

Another option might be to use the string-qq package with a more general template engine; it supports String, Text and ByteString, which should cover every use. However, this obviously doesn't support embedding Haskell code, and you'll need to specify the variables separately, so it's probably only useful in certain situations.

like image 167
ehird Avatar answered Oct 15 '22 13:10

ehird