[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 ...
随机推荐
- centos中安装tomcat6
在centos中安装tomcat6 1)通过yum自动安装tomcat和dependences root@Centos_AAA ~]# yum install tomcat6 [root@Cent ...
- python脚本后台执行
在Linux中,可以使用nohup将脚本放置后台运行,如下: nohup python myscript.py params1 > nohup.out 2>&1 & 1 但 ...
- The .NET weak event pattern in C#
Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...
- OData查询ASP.NET Web API全攻略
本篇使用ASP.NET Web API来体验OData各种query. 首先是本篇即将用到的Model.使用的OData版本是4.0. public class Customer { public i ...
- Android Service总结04 之被绑定的服务 -- Bound Service
Android Service总结04 之被绑定的服务 -- Bound Service 版本 版本说明 发布时间 发布人 V1.0 添加了Service的介绍和示例 2013-03-17 Skywa ...
- Jquery 验证 validate
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...
- HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean arg0)理解
request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个 reqeust.getSession(fa ...
- 疑犯追踪第一季/全集Person Of Interest迅雷下载
本季Person of Interest Season 1 第一季(2011)看点:如今,<疑犯追踪>正在纽约热拍,在11月1日的片场,刚刚完成了一场爆炸的戏.另外,<探索者传说第一 ...
- Material Designer的低版本兼容实现(一)—— 简介 & 目录
很长一段时间没写东西了,其实是因为最近在研究Material Designer这个东西,熬夜熬的身体也不是很好了.所以就偷懒没写东西,这回开的这个系列文章是讲如何将Material Designer在 ...
- POI Excel表格合并,边框设置
RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb); RegionUtil.setBorderBottom(1, cellRangeAdd ...