[Functional Programming] Daggy
const daggy = require('daggy');
const {tagged, taggedSum} = daggy;
const Coord = daggy.tagged('Coord', ['x', 'y', 'z'])
const Line = daggy.tagged('Line', ['from', 'to'])
Coord.prototype.translate = function(x, y, z) {
return Coord(
this.x + x,
this.y + y,
this.z + z
)
}
const origin = Coord(,,)
console.log(origin); // { x: 1, y: 2, z: 3 }
const myLine = Line(
origin,
origin.translate(,,)
);
console.log(myLine) // { from: { x: 1, y: 2, z: 3 }, to: { x: 3, y: 6, z: 9 } }
const Shape = taggedSum('Shape', {
Square: ['topleft', 'bottomright'],
Circle: ['centre', 'radius']
})
Shape.prototype.translate = function(x, y, z) {
return this.cata({
Square: (topleft, bottomright) => Shape.Square(
topleft.translate(x, y, z),
bottomright.translate(x, y, z)
),
Circle: (centre, radius) => Shape.Circle(
centre.translate(x, y, z),
radius
)
})
}
console.log(Shape.Square(Coord(, , ), Coord(, , ))
.translate(, , ))
// Square(Coord(5, 5, 3), Coord(6, 6, 3))
console.log(Shape.Circle(Coord(, , ), )
.translate(, , ))
// Circle(Coord(7, 7, 7), 8)
All the tagged function really does is give us a function to fill the given named properties on an object.
taggedSum is a combination of multi Typed class.
[Functional Programming] Daggy的更多相关文章
- 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 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 ...
- 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
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...
随机推荐
- Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors
Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors The goal of the pro ...
- 【Go命令教程】4. go get
hc@ubt:~$ go get github.com/hyper-carrot/go_lib/logging 命令 go get 可以根据要求和实际情况从互联网上下载或更新指定的代码包及其依赖包,并 ...
- Entity framework 增加默认执行时间
public partial class ProductionSupportEntities : DbContext { public ProductionSupportEntities() : ba ...
- 用delphi检查网络连接状态3种方式
用delphi检查网络连接状态3种方式 用delphi检查网络连接状态 检测计算机是否联网比较简单的做法可以通过一个 Win32 Internet(WinInet) 函数 InternetCheckC ...
- 委托、Lambda表达式、事件系列07,使用EventHandler委托
谈到事件注册,EventHandler是最常用的. EventHandler是一个委托,接收2个形参.sender是指事件的发起者,e代表事件参数. □ 使用EventHandler实现猜拳游戏 使用 ...
- SpringBoot集成JWT实现token验证
原文:https://www.jianshu.com/p/e88d3f8151db JWT官网: https://jwt.io/ JWT(Java版)的github地址:https://github. ...
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
- 女子监狱第一季/全集Orange Is the New Black迅雷下载
本季第一季 Orange Is the New Black 1 (2013) 看点:该剧描述主人公Piper Chapman(Taylor Schilling)在大学里结识了毒贩Alex(Laura ...
- 检测ASP.NET是否是调试模式
在web.config里,可以设置debug为true或者false <system.web> <compilation debug="false" target ...
- 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang
点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...