[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 ...
随机推荐
- 我的一次安装oracle的过程
1.在装oracle之前,先安装.net3.5 2.然后正常安装oracle,一直next 3.装完oracle后,安装plsql dev工具,打开工具,发现没有connect as,是需要进行一些配 ...
- 【 学习笔记 】memcached基础知识
源地址:http://kb.cnblogs.com/page/42731/ 仔细学习了下,以下是记录的笔记备忘内容. 一.memcached是什么? memcached是高性能的分布式内存缓存服 ...
- 错误”ORA-12560: TNS: 协议适配器错误“解决方法
错误”ORA-12560: TNS: 协议适配器错误“解决方法 原本是可以通过sqlplus连接数据库的,命令如下: C:\Users\xuwei>sqlplus /nolog SQL*Plus ...
- SPOJ-913
Query on a tree II Time Limit: 433MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Su ...
- 关于 Notepad++ 崩溃之后正在编辑文件内容被清空的致命问题的补救措施
Notepad++ 以其功能强大.界面简洁.操作简单方便.超低内存耗用而受众多挨踢从业者青睐. Notepad++ 不像 UE 那样在你编辑的时候会定时生成 bak 备份文件.虽然 Notepad++ ...
- docker从零开始(三)服务初体验docker compose
决条件 安装Docker 1.13或更高版本. 获取Docker Compose.在适用于Mac的Docker和适用于Windows的Docker上,它已预先安装,因此您可以随意使用.在Linux系统 ...
- Java多态机制和继承中重写重载
关于Java中多态机制 http://www.cnblogs.com/chenssy/p/3372798.html 这篇博文讲的很透彻 大体意思是 多态定义: 多态就是指程序中定义的引用变量所指向的具 ...
- C3P0连接池一些基本配置
C3P0连接池配置 数据库连接是一个耗费大量资源且相当慢的操作,所以为了提高性能和连接速度,诞生了连接池这样的概念. 在多用户并发操作过程中,连接池尤为重要. 它是将那些已连接的数据库连接存放在一个容 ...
- 开源的图像滤镜库----for Android
1.GPUImage for Android(推荐使用) GPUImage基于OpenGL实现的各种各样图像滤镜(图像处理)效果,多达50多种, idea源于GPUImage for iOS,基本囊括 ...
- 大数模板(Java)
大数加法 /* 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B ...