博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Functional Programming] Draw Items from One JavaScript Array to Another using a Pair ADT
阅读量:5264 次
发布时间:2019-06-14

本文共 6431 字,大约阅读时间需要 21 分钟。

We want to be able to pick nine random cards from an array of twelve cards, but can run into problems of keeping both the cards already draw and the cards left to draw from. Tracking two bits of state like this can create some hard to maintain argument gymnastics when creating our functions. Luckily we have a datatype Pair at our disposal that allows us to combine two values in to one value.

We will use this Pair type to model both a draw pile and a remaining pile, and take advantage of a couple special properties of Pair that will allow us to combine two Pair instances in a meaningful way by chaining. Just like we have done time and time again with the State ADT.

 

We have generated array of cards:

[   { id: 'orange-square', color: 'orange', shape: 'square' },  { id: 'orange-triangle', color: 'orange', shape: 'triangle' },  { id: 'orange-circle', color: 'orange', shape: 'circle' },  { id: 'green-square', color: 'green', shape: 'square' },  { id: 'green-triangle', color: 'green', shape: 'triangle' },  { id: 'green-circle', color: 'green', shape: 'circle' },  { id: 'blue-square', color: 'blue', shape: 'square' },  { id: 'blue-triangle', color: 'blue', shape: 'triangle' },  { id: 'blue-circle', color: 'blue', shape: 'circle' },  { id: 'yellow-square', color: 'yellow', shape: 'square' },  { id: 'yellow-triangle', color: 'yellow', shape: 'triangle' },  { id: 'yellow-circle', color: 'yellow', shape: 'circle' } ]

 

By the following code:

const {prop,assoc, pick, State, identity, omit, curry, filter, fanout, converge,map, composeK, liftA2, equals, constant,option, chain, mapProps, find, propEq, isNumber, compose, safe} = require('crocks');const  {
get, modify, of} = State; // #region generateCardsconst state = { colors: [ 'orange', 'green', 'blue', 'yellow' ], shapes: [ 'square', 'triangle', 'circle' ]};const getState = key => get(prop(key))const getColors = () => getState('colors').map(option([]))const getShapes = () => getState('shapes').map(option([]))const buildCard = curry((color, shape) => ({ id: `${color}-${shape}`, color, shape}));const buildCards = liftA2(buildCard)const generateCards = converge( liftA2(buildCards), getColors, getShapes)// #endregion

 

Now what we want to do is split cards array into a Pair,

on the left side pair is the selected card array,

on the rigth side pair is the unselected cards array.

// Splite Cards into two pars//[Selected Cards] - [UnSelected Cards]const getAt = index => array => array[index];const unsetAt = index => array => ([...array.slice(0, index), ...array.slice(index + 1)]);// Deck :: Pair [Card] [Card]// drawCardAt :: Integer -> [Card] -> Deckconst drawCardAt = index => fanout(    getAt(index),    unsetAt(index))console.log(    generateCards()        .map(drawCardAt(0))        .evalWith(state).fst() ) // { id: 'orange-square', color: 'orange', shape: 'square' }console.log(    generateCards()        .map(drawCardAt(0))        .evalWith(state).snd()    )/** [ { id: 'orange-triangle', color: 'orange', shape: 'triangle' },  { id: 'orange-circle', color: 'orange', shape: 'circle' },  { id: 'green-square', color: 'green', shape: 'square' },  { id: 'green-triangle', color: 'green', shape: 'triangle' },  { id: 'green-circle', color: 'green', shape: 'circle' },  { id: 'blue-square', color: 'blue', shape: 'square' },  { id: 'blue-triangle', color: 'blue', shape: 'triangle' },  { id: 'blue-circle', color: 'blue', shape: 'circle' },  { id: 'yellow-square', color: 'yellow', shape: 'square' },  { id: 'yellow-triangle', color: 'yellow', shape: 'triangle' },  { id: 'yellow-circle', color: 'yellow', shape: 'circle' } ]*/

Here we use '' to generate a Pair.

 

Notice that the left side pair is an object, not an array, we need to use 'bimap' to lift left side pair into Array. To do that,. we use ''

const drawCardAt = index => compose(    bimap(Array.of, identity),    fanout(        getAt(index),        unsetAt(index)    ))console.log(    generateCards()        .map(drawCardAt(0))        .evalWith(state).fst() ) // [{ id: 'orange-square', color: 'orange', shape: 'square' }]

 

---

const {prop,assoc, pick, bimap, State, identity, omit, curry, filter, fanout, converge,map, composeK, liftA2, equals, constant,option, chain, mapProps, find, propEq, isNumber, compose, safe} = require('crocks');const  {
get, modify, of} = State; // #region generateCardsconst state = { colors: [ 'orange', 'green', 'blue', 'yellow' ], shapes: [ 'square', 'triangle', 'circle' ]};const getState = key => get(prop(key))const getColors = () => getState('colors').map(option([]))const getShapes = () => getState('shapes').map(option([]))const buildCard = curry((color, shape) => ({ id: `${color}-${shape}`, color, shape}));const buildCards = liftA2(buildCard)const generateCards = converge( liftA2(buildCards), getColors, getShapes)// #endregion// Splite Cards into two pars//[Selected Cards] - [UnSelected Cards]const getAt = index => array => array[index];const unsetAt = index => array => ([...array.slice(0, index), ...array.slice(index + 1)]);// Deck :: Pair [Card] [Card]// drawCardAt :: Integer -> [Card] -> Deckconst drawCardAt = index => compose( bimap(Array.of, identity), fanout( getAt(index), unsetAt(index) ))console.log( generateCards() .map(drawCardAt(0)) .map(chain(drawCardAt(2))) .map(chain(drawCardAt(3))) .map(chain(drawCardAt(4))) .evalWith(state).fst() ) /**[ { id: 'orange-square', color: 'orange', shape: 'square' }, { id: 'green-square', color: 'green', shape: 'square' }, { id: 'green-circle', color: 'green', shape: 'circle' }, { id: 'blue-triangle', color: 'blue', shape: 'triangle' } ]*/console.log( generateCards() .map(drawCardAt(0)) .map(chain(drawCardAt(2))) .map(chain(drawCardAt(3))) .map(chain(drawCardAt(4))) .evalWith(state).snd() )/** [ { id: 'orange-triangle', color: 'orange', shape: 'triangle' }, { id: 'orange-circle', color: 'orange', shape: 'circle' }, { id: 'green-triangle', color: 'green', shape: 'triangle' }, { id: 'blue-square', color: 'blue', shape: 'square' }, { id: 'blue-circle', color: 'blue', shape: 'circle' }, { id: 'yellow-square', color: 'yellow', shape: 'square' }, { id: 'yellow-triangle', color: 'yellow', shape: 'triangle' }, { id: 'yellow-circle', color: 'yellow', shape: 'circle' } ]*/

 

转载于:https://www.cnblogs.com/Answer1215/p/10284743.html

你可能感兴趣的文章
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>