[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 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)的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- 第二章 Getting started with functional programming
Getting started with functional programming 开始函数式编程 higher-order functions-高阶函数 所有FP语言的主要特点是函数可以像普通值 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
随机推荐
- foj Problem 2282 Wand
Problem 2282 Wand Accept: 432 Submit: 1537Time Limit: 1000 mSec Memory Limit : 262144 KB Prob ...
- [bzoj3122][SDOI2013]随机数生成器 ——BSGS,数列
题目大意 给定递推序列: F[i] = a*F[i-1] + b (mod c) 求一个最小的i使得F[i] == t 题解 我们首先要化简这个数列,作为一个学渣,我查阅了一些资料: http://d ...
- 【IDEA】IDEA下maven项目无法提示和使用EL表达式的解决办法
今天在IDEA创建web项目之后发现无法使用EL和JSTL, 一.如果JSP中无法自动提示EL表达式,比如${pageContext.request.contextPath},可在pom.xml的&l ...
- 【SSH原理】ssh用法及命令
什么是SSH? Secure Shell 安全外壳协议 简单说,SSH是一种网络协议,用于 计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登 ...
- VS MFC RADIO控件 选择
我们假设有两个RADIO控件:IDC_RADIO_SINGLE和IDC_RADIO_RANGE,我们的目的是默认选种IDC_RADIO_SINGLE控件. 方法一: CheckRadioButton( ...
- shiro多realm验证之——shiro实现不同身份使用不同Realm进行验证(转)
转自: http://blog.csdn.net/xiangwanpeng/article/details/54802509 (使用特定的realm实现特定的验证) 假设现在有这样一种需求:存在两张表 ...
- Font Awesome 字体使用方法, 兼容ie7+
WebFont 技术可以让网页使用在线字体,而无需使用图片,从而有机会解决开头设计师提到的问题.它通过 CSS 的@font-face语句引入在线字体,使用 CSS 选择器指定运用字体的文本,与此同时 ...
- aliyun
阿里云启动不了网站 1 将网站的目录属性-安全中加入IUSER_计算机名字的访问权限 和 加入NER SERVICE的访问权限 2 IIS打开网站属性--目录--执行权限改为顺脚本 3 ...
- NYOJ16 矩形嵌套 【DAG上的DP/LIS】
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c ...
- Educational Codeforces Round 31 B. Japanese Crosswords Strike Back【暴力】
B. Japanese Crosswords Strike Back time limit per test 1 second memory limit per test 256 megabytes ...