一起使用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
最满意答案
好的,找到解决方案。 刚刚下载了该驱动程序,并将其包含在我的应用程
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); }
相关问答
更多-
为什么“react-native link some-native-module”是一个彩票?(Why is “react-native link some-native-module” a lottery?)[2023-06-06]
我在几个月内没有遇到上述任何问题。 我想这与最近的一次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指定反应版本和反应原生版本(react-native init specify react version and react-native version)[2023-11-11]
创建一个项目: 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 ...
-
反应原生升级命令究竟在做什么?(What exactly is react-native upgrade command doing? particularly to the gradle files?)[2023-10-27]
upgrade命令旨在在现有项目中更新RN版本之后运行(并且在运行npm install因此新版本位于node_modules )。 本质上,该命令从应用程序模板复制所有文件,该模板用于初始化新的RN应用程序。 模板是运行react-native init命令时获得的模板。 这也是它在安装新RN版本后需要运行的原因,因为模板应用程序本身带有react-native依赖项。 它询问您是否要替换每个已修改文件的原因是它不知道内容已更改的原因。 初始化RN应用程序后,您可能自己对文件进行了更改。 如果您没有进行任 ... -
升级到版本0.56后,React Native崩溃。(React Native crashes after upgrade to version 0.56. Possible Babel issue?)[2022-04-22]
尝试在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 ...