[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 ...
随机推荐
- error : cannot open source file "SDKDDKVer.h"
属性->VC++ Directions-> include path 中加了:$(IncludePath) library path 中加了:$(LibraryPath)
- HDU1285 裸的拓扑排序
拓扑排序: 拓扑排序是应用于有向无回路图(DAG)上的一种排序方式,对一个有向无回路进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u在v的前面.该序列说明了顶点表示的事件或 状态发生 ...
- 【转】beyond compare 启动提示“应用程序发生错误”
[转]beyond compare 启动提示“应用程序发生错误” 今天到公司BCompare不能打开,重新安装也不能打开.最后处理下,就解决了.方法是把C:\Documents and Setti ...
- jquery offset tip
/* * 这是一张 JavaScript 代码草稿纸. * * 输入一些 JavaScript,然后可点击右键或从“执行”菜单中选择: * 1. 运行 对选中的文本求值(eval) (Ctrl+R): ...
- 行为型设计模式之模板方法(Template Method)
结构 意图 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Te m p l a t e M e t h o d 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性 一次性 ...
- 推荐几个好用的PHP集成开发环境
(转自:http://blog.sina.com.cn/s/blog_5bd6b45101011bu2.html ) 分类: PHP PHP新手在准备正式开始写PHP代码的时候,不幸的是被PHP的开发 ...
- linux系统调用实现代码分析【转】
转自:http://linux.chinaunix.net/doc/kernel/2001-07-30/637.shtml 启动早就读完,现在为了写笔记再从启动之后粗略的大体读一遍,基本就是几个大模块 ...
- 9.OpenStack安装web界面
安装仪表板 安装仪表板组件 yum install -y openstack-dashboard httpd mod_wsgi memcached python-memcached 编辑/etc/op ...
- [译]java8新特性:函数式编程(functional programming)的优点
Java8引入了函数式编程,他对java是一个极大的扩展.Java从此不在是一个单纯的面向对象语言,现在他同时混合了函数式编程.这是巨大的改变,需要我们调整面对对象的编程习惯,以适应这些变化. 但是为 ...
- Appium+python自动化10-AVD 模拟器【转载】
前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面