Many times we need to access and transform state, either in part or in full, to be used when calculating other state transitions. We will look at how we can leverage the get function on the State ADT to read and modify portions of our application state, putting the result in the Resultant portion. We will create two transactions: one to select a card from a list of cards and another to access the hint portion of our state.

The code below is just trying to get 'cards' and 'hint' props from an object with two fns.

const {prop, State, option,chain, find, propEq, isNumber, compose, safe} = require('crocks');
const {get, modify} = State; const state = {
cards: [
{id: 'green-square', color: 'green', shape: 'square'},
{id: 'orange-square', color: 'orange', shape: 'square'},
{id: 'blue-square', color: 'blue', shape: 'triangle'}
],
hint: {
color: 'green',
shape: 'square'
}
} // getState :: String -> State Object (Maybe a)
const getState = key => get(prop(key)) const findById = id => find(propEq('id', id)); // getCard :: String -> State Object Card
const getCard = id => get(prop('cards'))
.map(chain(findById(id)))
.map(option({id: 'urk', color: '', shape: ''})) const getCard2 = (id) => compose(option({}), chain(findById(id)), prop('cards')) const getHint = () => getState('hint').map(option({
color: 'yay',
shape: 'wow'
})); const res = getCard('green-square')
.evalWith(state) // { id: 'green-square', color: 'green', shape: 'square' } const res1 = get(getCard2('green-square')) //{ id: 'green-square', color: 'green', shape: 'square' }
.evalWith(state) const res2 = getHint()
.evalWith(state); // { color: 'green', shape: 'square' }
console.log(res)
console.log(res1)
console.log(res2)

The important thing to understand is the difference between  'getCard' & 'getCard2' functions. They have the same functionalities.

const getCard = id => get(prop('cards'))
.map(chain(findById(id)))
.map(option({id: 'urk', color: '', shape: ''}))

Once we use 'get', it creates a State instance, therefore we have to use instance method 'map' to access its value.

const getCard2 = (id) => compose(option({}), chain(findById(id)), prop('cards'))    

By the time we define 'getCard2', we haven't lift it into State with 'get' function. We can do normal fp. The reason using 'chian' in both functions is because prop('cards') return a Maybe, findById return a Maybe, would be a nested Maybe type, so using 'chain' to flatten it.

[Functional Programming] Read and Transform Values from a State ADT’s State (get)的更多相关文章

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

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

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

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

  4. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

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

  6. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  7. Monad (functional programming)

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

  8. 第二章 Getting started with functional programming

    Getting started with functional programming 开始函数式编程 higher-order functions-高阶函数 所有FP语言的主要特点是函数可以像普通值 ...

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

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

随机推荐

  1. vue的main.js

    import Vue from 'vue'; import App from './App.vue'; //================http 请求======================= ...

  2. Linux下mknod详解

    mknod - make block or character special filesmknod [OPTION]... NAME TYPE [MAJOR MINOR] option 有用的就是 ...

  3. hdu 4359 dp

    /* 题目大意:给n个节点的二叉树第i个节点的权值为2^(i-1), 求所有含左右子树的节点都符合左子树的权和小于右子树权和的种数. */ #include <iostream> #inc ...

  4. 【SSH原理】ssh用法及命令

    什么是SSH? Secure Shell  安全外壳协议 简单说,SSH是一种网络协议,用于 计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登 ...

  5. 推送通知iOS客户端编写实现及推送服务器端编写

    http://blog.csdn.net/tonny_guan/article/details/8963262 1.iOS客户端编程 推送通知技术在Mac OS X和iOS系统上都可以运行,我们本章主 ...

  6. 华为上机测试题(水仙花数升级版-java)

    PS:这题满分100,没有做对,大家帮忙看看问题在哪 /* * 题目:水仙花数升级版  * 描述: 水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身.(例如:1 ...

  7. sh脚本变量赋值时同时执行命令时的环境问题

    在v2ex看到一个问题: 允许在一个命令之前立即发生一个或多个变量赋值,这些赋值为跟随着的命令更改环境变量,这个赋值的影响是暂时的. 那为什么: int=100 int=10 echo $(($int ...

  8. Java异常throws与throw的区别

    throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象.谁调用谁处理: 抛出异常throws: ...

  9. UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)

    VJ题目链接 题意: 知道n(n <= 8e6)个工作的完成所需时间q和截止时间d,你一次只能做一个工作.问最多能做多少工作? 思路: 首先很像贪心.观察发现如下两个贪心性质: 1)一定存在一个 ...

  10. 【linux高级程序设计】(第十二章)Linux多线程编程 2

    线程同步机制 互斥锁通信机制 int pthread_mutex_init (pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mute ...