[Functional Programming] Using JS, FP approach with Arrow or State Monad
Using Naive JS:
const {modify, get} = require('crocks/State');
const K = require('crocks/combinators/constant');
const B = require('crocks/combinators/composeB');
const assign = require('crocks/helpers/assign');
const prop = require('crocks/Maybe/prop');
const option = require('crocks/pointfree/option');
// rando : Integer -> State Object Float
const rando = x => {
const seed = ( * x + ) & 0x7fffffff
const value = (seed >>> ) / 0x7fff;
return modify(assign({seed})) // update the seed Pair(_, seed)
.map(K(value)); // update the value Pair(value, seed)
}
// pullRandom :: Integer -> State Object Float
const pullRandom = defSeed =>
get(s => pluckSeed(defSeed)).chain(rando);
// pluckSeed :: Integer -> Object -> Integer
const pluckSeed =
def => B(option(def), prop('seed'))
module.exports = {
pullRandom
};
The main problem is inside 'rando' function, not really a FP way doing stuff.
Arrow approach:
const {modify, get} = require('crocks/State');
const Arrow = require('crocks/Arrow');
const K = require('crocks/combinators/constant');
const B = require('crocks/combinators/composeB');
const assign = require('crocks/helpers/assign');
const branch = require('crocks/Pair/branch');
const prop = require('crocks/Maybe/prop');
const option = require('crocks/pointfree/option');
const merge = require('crocks/pointfree/merge');
// calcSeed :: Arrow Integer
const calcSeed = Arrow(x => ( * x + ) & 0x7fffffff);
// value :: Arrow (Pair Integer) -> Pair (Integer Float)
const value = Arrow(x => (x >>> ) / 0x7fff).second();
// genRandom :: Arrow Integer Object
const genRandom = calcSeed
.map(branch)
.compose(value)
.map(p => [p.fst(), p.snd()])
// rando : Integer -> State Object Float
const rando = x => {
const [seed, value] = genRandom.runWith(x);
return modify(assign({seed})) // update the seed Pair(_, seed)
.map(K(value)); // update the value Pair(value, seed)
}
// pullRandom :: Integer -> State Object Float
const pullRandom = defSeed =>
get(s => pluckSeed(defSeed)).chain(rando);
// pluckSeed :: Integer -> Object -> Integer
const pluckSeed =
def => B(option(def), prop('seed'))
module.exports = {
pullRandom
};
It becomes complex with we need to do Pari and Arrow.
State version:
const {modify, get} = require('crocks/State');
const B = require('crocks/combinators/composeB');
const assign = require('crocks/helpers/assign');
const prop = require('crocks/Maybe/prop');
const option = require('crocks/pointfree/option');
//initialState :: GameState
const initialState = {
deck: [],
seed:
}
// newSeed :: Int -> INt
const newSeed = seed => ( * seed + ) & 0x7fffffff;
// calcValue :: Int -> Float
const calcValue = seed => (seed >>> ) / 0x7fff;
// pluckSeed :: Integer -> GameState -> Integer
const pluckSeed =
def => B(option(def), prop('seed'));
// getSeed :: () -> State GameState Int
const getSeed = () => get(pluckSeed({seed: }));
// putSeed :: Int -> State GameState ()
const putSeed = seed => modify(assign({seed}));
// genSeed :: () -> State GameState ()
// get default seed
// map to a new seed
// update current seed in state
const genSeed = () =>
getSeed()
.map(newSeed)
.chain(putSeed);
// evaluate :: () -> State GameState Float
const evaluate = () =>
getSeed()
.map(calcValue);
// pullRandom :: () -> State GameState Float
const pullRandom = () =>
genSeed()
.chain(evaluate);
console.log(
pullRandom()
.runWith(initialState)
)
The idea is easier to follow and thinking in a way of state transition.
[Functional Programming] Using JS, FP approach with Arrow or State Monad的更多相关文章
- [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction
We have the ability to select a single random card from a pile of twelve cards, but we would like to ...
- 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 ...
- 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 ...
- 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)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- 第二章 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 ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
随机推荐
- Jack--10天学会IOS大纲;注意将图片放大观看!
第一天:磨刀霍霍期 耐得住性子好好熟悉和认识开发环境 ---------Jack/版权全部 认识开发环境 操作系统认识 Mac系统是苹果机专用系统.是基 ...
- Lodop客户端本地和集中打印 [是否安装][操作系统]
前面有些博文或图片介绍了这两种方式,可能文字太多,耐心看完的人不多,这里简略描述+图片的方式解释一下. 客户端本地打印: 用户访问某网站,用自己本地的打印机打出来. 集中打印方式: 用户访问某网页,用 ...
- git 拉取和获取 pull 和 fetch 区别
使用Git 直接提交的话 直接 push 获取最新版本 有两种 拉取 和 获取 pull 和 fetch git pull 从远程拉取最新版本 到本地 自动合并 merge ...
- java系列之 原生数据类型
在我看来,java里面里面除了原生类型不是对象,其他的都是对象.但java是面向对象的语言,很多地方还要要操作对象,所以java会自动把原生类型转为对应的包装类型.这个过程叫自动装箱.有装箱就有拆箱, ...
- Revit API遍历全部风管,找到与风管相关的墙开洞
涉及向量计算,求相交等相关技术. ) { foreach (Face face in solid.Faces) ...
- 集群服务器下使用SpringBoot @Scheduled注解定时任务
原文:https://blog.csdn.net/huyang1990/article/details/78551578 SpringBoot提供了 Schedule模块完美支持定时任务的执行 在实际 ...
- FreeCommander 学习手册
概述 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3470728.html FreeCommander(下文简称FC),是Windows下面比较强大的文 ...
- Android Service总结02 service介绍
Android Service总结02 service介绍 版本 版本说明 发布时间 发布人 V1.0 介绍了Service的种类,常用API,生命周期等内容. 2013-03-16 Skywang ...
- tms mqtt
tms mqtt 功能概述 MQTT客户端组件 可用于VCL,FMX和LCL应用 支持Windows,iOS,Android,macOS,Linux,Raspberry Pi 实现完整的MQTT规范, ...
- Nginx 的动态 upstreams
原文地址: http://www.oschina.net/translate/nginx-with-dynamic-upstreams 我最近在工作中做一个设置,我有一个面向用户的 Nginx 服务, ...