[Functional Programming] Build a Linear congruential generator
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的更多相关文章
- LCG(linear congruential generator): 一种简单的随机数生成算法
目录 LCG算法 python 实现 LCG算法 LCG(linear congruential generator)线性同余算法,是一个古老的产生随机数的算法.由以下参数组成: 参数 m a c X ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- 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 ...
- 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 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- 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 without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
随机推荐
- 在Windows上安装FFmpeg程序
原文地址:http://helloway.blog.51cto.com/7666282/1642247 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它提供了录 ...
- 【BZOJ 3640】JC的小苹果 (高斯消元,概率DP)
JC的小苹果 Submit: 432 Solved: 159 Description 让我们继续JC和DZY的故事. “你是我的小丫小苹果,怎么爱你都不嫌多!” “点亮我生命的火,火火火火火!” 话 ...
- zoj 3229 上下界网络最大可行流带输出方案
收获: 1. 上下界网络流求最大流步骤: 1) 建出无环无汇的网络,并看是否存在可行流 2) 如果存在,那么以原来的源汇跑一次最大流 3) 流量下界加上当前网络每条边的流量就是最大可行流了. 2. 输 ...
- python 爬虫 处理超级课程表传输的数据
借鉴的别人的思路 http://www.oschina.net/code/snippet_2463131_53711 抓取超级课程表传输的数据 他的传输数据居然是明文的-- 现在已经把自己的课表都抓出 ...
- Codeforces Round #357 (Div. 2) B. Economy Game 水题
B. Economy Game 题目连接: http://www.codeforces.com/contest/681/problem/B Description Kolya is developin ...
- Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
B. Bear and Finding Criminals 题目连接: http://www.codeforces.com/contest/680/problem/B Description Ther ...
- POJ 2356 Find a multiple 抽屉原理
从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...
- 【windows socket+HTTPserverclient】
Windows Socket+HTTPserverclient Winsock是 Windows下套接字标准. 1.HTTP协议: HTTP ...
- OllyDbg 使用笔记 (十二)
OllyDbg 使用笔记 (十二) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 演示样例程序下载地址:http://pan.baidu.com/s/1eQiV6aI 安装好程序 ...
- C#的new操作符到底做了什么
使用new操作符来创建对象,其背后到底发生了什么? 有一个父类Animal,Dog派生于Animal. class Program { static void Main(string[] args) ...