如何将字符列表转换为purescript中的字符串 [英] How do I convert a list of chars to a string in purescript
问题描述
我正在寻找一种惯用的方式来编写函数 List Char-> Purescript中的字符串
。
I'm looking for an idiomatic way to write a function List Char -> String
in Purescript.
这似乎很简单,但是我是Purescript的新手,已经浏览了一段时间了现在没有任何进展!
This seems like a simple thing to do, but I'm new to Purescript and have been browsing documentation for a while now with no progress!
背景信息:我正在将一个简单的函数从Haskell移植到Purescript
Background information: I am porting a simple function from Haskell to Purescript
generateId :: Int -> [Char]
这将生成指定长度的字符串。转换代码以使用 List Char
操作非常容易(其中 List
来自 Data.List
(在Purescript中)。在Haskell中, [Char]
与 String
相同,因此不需要其他处理,但是,我找不到一个将 List Char
转换为Purescript中的本地 String
的函数!
This generates a String of specified length. It was quite easy to convert the code to use List Char
operations (where List
is from Data.List
in Purescript). In Haskell [Char]
is the same as String
so no other processing is needed, however, I can't find a function to convert from List Char
to a native String
in Purescript!
我的搜索将我带到 fromCharArray :: Array Char->
,但是我找不到从 Data.String
中的String List Char
到 Array Char
!
My search lead me to fromCharArray :: Array Char -> String
in Data.String
, however I could not find a way to convert from List Char
to an Array Char
!
我可以通过折叠 List来手动在它们之间进行转换Char
并使用 snoc
构建一个 Array Char
,但是肯定我必须缺少一个内置的Purescript中似乎基本的String操作的解决方案!
I could manually convert between them by folding over List Char
and building an Array Char
using snoc
, but surely I must be missing an inbuilt solution for what seems like basic String manipulation in Purescript!
编辑: fromList
可以从任何不可折叠
(例如数组)到 List
的列表。万一有一个更惯用的方法可以解决这个问题,那么这个问题仍然没有解决。
fromList
works to convert from any Unfoldable
(such as Arrays) to a List
. Still leaving this question open in case there is a more idiomatic way of achieving this.
推荐答案
我同意您的编辑。 Data.String.fromCharArray<<< Data.List.fromList
对我来说听起来不错。 fromCharArray
是使用 array.join()
在本机JS中实现的。
I agree with your edit. Data.String.fromCharArray <<< Data.List.fromList
sounds pretty decent to me. fromCharArray
is implemented in native JS with array.join("")
.
更新::现在已弃用 fromList
-改为使用 toUnfoldable
Update: fromList
is deprecated now - use toUnfoldable
instead
这篇关于如何将字符列表转换为purescript中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!