首页 \ 问答 \ 为什么要将Reader的构造函数参数定义为函数?(Why to define the constructor parameter of Reader as a function?)

为什么要将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
更新时间:2023-10-14 22:10

最满意答案

我认为您需要添加更多信息,但只需使用您所拥有的信息即可尝试这种方法

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。