[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 表达式 ...
随机推荐
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- hdu 2197 推公式
题意:由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,100100不是本原串,因为他是由两 ...
- python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐
## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...
- Java外部类可以访问内部类private变量
在讲Singleton时我举例时用过这样一段代码: public class SingletonDemo { private static class SingletonHolder{ private ...
- 几个常用的Eclipse插件
用Eclipse Neon做ROS开发需要几个常用的插件,可以大大加速开发的进度. 1.常用插件 a.CMake Editer 地址:http://cmakeed.sourceforge.net/ec ...
- ELNEC Programmer
BeeHive204 Very fast universal 4x 48-pindrive concurrent multiprogramming system with ISP capability ...
- maven里如何根据不同的environment打包
一个项目里总会有很多配置文件.而且一般都会有多套环境.开发的.测试的.正式的.而在这些不同的环境这些配置的值都会不一样.比如mail的配置.服务的url配置这些都是很常见的.所以在打包的时候就要根据e ...
- HDU 2222 Keywords Search 【AC自动机模板】
询问有多少个模式串出现在了文本串里面.将模式串插入Trie树中,然后跑一边AC自动机统计一下就哦了. 献上一份模板. #include <cstdio> #include <cstr ...
- spring boot JedisCluster连接redis集群配置
配置文件 配置类 构造的时候, 可以看一下, 只有Set<HostAndPort> 参数是必须的 做了一层封装, 更方便使用 结果
- 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境
参考: http://blog.csdn.net/linyt/article/details/42504975 环境介绍: Win7 64 + Vmware 11 + ubuntu14.04 32 u ...