[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 ...
随机推荐
- Tomcat学习笔记(十二)
Host和Engine容器 Context容器的父容器通常是Host容器. Engine容器表示Catalina的整个servlet引擎.如果使用Engine容器,那么它总是处于容器层级的最顶层.默认 ...
- getprop 获取android系统属性
Android属性系统 property_get/property_set (很透彻)http://www.blogjava.net/MEYE/articles/359773.html getpro ...
- stringutil的方法
StringUtils 源码,使用的是commons-lang3-3.1包. 下载地址 http://commons.apache.org/lang/download_lang.cgi 以下是Stri ...
- 用ServletContext做一个简单的聊天室
这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问.由此可见ServletContext比cookie和session的作用范围要大[ ...
- SyntaxError: Unexpected token ' in JSON at position 1
刚才遇到一个问题: 在公司两个不同的环境竟然会出现错误 对比一下两个环境的数据,发现一个传来的是双引号的JSON字符串,一个是单引号.而当我用JSON.parse()时,就会报错!! 原因是JSON. ...
- 转:Java NIO(2)
java.nio包是Java在1.4之后增加的,用来提高I/O操作的效率.在nio包中主要包括以下几个类或接口: * Buffer:缓冲区,用来临时存放输入或输出数据. * Charset:用来把Un ...
- 51nod 1583 犯罪计划——矩阵乘法优化dp
文泽想在埃及做案n次,并且想在最后不用得到惩罚.案件的被分成几种类型.比如说,案件A,当案件A被重复犯两次时,案件A将被认为不是犯罪案件,因此犯案人不用得到惩罚.也就是说,案件A被犯偶数次时,犯案人将 ...
- switch case语句的用法
Java语言 switch支持部分基本数据类型(primitive data types),如:byte.short.int.long.char:不支持boolean.float.double. 如图 ...
- Html5学习进阶三 Input 类型
HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: email url number range Date ...
- 总结DSP28335的程序设计的方法
对DSP进行开发时,需要对其底层的硬件及外设进行相应的配置,当配置完成后才可以将其相应模块激活,才可以在其内部进行程序编写及调试处理.下面对程序配置及操作进行简单的整理,仅供参考. 第一步:初始化系统 ...