首页 \ 问答 \ 一起使用Maybe和Writer(Using Maybe and Writer together)

一起使用Maybe和Writer(Using Maybe and Writer together)

这是我的鸡蛋包装厂:

type Eggs = Int
data Carton = Carton Eggs deriving Show

add :: Eggs -> Carton -> Maybe Carton
add e (Carton c)
    | c + e <= 12 = Just (Carton $ c + e)
    | otherwise = Nothing

main = do
    print $ pure(Carton 2) >>= add 4 >>= add 4 >>= add 3

似乎工作得很好,我可以很好地链接add功能。

但我想记录每一步添加多少鸡蛋的日志。 所以我这样做:

import Control.Monad.Writer

type Eggs = Int
data Carton = Carton Eggs deriving Show

add :: Eggs -> Carton -> Writer [String] (Maybe Carton)
add e (Carton c)
    | c + e <= 12 = do
        tell ["adding " ++ show e]
        return (Just (Carton $ c + e))
    | otherwise = do
        tell ["cannot add " ++ show e]
        return Nothing

main = do
    let c = add 4 $ Carton 2
    print $ fst $ runWriter c
    mapM_ putStrLn $ snd $ runWriter c

这给了我想要的东西:我可以看到生成的纸箱和添加4个鸡蛋的记录。

但是,我似乎失去了像我之前做的那样链接add函数的能力:

let c = pure(Carton 2) >>= add 4 -- works
let c = pure(Carton 2) >>= add 4 >>= add 2 -- does not work

我怎样才能链接我的新作家功能的add功能? 有没有更好的方法来做到这一点?


Here is my egg packing factory:

type Eggs = Int
data Carton = Carton Eggs deriving Show

add :: Eggs -> Carton -> Maybe Carton
add e (Carton c)
    | c + e <= 12 = Just (Carton $ c + e)
    | otherwise = Nothing

main = do
    print $ pure(Carton 2) >>= add 4 >>= add 4 >>= add 3

Seems to work well, I can nicely chain add functions.

But I want to record a log of how many eggs were added at every step. So I do this:

import Control.Monad.Writer

type Eggs = Int
data Carton = Carton Eggs deriving Show

add :: Eggs -> Carton -> Writer [String] (Maybe Carton)
add e (Carton c)
    | c + e <= 12 = do
        tell ["adding " ++ show e]
        return (Just (Carton $ c + e))
    | otherwise = do
        tell ["cannot add " ++ show e]
        return Nothing

main = do
    let c = add 4 $ Carton 2
    print $ fst $ runWriter c
    mapM_ putStrLn $ snd $ runWriter c

This gives me what I want: I can see the resulting carton and the record for 4 eggs being added.

But I seem to have lost the ability to chain add functions like I did before:

let c = pure(Carton 2) >>= add 4 -- works
let c = pure(Carton 2) >>= add 4 >>= add 2 -- does not work

How can I chain my new writer-enabled add functions? Is there a better way of doing this?


原文:https://stackoverflow.com/questions/38194378
更新时间:2023-08-11 08:08

最满意答案

好的,找到解决方案。 刚刚下载了驱动程序,并将其包含在我的应用程

import Websocket from "../Applications/websocket_client"

export default class SecWatt extends Component {
    constructor(props){
        super(props);
    }

    componentDidMount(){
        webs = new Websocket();
        webs.setEventCallback(printEvent);
        webs.initialize("wss://*MattermostURL*/api/v3/users/websocket" , "YourToken");
    }
    render()
    {
        return(
            <View>
            </View>
        );
    }    
}

function printEvent(msg){
        console.log(msg);
    }

Ok, found the solution. Just downloaded this driver and include it in my App:

import Websocket from "../Applications/websocket_client"

export default class SecWatt extends Component {
    constructor(props){
        super(props);
    }

    componentDidMount(){
        webs = new Websocket();
        webs.setEventCallback(printEvent);
        webs.initialize("wss://*MattermostURL*/api/v3/users/websocket" , "YourToken");
    }
    render()
    {
        return(
            <View>
            </View>
        );
    }    
}

function printEvent(msg){
        console.log(msg);
    }

相关问答

更多
  • 我在几个月内没有遇到上述任何问题。 我想这与最近的一次RN升级有关。 今天我从0.45升级到0.47,没有任何问题。 难道是RN中的某些东西是固定的,这些问题消失了吗? 我当然希望如此。 I haven't had any of the above problems in a couple of months. I suppose it has to do with one of the recent RN upgrades. Today I upgraded from 0.45 to 0.47 with ...
  • HighCharts与DOM协同工作。 您无法直接在React Native中使用HighCharts或绘制依赖于DOM的库,但可以在带有React Native的WebView中使用图表库。 我没有专门使用High Charts,但是使用了WebView的其他图表库,并且效果很好。 HighCharts works with the DOM. You cannot use HighCharts or charting libraries that depend on the DOM directly in ...
  • 如果您想使用没有人编写React Native组件的本React Native组件,您将自己完成。 对大多数事情来说并不是很难。 您可以在链接的文档中学习如何为iOS和Android执行此操作。 If you want to use a native component that nobody wrote a React Native component for, you will to do it yourself. It's not very hard for most things. You can ...
  • 创建一个项目: react-native init MyNewApp --version 0.45.0 如果你想要改变版本然后尝试这个: npm install --save react-native@0.44.0 npm install --save react@16.0.0-alpha.6 Create a project : react-native init MyNewApp --version 0.45.0 If you want do change version then try wit ...
  • 好的,找到解决方案。 刚刚下载了该驱动程序,并将其包含在我的应用程 import Websocket from "../Applications/websocket_client" export default class SecWatt extends Component { constructor(props){ super(props); } componentDidMount(){ webs = new Websocket(); ...
  • React Native正在快速更新平台。 每个版本都有很多错误修复,新的性能改进以及新的或升级的组件。 如果它可行且可行,我建议您升级最新的稳定版本。 您可以查看升级React Native的文档以获取有关如何升级的详细信息。 由于您的版本相当陈旧,因此可能会有很多更改。 我建议你检查每个库(如果你使用的话),如果它们与新版本的react-native兼容,以及你应该如何升级库。 反应原生的v0.40有一些重大变化。 React Native is rapidly updating platform. E ...
  • 我使用了fetch方法并使用RESTgul API连接到Parse。 以下是一些可能对您有所帮助的链接: https://www.parse.com/docs/rest/guide http://updates.html5rocks.com/2015/03/introduction-to-fetch I have used fetch method and connected to Parse using the RESTgul API. Here are some links that may help ...
  • upgrade命令旨在在现有项目中更新RN版本之后运行(并且在运行npm install因此新版本位于node_modules )。 本质上,该命令从应用程序模板复制所有文件,该模板用于初始化新的RN应用程序。 模板是运行react-native init命令时获得的模板。 这也是它在安装新RN版本后需要运行的原因,因为模板应用程序本身带有react-native依赖项。 它询问您是否要替换每个已修改文件的原因是它不知道内容已更改的原因。 初始化RN应用程序后,您可能自己对文件进行了更改。 如果您没有进行任 ...
  • 尝试在package.json中更新babel-preset-react-native的版本,如下所示: "babel-preset-react-native": "^5" 。 Try to update the version forbabel-preset-react-native in your package.json like so: "babel-preset-react-native": "^5".
  • 问题是/Users/bhushanraut/.npm-packages/bin/目录未添加到全局PATH变量。 您需要将其添加到PATH变量。 在终端中执行以下命令 vi ~/.bash_profile 这将在vi编辑器中打开该文件。 在该文件中附加以下行。 export PATH=$PATH:'/Users/bhushanraut/.npm-packages/bin/' 然后保存并关闭该文件。 执行以下命令 source ~/.bash_profile 现在尝试执行react-native in ...

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。