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. AES Test vectors

    Table of content List of test vectors for AES/ECB encryption mode AES ECB 128-bit encryption mode AE ...

  2. ubuntu下安装ftp服务器

    参考文献: 5.4 FTP 服务器 vsftpd - FTP 服务器安装 vsftpd 是可在 Ubuntu 中使用的 FTP 守护程序之一.它在安装.设置和维护方面十分方便.要安装 vsftpd 您 ...

  3. 汽车之家店铺数据抓取 DotnetSpider实战

    一.背景 春节也不能闲着,一直想学一下爬虫怎么玩,网上搜了一大堆,大多都是Python的,大家也比较活跃,文章也比较多,找了一圈,发现园子里面有个大神开发了一个DotNetSpider的开源库,很值得 ...

  4. 线程池大小设置,CPU的核心数、线程数的关系和区别,同步与堵塞完全是两码事

    线程池应该设置多少线程合适,怎么样估算出来.最近接触到一些相关资料,现作如下总结. 最开始接触线程池的时候,没有想到就仅仅是设置一个线程池的大小居然还有这么多的学问,汗颜啊. 首先,需要考虑到线程池所 ...

  5. rTorrent + ruTorrent 安装和配置

    原文地址:http://wangyan.org/blog/rtorrent-and-rutorrent-tutorial.html rTorrent 是一款非常简洁优秀的BT客户端,它完全基于文本并可 ...

  6. Linkedin工程师是如何优化他们的Java代码的

    http://greenrobot.me/devpost/java-faster-less-jvm-garbage/ Linkedin工程师是如何优化他们的Java代码的 最近在刷各大公司的技术博客的 ...

  7. Java实现用汉明距离进行图片相似度检测的

    Google.Baidu 等搜索引擎相继推出了以图搜图的功能,测试了下效果还不错~ 那这种技术的原理是什么呢?计算机怎么知道两张图片相似呢? 根据Neal Krawetz博士的解释,原理非常简单易懂. ...

  8. java数字转换成文字方法

    public class IntToSmallChineseNumber { public static String ToCH(int intInput) { String si = String. ...

  9. IT狂人第一至四季/全集The IT Crowd迅雷下载

    本季第一至四季 The IT Crowd (2006)看点:<IT狂人>史上最囧,最雷,最脑残,最出乎意料,最不按常理出牌的IT “精英们”登上银屏了.让超擅长收发邮件.单击和双击鼠标的I ...

  10. 监听Listview的滚动状态,是否滚动到了顶部或底部

    /** * @author:Jack Tony * @description : 监听listview的滑动状态,如果到了顶部就刷新数据 * @date :2015年2月9日 */ private c ...