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. LPC18xx/43xx OTP Controller driver

    LPC18xx/43xx OTP Controller driver /* * @brief LPC18xx/43xx OTP Controller driver * * @note * Copyri ...

  2. STM32F1-workarea : how to drive a WS2812 RGB LED using PWM and DMA

    how to drive a WS2812 RGB LED using PWM and DMA #include <stm32f10x.h> void Delay(__IO uint32_ ...

  3. [置顶] Redis String类型数据常用的16条命令总结

    Redis String类型数据常用的16条命令总结 描述:String 类型是最简单的类型,一个Key对应一个Value,String类型是二进制安全的.Redis的String可以包含任何数据,比 ...

  4. JavaScript学习总结(十五)——Function类

    在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...

  5. 任务失败,因为未找到“AxImpexe”,或未安装正确的 Microsoft Windows SDK

    jenkins自动构建.net时发生错误,查看Console Output看到如下错误: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft ...

  6. spring Quartz基于配置文件和注解的实现

    这里仅仅是做简单的记录怎样实现. 一.基于配置文件的实现 ①编写须要调度的类 package com.study; import org.springframework.scheduling.anno ...

  7. 基于设备树的controller学习(1)

    作者 彭东林pengdonglin137@163.com 平台 TQ2440Linux-4.10.17 概述 在设备树中我们经常见到诸如"#clock-cells"."# ...

  8. [erlang] mnesia

    原文地址: http://www.cnblogs.com/bluefrog/archive/2012/05/16/2504625.html 本来是项目合作的,可是你却一而再再而三的使用这招,我处理愤怒 ...

  9. ASIHttpRequest release 包无法发出请求

    ASIHttpRequest 在 Release 模式下,Optimize 后会导致发不出请求. 解决方案: 去掉这两个文件的 Optimization:ASIFormDataRequest.m AS ...

  10. 【jvm】linux 调用 jmap 报错Permission denied

    linux 调用 jmap  报错Permission denied 解决方案: 分别对java安装目录,java的bin目录以及jmap命令设置权限 chmod jdk1..0_79 chmod b ...