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. junit单元测试+junit与Spring结合

    配置:右键要加入单元测试的工程,选择properties,然后选择java build path,选择add library,选择junit即可. 编写:右键要测试的class,new一个junit ...

  2. Java易错知识点(1) - 关于ArrayList移除元素后剩下的元素会立即重排

    帮一个网友解答问题时,发现这样一个易错知识点,现总结如下: 1.易错点: ArrayList移除元素后,剩下的元素会立即重排,他的 size() 也会立即减小,在循环过程中容易出错.(拓展:延伸到所有 ...

  3. heightCharts改变图表内图标的样式:链接外部图片

    series: [{ name: '东京', marker: { symbol: 'url(https://www.highcharts.com/demo/gfx/sun.png)'     //链接 ...

  4. SQL按多个字段排序时的实现规则

    1.在使用SQL中的ORDER BY按照表中的多个列对表做排序是,会按照第一个列的排序条件作为排序基准,当第一个列的值都相同时,才会按照后面的列的排序条件作为排序基准: 案例如下: 图一和图二展示的是 ...

  5. C++类中引用成员和常量成员的初始化(初始化列表)

    如果一个类是这样定义的: Class A { public: A(int pram1, int pram2, int pram3); privite: int a; int &b; const ...

  6. Java基础:GC机制

    上一节,简单的介绍了java当中的内存模型,那么经常和内存模型一起提到的JAVA垃圾回收机制当然也需要在这里一并的总结一下. 所谓是垃圾回收机制,用通俗的话来说,就是将那些没有被任何变量引用的实例对象 ...

  7. linux 批量替换所有文件中包含的字符串

    sed -i "s/原来字符串/新字符串/g" `grep 原来字符串 -rl .` 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 ...

  8. ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】

    #1579 : Reverse Suffix Array 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strong data structure ...

  9. HDU 多校1.12

  10. [CF460E]Roland and Rose

    题意:给定$n$和$r$,要找$n$个整点,使得他们两两距离的平方和最大,并且所有点到原点的距离必须小于$r$ 很容易猜到答案在凸包上然后暴力找,但证明还是挺妙的 首先转化一下距离平方和 令$\vec ...