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. 【转载】CMarkup函数说明

    1.初始化Load    导入一个XML文件到CMarkup的对象中,并对它进行解析.类似C#的Load.SetDoc  从字符串中导入XML数据,并对它解析.类似C#的LoadXml. 2.输出Sa ...

  2. UVALive 5058 Counting BST 数学

    B - Counting BST Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  3. 最小生成树-普利姆算法lazy实现

    算法描述 lazy普利姆算法的步骤: 1.从源点s出发,遍历它的邻接表s.Adj,将所有邻接的边(crossing edges)加入优先队列Q: 2.从Q出队最轻边,将此边加入MST. 3.考察此边的 ...

  4. fragment和fragmentactivity解析

    一.为什么要使用Fragment  1.当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个 ...

  5. [Asp.net web api]缓存

    摘要 为了提高接口的性能,我们常做的优化就包括缓存,对经常访问但变化不大的数据进行缓存.或者使用http的缓存,减少请求的次数. web api缓存 在提供的api,我们也可以实现缓存,来减少访问的次 ...

  6. VisualSVN Server 修改用户密码

    VisualSVN Server是非常方便好用的SVN服务器端软件,但有个问题,你在服务器端创建了用户名密码后,用户无法自己修改密码,据说VisualSVN的客户端可以修改用户密码,但客户端是收费软件 ...

  7. 【springboot+easypoi】一行代码搞定excel导入导出

    原文:https://www.jianshu.com/p/5d67fb720ece 开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事 ...

  8. ExtJS创建选项卡

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 算法:哈希表格(Hash Table)

    背景 Java 和 .Net 平台都有一个所有引用类型都会间接或直接继承的类型:Object,这个类型提供最基本的相等性比较算法和哈希算法,很多书上都给出了在重写这两个算法的时候的主意事项,其中大多数 ...

  10. Memcached 集群架构方面的问题

    *  集群架构方面的问题 o memcached是怎么工作的? o memcached最大的优势是什么? o memcached和MySQL的query cache相比,有什么优缺点? o memca ...