[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 ...
随机推荐
- LPC43xx SGPIO Slice 示意图
SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...
- linux 内核crash 命令
https://www.dedoimedo.com/computers/crash-book.html#download
- XML 高速入门总结
XML已经学习完了一段时间了.一直感觉知识比較琐碎,没有去好好总结.事实上越琐碎的知识也越须要我们去好好 理一下.将知识串起来.争取变得不再琐碎.以下是我学完xml画的一张图. 以下对XML进行一下简 ...
- 在Delphi中DBGrid有一个MouseMove事件,当鼠标移动时怎么知道光标在哪个单元格上面
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var coords:TGr ...
- 使用cat读取和echo写内核文件节点的一些问题
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...
- 程序员必须知道的HTML常用代码有哪些?
HTML即超文本标记语言,是目前应用最为广泛的语言之一,是组成一个网页的主要语言.在现今这个HTML5华丽丽地占领了整个互联网的时候,如果想要通过网页抓住浏览者的眼球光靠因循守旧是不行的,程序猿们需要 ...
- WordPress主题开发:WP_Query基本用法
为什么要学WP_Query? wordpress默认会根据网址调用数据,不能满足我们所有建站要求,而WP_Query可以用于查询任何你想要的内容,相当于自定义数据调用. 便于记忆,在讲用法之前我们回顾 ...
- log4j1 修改FileAppender解决当天的文件没有日期后缀
直接上代码: /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license ...
- XVFB实现selenium在linux上无界面运行安装篇
selenium在linux上无界面运行,其实是非常简单的.具体的方法有使用HtmlUnitDriver或者PhantomJSDriver,有时间我会写写关于这两个东东的文章,其实基本和ChromeD ...
- 将hta包装为exe发布
hta在打开的时候,有时候会被杀毒软件拦截而不给执行,更重要的一点是通常都可以右击查看源代码,里面如果涉及到域名或者其它的一些细节就很容易被其它人了解. 网络上有一些hta转exe的,类似的软件基本上 ...