From ac1fa288906fe28c8cd6815f8c12251ad79d7987 Mon Sep 17 00:00:00 2001 From: Ohad Kammar Date: Fri, 26 Jun 2020 16:52:37 +0100 Subject: [PATCH] Add a simple DYI f-string / string interpolation library Apparently the concrete syntax is controversial ("{apples}" vs. "$oranges"), so I'm just adding a simple DYI version until we agree on a concrete syntax --- libs/contrib/Data/String/Interpolation.idr | 33 ++++++++++++++++++++++ libs/contrib/contrib.ipkg | 1 + 2 files changed, 34 insertions(+) create mode 100644 libs/contrib/Data/String/Interpolation.idr diff --git a/libs/contrib/Data/String/Interpolation.idr b/libs/contrib/Data/String/Interpolation.idr new file mode 100644 index 000000000..4a5d1f901 --- /dev/null +++ b/libs/contrib/Data/String/Interpolation.idr @@ -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}." + ] + +-} diff --git a/libs/contrib/contrib.ipkg b/libs/contrib/contrib.ipkg index 983bfbd52..f738102bf 100644 --- a/libs/contrib/contrib.ipkg +++ b/libs/contrib/contrib.ipkg @@ -24,6 +24,7 @@ modules = Control.Delayed, Data.SortedMap, Data.SortedSet, Data.String.Extra, + Data.String.Interpolation, Debug.Buffer,