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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  5. [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 ...

  6. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  7. [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 ...

  8. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  9. 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 ...

随机推荐

  1. 【POI2017||bzoj4726】Flappy Birds

    外国人很良心的啊,这题比NOIP那题还简单…… 不用管他最后的位置,因为移动的次数肯定是恒定的,所以维护在每一个柱子的位置能飞到的范围,递推下去即可. #include<bits/stdc++. ...

  2. 解决 Android Studio 错误:需要常量表达式

    1. 2.Android Studio快捷键

  3. python带cookie提交表单自动登录

    import urllib import urllib2 import cookielib login_url = "xxxxxxxxxxxxx" cj = cookielib.C ...

  4. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club

    Doki Doki Literature Club Time Limit: 1 Second      Memory Limit: 65536 KB Doki Doki Literature Club ...

  6. 树链剖分【p2568】[SDOI2011]染色

    Description 给定一颗有\(n\)个节点的无根树和\(m\)个操作,操作有\(2\)类: 1.将节点\(a\)到节点\(b\)路径上所有点染成颜色\(c\) 2.询问节点\(a\)到节点\( ...

  7. 简单DP【p3399】丝绸之路

    Background 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲 ...

  8. dutacm.club Water Problem(矩阵快速幂)

    Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Tot ...

  9. CSS中的层叠、特殊性、继承、样式表中的@import

    CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...

  10. 微信小程序开发教程(四)线程架构与开发步骤

    线程架构 从前面的章节我们可以知道,.js文件是页面逻辑处理层.我们可以按需在app.js和page.js中添加程序在生命周期的每个阶段相应的事件.如在页面的onLoad时进行数据的下载,onShow ...