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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  4. 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 ...

  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)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

  7. 第二章 Getting started with functional programming

    Getting started with functional programming 开始函数式编程 higher-order functions-高阶函数 所有FP语言的主要特点是函数可以像普通值 ...

  8. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  9. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

随机推荐

  1. Timer triggered DMA transfer - Delay between requesting the DMA transfer

    Hello, I'm working with a STM32F407 controller board.   Right now, I want to trigger a DMA transfer ...

  2. 根据twitter的snowflake算法生成唯一ID

    C#版本 /// <summary> /// 根据twitter的snowflake算法生成唯一ID /// snowflake算法 64 位 /// 0---0000000000 000 ...

  3. 微信emoji表情编码 、MySQL 存储 emoji 表情符号字符集

    相关资料 微信emoji表情编码 微信用户名显示「emoji表情」 PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码) MySQL 存储emoji表情 MySQL 存储 e ...

  4. Revit Family API 添加类型

    FamilyManager.NewType("");添加新类型,然后设置参数,就是为新类型设置参数. [TransactionAttribute(Autodesk.Revit.At ...

  5. 加快Qemu Aarch32虚拟开发板的启动速度

    软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...

  6. Delphi XE2 compiler performance

    原文: http://blog.barrkel.com/2011/10/delphi-xe2-compiler-performance.html Delphi XE2 compiler perform ...

  7. Net Framework 2.0 MSI returned error code 1603解决方法

    出现这种情况的原因,主要是用ghost做的系统,有很多系统中把ie给绑架了.下面的截图就是ghost做的系统中注册表的显示,通过上面的方法就可以解决这种Microsoft .NET Framework ...

  8. 关于toString方法的重写工具ToStringBuilder

    原文:https://blog.csdn.net/zhaowen25/article/details/39521899# apache的commons-lang3的工具包里有一个ToStringBui ...

  9. C#编程(七十二)----------DynamicObject和ExpandoObject

    DynamicObject和ExpandoObject 1.ExpandoObject表示一个对象,该对象包含可在运行时动态添加和移除的成员.这个类和动态类型有关,这个类能做些什么? 案例: //Ex ...

  10. WebLogic清理缓存

    如果发布到weblogic的工程,登录发现还是原来的代码错误,可尝试清理weblogic缓存: 1.在weblogic控制台中停止应用,删除部署的工程 2.登录weblogic服务器,删除以下目录中的 ...