为什么要将Reader的构造函数参数定义为函数?(Why to define the constructor parameter of Reader as a function?)
在学习Reader Monad时,我发现它被定义为:
newtype Reader r a = Reader { runReader :: r -> a } instance Monad (Reader r) where return a = Reader $ \_ -> a m >>= k = Reader $ \r -> runReader (k (runReader m r)) r
我想知道为什么使用函数作为构造函数参数而不是其他东西,如元组:
newtype Reader r a = Reader { runReader :: (r, a) } instance Monad (Reader r) where -- Here I cannot get r when defining return function, -- so does that's the reason that must using a function whose input is an "r"? return a = Reader (r_unknown, a) m >>= k = Reader (fst $ runReader m) (f (snd $ runReader m))
根据Reader的定义,我们需要一个“环境”来生成“价值”。 我认为Reader类型应该包含“environment”和“value”的信息,所以元组看起来很完美。
When learning the Reader Monad, I find that it is defined as:
newtype Reader r a = Reader { runReader :: r -> a } instance Monad (Reader r) where return a = Reader $ \_ -> a m >>= k = Reader $ \r -> runReader (k (runReader m r)) r
I want to known why using function as constructor parameter instead of something else such as a tuple:
newtype Reader r a = Reader { runReader :: (r, a) } instance Monad (Reader r) where -- Here I cannot get r when defining return function, -- so does that's the reason that must using a function whose input is an "r"? return a = Reader (r_unknown, a) m >>= k = Reader (fst $ runReader m) (f (snd $ runReader m))
According to the Reader definition, we need a "environment" which we can use to generate a "value". I think a Reader type should contain the information of "environment" and "value", so the tuple seems perfect.
原文:https://stackoverflow.com/questions/42310931
最满意答案
我认为您需要添加更多信息,但只需使用您所拥有的信息即可尝试这种方法
Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{ $_.server = (($_.server).split("(")[0]).Trim() $_ }
我们导入csv数据并分配标头。 如果您已经有一个,则可以省略此参数。
然后我们将每行数据作为对象进行检查。 通过将其分隔为空格来更改
server
数据。 如果此数据用于服务器名称,则可以安全地假设第一个空格之前的所有内容都是服务器名称。 这种方法取决于那里的空间。 我们也可以使用相同的逻辑(
但如果空间是保证,这将更容易。因此,我们更新
server
,然后使用$_
将数据发送回管道。示例输出
server 1 2 3 4 ------ - - - - GDR01W01SQ004 1 4 63 NY-TER-PLN-P-5N GDR01L02D001 4 12 129 CO-FDP-STE-NP-5N
根据评论进行编辑
由于它是服务器显示名称,因此我根据“(”)将逻辑更改为拆分。还使用
Split()
方法而不是-split
运算符。I think you need to add more information but just using what you have let try this one approach
Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{ $_.server = (($_.server).split("(")[0]).Trim() $_ }
We import the csv data and assign a header. If you already have one then this parameter can be omitted.
Then we examine each row of data as an object. Change the
server
data by splitting it up by its spaces. If this data is for server names then it is safe to assume that that everything before the first space is the server name. This approach is dependent on the space being there. We could also use the same logic with the(
but this would be easier if the space was a guarantee.So we update the
server
and then send the data back down the pipe with$_
.Sample Output
server 1 2 3 4 ------ - - - - GDR01W01SQ004 1 4 63 NY-TER-PLN-P-5N GDR01L02D001 4 12 129 CO-FDP-STE-NP-5N
Edit based on comments
Since it is a server display name I changed the logic to split based on the "(". Also using the
Split()
method instead of the-split
operator.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
转到Find-> Replace ...并启用正则表达式。 替换: (.{2}) 使用: $1SPACE SPACE是一个空间。 Go to Find->Replace... and enable regular expressions. Replace: (.{2}) With: $1SPACE Where SPACE is a space.
-
由于回复评论很糟糕: gsub('\\$|,', '', d) 用空字符串替换$或( | )。 Since answering in the comments is bad: gsub('\\$|,', '', d) replaces either $ or (|) , with an empty string.
-
您正在寻找的是以下结构: STRING_1 (?:(?!STRING_2)[\s\S])*? STRING_3 (?:(?!STRING_2)[\s\S])*? STRING_2 仅当STRING_3存在于其间时,这才匹配STRING_1到STRING_2 (该技术称为调节贪婪令牌 )。 您也可以将它放在一行中: STRING_1(?:(?!STRING_2)[\s\S])*?STRING_3(?:(?!STRING_2)[\s\S])*?STRING_2 请参阅regex101.com上的演示 。 W ...
-
[regex]::Match()只返回第一个匹配。 使用[regex]::Matches()来捕获所有匹配: $s = Get-Content 'C:\app.config' [regex]::Matches($s, '{{([^/)]+)}}') |ForEach-Object { $_.Groups[1].Value } [regex]::Match() only returns the first match. Use [regex]::Matches() to capture all matche ...
-
尝试, dim str as string str = "I like apples (because I don't like pears)." str = split(split(str, ")")(0), "(")(1) debug.print str 嵌套的Split( split(str, ")")(0) )将字符串转换为具有两个元素的基于零的数组; 例如I like apples (because I don't like pears和I like apples (because I don' ...
-
使用Powershell删除两个字符之间的字符串中的文本(Removing text in a string between two characters using Powershell)[2022-02-20]
我认为您需要添加更多信息,但只需使用您所拥有的信息即可尝试这种方法 Import-Csv C:\temp\test.csv -Header server,1,2,3,4 | ForEach-Object{ $_.server = (($_.server).split("(")[0]).Trim() $_ } 我们导入csv数据并分配标头。 如果您已经有一个,则可以省略此参数。 然后我们将每行数据作为对象进行检查。 通过将其分隔为空格来更改server数据。 如果此数据用于服务器名称,则 ... -
Substring()返回一个新的字符串实例,并保持当前实例不变。 因此,您的调用只会创建一个新的临时字符串,但由于您没有对其执行任何操作,因此会立即将其丢弃。 要使更改成为现实,您需要将Substring()的结果分配回文本框,如下所示: phoneNumberTextBox.Text = phoneNumberTextBox.Text.Substring(0, phoneNumberTextBox.Text.Length - 2); 确保首先检查字符串的长度,以避免处理短字符串(少于2个字符) ...
-
你可以这样做: text = text.replaceAll("\\'([$a-zA-Z]+)\\.", "'").replaceAll("'", ""); text.replaceAll("\\'([$a-zA-Z]+)\\.", "'")给你: data['high'] && data['low'] || data['now'] 和.replaceAll("'", ""); 会给你想要的结果(没有' )。 data[high] && data[low] || data[now] You can ...
-
您没有提到您正在执行此操作的上下文... VBA过程,Access中的查询,来自Access会话外部的查询。 但是,如果您可以使用Split()函数,这可能非常简单。 从立即窗口考虑此示例。 strUrl = "http://websiteurl.com/sometext-STUFFIWANT-foo-bar" varPieces = Split(strUrl, "-") ? varPieces(1) STUFFIWANT 既然你说你想在查询中这样做,我想你正在寻找这样的东西...... SELECT ...