以语义方式填充元组列表 [英] Populating a list of tuples in a semantic way
问题描述
我正在处理一段代码,我必须处理元组列表,其中元组的键( fst
s)的顺序和名称)匹配某个模板。我通过验证和(如果需要)基于输入生成有效列表来实现容错。
以下是我的意思的示例:
给定键的模板, [hello,world,this,is,a,test]
和一个列表 [(hello,Just 1),(world,Just 2),(test,Just 3)]
,传递给我的函数 validate
会导致验证失败 - 因为键的顺序和值与模板不匹配。
验证失败后,我想生成一个新列表,它看起来像 [(hello,Just 1),(world,Just 2),(这个,Nothing),(is,Nothing),(a,Nothing),(test,Just 3)]
。
我尝试使用(不完整的)列表理解来执行最后一步:
[(x,y)| x < - template,y <-1]
(显然,这是缺少步骤的地方空条目将被替换为 Nothing
s,并且假设输入的类型为 [(String,Maybe Int)]
这样做最简单的语义方式是什么?
你基本上想要将一个函数映射到你的字符串列表(你称之为模板),也就是
- 取一个字符串
xs
, - 返回
<如果一个整数n
与<$ $相关联,那么ul>
-
(xs,Just n)
-
(xs,Nothing)
否则,c $ c> xs 。
以下是一种可能的方法:
import Data.List(lookup)
import Control.Monad(join)
合并:: [String] - > [(String,Maybe Int)] - > [(String,Maybe Int)]
合并temp l = map(\ xs - >(xs,join $ lookup xs l))temp
然而,如果你建立一个 Map
: / p>
将限定的Data.Map导入为M
导入Data.Maybe(也许)
合并:: [String] - > [(String,Maybe Int)] - > [(String,Maybe Int)]
合并temp l = map(\cs - >(cs,M.lookup cs $ fromList'l))temp
fromList':: Ord a => [(a,Maybe b)] - > M.Map a b
fromList'xs = foldr insertJust M.empty xs
insertJust :: Ord a => (a,可能b) - > M.Map a b - > M.Map ab
insertJust(xs,maybeVal)mp = maybe mp(\\\
- > M.insert xs n mp)maybeVal
在GHCi中:
λ>让myTemplate = [hello,world,this,is,a,test]
λ>让myList = [(hello,Just 1),(world,Just 2),(test,Just 3)]
λ>合并myTemplate myList
[(hello,Just 1),(world,Just 2),(this,Nothing),(is,Nothing),(a,Nothing), (test,Just 3)]
I'm working on a piece of code where I have to process lists of tuples where both the order and names of the "keys" (fst
s of the tuples) match a certain template. I'm implementing fault tolerance by validating and (if needed) generating a valid list based on the input.
Here's an example of what I mean:
Given the template of keys, ["hello", "world", "this", "is", "a", "test"]
, and a list [("hello", Just 1), ("world", Just 2), ("test", Just 3)]
, passing it to my function validate
would cause it to fail validation - as the order and values of the keys do not match up with the template.
Upon failing validation, I want to generate a new list, which would look like [("hello", Just 1), ("world", Just 2), ("this", Nothing), ("is", Nothing), ("a", Nothing), ("test", Just 3)]
.
I tried performing this last step using an (incomplete) list comprehension:
[(x, y) | x <- template, y <- l]
(Obviously, this is missing the step where empty entries would be replaced with Nothing
s, and works under the assumption that the input is of type [(String, Maybe Int)]
).
What would be the easiest semantic way of doing this?
You essentially want to map a function to your list of strings (which you call "template"), i.e. the function that
- takes a string
xs
, - returns
(xs, Just n)
if an integern
is associated toxs
in your "list to validate",(xs, Nothing)
otherwise.
Here is one possible approach:
import Data.List ( lookup )
import Control.Monad ( join )
consolidate :: [String] -> [(String, Maybe Int)] -> [(String, Maybe Int)]
consolidate temp l = map (\xs -> (xs, join $ lookup xs l)) temp
However, you will get faster lookup if you build a Map
holding the key-value pairs of your association list (the "list to validate"):
import qualified Data.Map as M
import Data.Maybe (maybe)
consolidate :: [String] -> [(String, Maybe Int)] -> [(String, Maybe Int)]
consolidate temp l = map (\cs -> (cs, M.lookup cs $ fromList' l)) temp
fromList' :: Ord a => [(a, Maybe b)] -> M.Map a b
fromList' xs = foldr insertJust M.empty xs
insertJust :: Ord a => (a, Maybe b) -> M.Map a b -> M.Map a b
insertJust (xs, maybeVal) mp = maybe mp (\n -> M.insert xs n mp) maybeVal
In GHCi:
λ> let myTemplate = ["hello", "world", "this", "is", "a", "test"]
λ> let myList = [("hello", Just 1), ("world", Just 2), ("test", Just 3)]
λ> consolidate myTemplate myList
[("hello",Just 1),("world",Just 2),("this",Nothing),("is",Nothing),("a",Nothing),("test",Just 3)]
这篇关于以语义方式填充元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!