Using put to update our state for a given state transaction can make it difficult to modify a given state based on its previous value. In this lesson we will explore a means to lift specialized functions that can be used modify our state’s value. These specialized functions must have the same type in their input and output. We take a look at a construction helper named modify that is used to remove the boilerplate of this type of construction. We also start to see how we can extend simple discrete stateful transactions to create other stateful transactions.

Modify state is apply a function to the original state to get new state:

// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modifyState(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)

Of couse, we have the 'modify' function already in the library, so the code can be changed to:

const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)

[Functional Programming Monad] Modify The State Of A State Monad的更多相关文章

  1. [Functional Programming] Read and Transform Values from a State ADT’s State (get)

    Many times we need to access and transform state, either in part or in full, to be used when calcula ...

  2. [Functional Programming Monad] Combine Stateful Computations Using A State Monad

    The true power of the State ADT really shows when we start combining our discrete, stateful transact ...

  3. [Functional Programming] Using ComposeK for both get State and modify State

    We have State like this: const state = { cards: [ { id: "green-square", color: "green ...

  4. [Functional Programming Moand] Update The State Of A State Monad (put)

    Stateful computations require the ability for their state to change overtime. We take a look on one ...

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

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

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

  7. Monad (functional programming)

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

  8. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

  9. [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)

    Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...

  10. [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)

    While sometimes outside input can have influence on how a given stateful transaction transitions, th ...

随机推荐

  1. H5游戏开发:贪吃蛇

    贪吃蛇的经典玩法有两种: 积分闯关 一吃到底 第一种是笔者小时候在掌上游戏机最先体验到的(不小心暴露了年龄),具体玩法是蛇吃完一定数量的食物后就通关,通关后速度会加快:第二种是诺基亚在1997年在其自 ...

  2. POJ 1845 Sumdiv (整数唯一分解定理)

    题目链接 Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25841   Accepted: 6382 Desc ...

  3. Java中分拣存储的demo

      //Letter.java package yzhou.map; /** * * @author 洋 * */ public class Letter { private String name; ...

  4. 234. Palindrome Linked List【Easy】【判断链表是否回文】

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. AndroidManifest.xml文件详解(uses-feature)

    http://blog.csdn.net/think_soft/article/details/7596796 语法(SYNTAX): <uses-featureandroid:name=&qu ...

  6. [Codeforces #494] Tutorial

    记录下一开始写错的两道水题 E: 先建出直径,然后在保证直径不变的情况下按照最大度数贪心就好了 注意一下一开始的特判 #include <bits/stdc++.h> using name ...

  7. AGC 022 C - Remainder Game

    题面在这里! 显然权值是 2^i 这种的话就是要你贪心,高位能不选就不选. 并且如果 % x 之后再去 % 一个>=x的数是没有用的,所以我们可以把操作的k看成单调递减序列. 这样的话就是一个有 ...

  8. [BalkanOI2016]Lefkaritika

    题目大意: 一个n*m的格子上有b个障碍物,现在让你往上面放正方形(长宽在格线上).问可以放多少种边长.位置不同的正方形? 思路: 很容易想到一个O(n^3)的暴力: 首先前缀和,然后枚举某一个顶点和 ...

  9. FreeMarker输出$

    FreeMarker如何输出$(美元符号)   使用${'$'} 如需要输出${user.id} 则${'$'}{user.id}

  10. Matlab中如何读取.dat文件

    处理movielens1M数据集 中间有个双冒号,直接用load的只读取第一列,这时候我们可以用dlmread(中间是l不是i),如下图,只需要提取1 3 5列就好了.