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. Linux命令之rhn_check

    NAME rhn_check - check for queued actions on RHN and execute them SYNOPSIS /usr/sbin/rhn_check [-v] ...

  2. js剪贴板操作

    这是一个很有意思的地方,很多不了解的人或者初级的js编程者会觉得很不可思议,js都那么强大了,访问剪贴板一个粘贴复制,大概就是一行命令的事情,但是事实如此,js对于访问本地计算机的剪贴板的支持其实是十 ...

  3. SD卡给MCU升级

    目 录1. 前言2. 初识BootLoader2.1 百度百科的BootLoader2.2 BootLoader的简单理解2.3 BootLoader的作用3. BootLoader预备知识3.1 复 ...

  4. 行为型设计模式之备忘录模式(Memento)

    结构 意图 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 适用性 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时 ...

  5. OpenGL入门学习(三)

    http://developer.178.com/201103/94954704639.html 在第二课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点 ...

  6. C++ 播放音频流(PCM裸流)--改善

    直接上代码,如果有需要可以直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用了(注意将声道和频率与你自己的文件对应).当然我自己也用VS2008写了个例子上传了,如果有需要下载地址如下: ...

  7. 12.OpenStack镜像和存储服务配置

    配置镜像服务 编辑 /etc/glance/glance-api.conf与/etc/glance/glance-registry.conf添加以下内容 [DEFAULT] notification_ ...

  8. hdu 5142(数学-进制转换)

    NPY and FFT Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. spoj 913 Query on a tree II (倍增lca)

    Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...

  10. oracle case else end

    --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索函数 CASE WHEN sex = '1' ...