[Functional Programming Monad] Map And Evaluate State With A Stateful Monad
We explore our first stateful transaction, by devising a means to echo our state value into the resultant for independent modification. With our state value in the resultant, we explore using mapto lift functions into our type as a means to modify the resultant.
In our exploration we find that not only can we change values of the resultant, but we can change the type as needed. We also find that we can combine subsequent map calls into function compositions, allowing us to clean up our code and combine our mappings to avoid excessive interactions with our State datatype.
To wrap it all up, we take a look at a handy State construction helper called get that we can use to query the state into resultant. This allows to read from our state for computations that are based on the value of a given state.
const { curry, compose, Pair, State, mapProps, composeK } = require("crocks");
const { get } = State;
// State s a
// We define State as a fixed type of state 's' or the left and a variable 'a' on the right
// (s -> (a, s))
// State Unit defined as Pair(a, s) with 'a' variable on the left and 's' on the right
// add :: Number -> Number -> Number
const add = x => y => x + y;
// pluralize :: (String, String) -> Number -> String
const pluralize = (single, plural) => num => `${num} ${Math.abs(num) === 1 ? single : plural}`;
// getState :: () -> State s
const getState = () => State(s => Pair(s, s))
// makeAwesome :: Number -> String
const makeAwesome = pluralize('Awesome', 'Awesomes')
// flow :: Number -> String
const flow = compose(
makeAwesome,
add(10)
)
console.log(
getState()
.map(flow)
.runWith(2)
.fst()
)
Get state constructor is so common, there is well made function call 'get' to replace our 'getState':
const { curry, compose, Pair, State, mapProps, composeK } = require("crocks");
const { get } = State;
// State s a
// We define State as a fixed type of state 's' or the left and a variable 'a' on the right
// (s -> (a, s))
// State Unit defined as Pair(a, s) with 'a' variable on the left and 's' on the right
// add :: Number -> Number -> Number
const add = x => y => x + y;
// pluralize :: (String, String) -> Number -> String
const pluralize = (single, plural) => num => `${num} ${Math.abs(num) === 1 ? single : plural}`;
// makeAwesome :: Number -> String
const makeAwesome = pluralize('Awesome', 'Awesomes')
// flow :: Number -> String
const flow = compose(
makeAwesome,
add(10)
)
console.log(
get()
.map(flow)
.runWith(2)
.fst()
)
[Functional Programming Monad] Map And Evaluate State With A Stateful Monad的更多相关文章
- [Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- [Functional Programming 101] Crocks.js -- when to use map and when to use chain?
As a beginner of Crocks.js, it was a problem for we to figure out when to use .map() and when to use ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
随机推荐
- string与double的互相转换
#include <iostream> #include <string> #include <sstream> string DoubleToString(dou ...
- 搜索引擎--范例:django之初试牛刀
这学期学了一门课<信息检索>,也就是传说中的搜索引擎 大作业自然而然的让我们自己做一个小型的搜索引擎.于是乎,我们本次的主题就诞生了 我也是边学边用,下面和大家一起分享我在这个过程中学到的 ...
- ZOJ-3822
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- 如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?
如果不考虑意外, 第一版最简单的构思如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import getopt, sys import subpro ...
- jquery插件需要明白的那些知识点
1.jquery中$是神马?$.fn又是神马? 稍微有jquery经验的都知道在jquery中$等价于jQuery,在控制台一试便知: 我们在jquery(1.8.3)源码中也能找到下面代码: 其实在 ...
- (五)agentd端cpu的触发器配置
配置===>模板===>选择对应的模板===> 这里我验证触发器是否有效,定义的触发器的值超过0.01就出发报警,这里我做的是最新的T值超过0.01就触发触发器 验证,说明触发器触发 ...
- Bot Framework测试
在开发完成Bot Framework后,在本机的模拟器都是成功的,但未知在发布后会出现什么样的问题,所以需要将本机发布的站点给到Bot 1.在Bot Framework注册一个Bot,打开Bot Fr ...
- centos 修改ftp目录
# usermod -d /home/www username // # service vsftpd restart // 重启vsftpd 两步搞定
- [thinkPHP] buildSql可以查看tp CURD操作对应的SQL
$goods = M('Goods')->where($map)->buildSql(); echo $goods;
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...