What we are going to do in this post, is to build a random number generator. As you might know that Javascript provides Math.random(), but the problem for Math.random() is you cannot control it, what we actully want is a number generator which can provide the same sequence of random numbers. Imaging we have a game, everytime we start the game it will genarte

[5,7,9,8,1,3....]

Second times, it runs the game, it produce the same output: [5,7,9,8,1,3....]. This kind of number generator is called: Linear congruential generator

We are going to use 'crocks/State' to coding it:

const log = require('./lib/log');

const State = require('crocks/State');
const B = require('crocks/combinators/composeB');
const K = require('crocks/combinators/constant');
const prop = require('crocks/Maybe/prop');
const option = require('crocks/pointfree/option');
const {modify, get} = State;
const assign = require('crocks/helpers/assign'); // pluckSeed :: Integer -> Object -> Integer
const pluckSeed =
def => B(option(def), prop('seed')) // rando : Integer -> State Object Float
const rando = x => {
const seed = (1103515245 * x + 12345) & 0x7fffffff
const value = (seed >>> 16) / 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(pluckSeed(defSeed)).chain(rando); // limitIndx :: Integer -> Float -> Integer
const limitIndx = len => x => (x * len) | 0; const seed = 76;
log(
State.of(seed)
.chain(pullRandom) // 'Pair( 0.13864558854945525, { seed: 297746555 } )'
.map(limitIndx(52)) // 'Pair( 7, { seed: 297746555 } )'
.runWith({seed: 10})
);

Let's explain some operator a little bit:

const B = require('crocks/combinators/composeB');

'composeB', the same as 'compose' read from right to left, the only difference is it only accepts two functions! no more no less.

// pluckSeed :: Integer -> Object -> Integer
const pluckSeed =
def => B(option(def), prop('seed'))

'pluckSeed', we use 'prop' from 'Maybe', to get value from Maybe, we need to use 'option', if Maybe returns Nothing, then we use the default value from 'option(def)'.

const State = require('crocks/State');
const {modify, get} = State; // rando : Integer -> State Object Float
const rando = x => {
const seed = (1103515245 * x + 12345) & 0x7fffffff
const value = (seed >>> 16) / 0x7fff; return modify(assign({seed})) // update the seed Pair(_, seed)
.map(K(value)); // update the value Pair(value, seed)
}

'modify' it a constructor' method for 'State' moand, the State monad is constructed by Pair moand:

State(Pair(a, s))

On the left side, 'a' is the new state which come from 's' after mapping to some function. 's' is the origianl state.

When you call 'modify', it is actually modifying the right side 's':

modify(assign({seed})) // update the seed Pair(_, seed)

When you call .map(fn), it is change the value on the left side of Pair:

    return modify(assign({seed})) // update the seed Pair(_, seed)
.map(constant(value)); // update the value Pair(value, seed)

[Functional Programming] Build a Linear congruential generator的更多相关文章

  1. LCG(linear congruential generator): 一种简单的随机数生成算法

    目录 LCG算法 python 实现 LCG算法 LCG(linear congruential generator)线性同余算法,是一个古老的产生随机数的算法.由以下参数组成: 参数 m a c X ...

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

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

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

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

  5. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

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

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

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

  8. Functional programming

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

  9. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

随机推荐

  1. 从数组中查看某值是否存在,Arrays.binarySearch

    Arrays.binarySearch为二分法查询,注意:需要排序 使用示例 Arrays.binarySearch(selectedRows, i) >= 0

  2. hdoj 5122 K.Bro Sorting 贪心

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...

  3. android 从零单排 第一期 按键显示helloworld

    啦啦啦- 我是qscqesze 今天开始android的从零单排啦啦啦- 首先从最简单的开始 要求: 程序运行后,单击屏幕上的按键后可以显示一句话,如“Hello World!” 这是一个最基础最基础 ...

  4. Linux下Qt安装

    1.下载qt-everywhere-opensource-src4.7.2.tar.gz(http://download.qt.io/archive/qt/4.7),并解压在/opt目录下,文件名为q ...

  5. ROS知识(12)----cv_bridge依赖opencv版本的问题

    cv_bridge默认依赖的oencv版本是2.4.8,如果安装了新的opencv版本,比如2.4.11,那么在编译cv_bridge时候会提示无法找到opencv 2.4.8.so的库. 为解决这个 ...

  6. 装了wamp之后,80端口被占用解决办法

    1.如果装了IIS,那么把IIS停掉. 2.如果装了sqlserver,那么在cmd里面执行命令:services.msc,进入服务里面,把SQL Server Reporting Services ...

  7. UploadFileUtil

    package cn.tz.util.file; import java.io.File; import java.io.FileOutputStream; import java.io.InputS ...

  8. 推荐一个简洁优雅的博客系统,farbox

    这是我用farbox搞的一个博客:http://www.jsnull.com/ 特点: 1.无数据库,数据存在dropbox里,需要自己注册一个dropbox帐号 2.静态文本文件即是文章,可以在任何 ...

  9. [转贴] start-stop-daemon命令

    转贴自 http://www.lampblog.net/ubuntu/start-stop-daemon%E5%91%BD%E4%BB%A4/ 1.功能作用 启动和停止系统守护程序 2.位置 /sbi ...

  10. perf 工具介绍3

    http://blog.chinaunix.net/uid-10540984-id-3854969.html http://blog.csdn.net/zhangskd/article/details ...