mirror of
https://github.com/idris-lang/Idris2.git
synced 2025-01-05 15:08:00 +03:00
Merge pull request #368 from ohad/fstrings
Add a simple DYI f-string / string interpolation library
This commit is contained in:
commit
574524d8de
33
libs/contrib/Data/String/Interpolation.idr
Normal file
33
libs/contrib/Data/String/Interpolation.idr
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
||| A DYI version of 'string interpolation', mimicking Python 3's 'f-string' syntax
|
||||||
|
||| Not as fancy
|
||||||
|
module Data.String.Interpolation
|
||||||
|
|
||||||
|
import Data.Strings
|
||||||
|
|
||||||
|
namespace Data.String.Interpolation.Basic
|
||||||
|
%inline
|
||||||
|
public export
|
||||||
|
F : List String -> String
|
||||||
|
F strs = concat (strs)
|
||||||
|
|
||||||
|
namespace Data.String.Interpolation.Nested
|
||||||
|
%inline
|
||||||
|
public export
|
||||||
|
F : List (List String) -> String
|
||||||
|
F strss = F (concat strss)
|
||||||
|
|
||||||
|
{- Examples:
|
||||||
|
fstring : String
|
||||||
|
fstring = let apples = "apples" in
|
||||||
|
F["I have some ", apples," here."] --- cf. f"I have some {apples} here."
|
||||||
|
|
||||||
|
multiline : String
|
||||||
|
multiline = let name = "Edwin"
|
||||||
|
profession = "Hacker"
|
||||||
|
affiliation = "the University of St. Andrews" in --- cf.
|
||||||
|
F [["Hi ",name,". " ] --- f"Hi {name}. " \
|
||||||
|
,["You are a ",profession,". "] --- f"You are a {profession}. " \
|
||||||
|
,["You were in ",affiliation,"."] --- f"You were in {affiliation}."
|
||||||
|
]
|
||||||
|
|
||||||
|
-}
|
@ -24,6 +24,7 @@ modules = Control.Delayed,
|
|||||||
Data.SortedMap,
|
Data.SortedMap,
|
||||||
Data.SortedSet,
|
Data.SortedSet,
|
||||||
Data.String.Extra,
|
Data.String.Extra,
|
||||||
|
Data.String.Interpolation,
|
||||||
|
|
||||||
Debug.Buffer,
|
Debug.Buffer,
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user