[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 ...
随机推荐
- eclipse中git的使用
首先在Eclipse中安装EGit插件,如下图: 1. 2.点击Add 3. 4. 5. 给Eclipse安装插件很少遇到没被屏蔽的,这是一个.安装过程并不长,稍候即可. 安装成功之后我们就可以使用了 ...
- rest项目的基础返回类设计
package com.hmy.erp.api.vo; import java.io.Serializable; import lombok.Data; /** * erp基本状态返回类 * * @a ...
- webdiyer aspnet pager最近又用这个。还是记录下。
这个是页面里的代码需要在上面引入: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer&quo ...
- CCAction详解
http://blog.csdn.net/bailongvip/article/details/7895370 转载自雨松MOMO程序研究院 上一章我们了解了CCNode的实现原理,这次我跟大家探讨一 ...
- Xen虚拟化技术中PV和HVM的区别
转自 这里 Xen是一个开源的type-1或者裸机管理程序,它使得一个物理主机能够同时并行运行多个相同的或者不同的操作系统实例.Xen是目前唯一的开源可得的type-1管理程序.Xen被应用于许多商业 ...
- java的io操作(将字符串写入到txt文件中)
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...
- gitlib 安装
参考文件https://www.cnblogs.com/rslai/p/9109624.html
- Selenium2+python自动化29-js处理多窗口【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/js/ 前言 在打开页面上链接的时候,经常会弹出另外一个窗口(多窗口情况前面这篇有讲解: ...
- 【原创】SQL Server Job邮件详细配置
1 简介 SQL Server 代理具有发送电子邮件的功能.您可以配置 SQL Server 代理邮件,使其在出现下列情况时向预定义的操作员发送电子邮件: 警报触发时.可以配置警报,以针对所发生的特定 ...
- .net core 发布iis 错误
点击iis功能,例如 点击log日志,提示xxx路径下的web.config错误 百度之后 安装NET Core Windows Server Hosting ->DotNetCore.2.0. ...