[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 ...
随机推荐
- 最简单的DLL
静态链接库与动态链接库都是共享代码的方式,如果采用静态链接库,则无论你愿不愿意,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了.但是若使用 DLL,该 DLL 不必被包含在最终 EXE ...
- Spring JdbcTemplate框架搭建及其增删改查使用指南
Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...
- 2.如何使用python连接hdfs
总所周知,python是一门很强大的语言,主要在于它有着丰富的第三方模块,当然连接hdfs的模块也不例外. 在python中有一个模块也叫hdfs,可以使用它连接hadoop的hdfs.直接pip i ...
- flask学习:如何从config里载入配置
代码如下: 1.main.py from flask import Flask from config import DevConfig app=Flask(__name__) app.config. ...
- 准备开发开放API接口
准备开发APP开放接口,允许JQUERY直接调用http://blog.csdn.net/wuxiangege/article/details/52238968 SIGN的设计与实现http://bl ...
- 25,Spark Sort-Based Shuffle内幕彻底解密
一:为什么需要Sort-Based Shuffle? 1, Shuffle一般包含两个阶段任务: 第一部分:产生Shuffle数据的阶段(Map阶段,额外补充,需要实现ShuffleManager中 ...
- Java中导入导出Excel -- POI技术
一.介绍: 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实 ...
- 最小生成树的Prim算法
构造最小生成树的Prim算法 假设G=(V,E)为一连通网,其中V为网中所有顶点的集合,E为网中所有带权边的集合.设置两个新的集合U和T,其中集合U用于存放G的最小生成树的顶点,集合T用于 ...
- Codeforces 855C - Helga Hufflepuff's Cup
855C - Helga Hufflepuff's Cup 题意 要求构建一棵树,树上至多可以存在 \(x\) 个权值为 \(k\) 的重要点,且与重要点连边的点的权值必须小于 \(k\),问有多少种 ...
- 洛谷——P1586 四方定理
P1586 四方定理 题目描述 四方定理是众所周知的:任意一个正整数nn,可以分解为不超过四个整数的平方和.例如:25=1^{2}+2^{2}+2^{2}+4^{2}25=12+22+22+42,当然 ...