泛函编程(5)-数据结构(Functional Data Structures)

编程即是编制对数据进行运算的过程。特殊的运算必须用特定的数据结构来支持有效运算。如果没有数据结构的支持,我们就只能为每条数据申明一个内存地址了,然后使用这些地址来操作这些数据,也就是我们熟悉的申明变量再对变量进行读写这个过程了。试想想如果没有数据结构,那我们要申明多少个变量呢。所以说,数据结构是任何编程不可缺少的元素。

泛函编程使用泛函数据结构(Functional Data Structure)来支持泛函程序。泛函数据结构的特点是”不可变特性“(Immutability), 是泛函编程中函数组合(composition)的必需。所以,与其它编程范畴不同,泛函编程的泛函数据结构必须具体一套特定的数据运算方式。

泛函数据结构及运算方法具备以下特征:

1、不可变特性(Immutable)

2、运算在数据结构内进行。尽量避免使用中间变量

3、运算返回新的数据结构作为结果

我们先看看熟悉的OOP数据运算风格:

1 scala> var arr = Array(1,2,3)
2 arr: Array[Int] = Array(1, 2, 3)
1 scala> var sum = arr(0)+arr(1)+arr(2)
2 sum: Int = 6

以上运算是需要中间变量的。而且是在结构外进行的:先把数据从地址读出再相加。

1 scala> arr(0) = sum
2
3 scala> arr
4 res9: Array[Int] = Array(6, 2, 3)

直接赋值后arr内容变了。在这里arr是“可变的”(Mutable)数据结构。肯定的是如果下面需要再次使用arr时,我们是无法保证它内容一致性的。

再看看泛函风格:

1 scala> val arr = Array(1,2,3)
2 arr: Array[Int] = Array(1, 2, 3)
3 scala> val sum = arr.sum
4 sum: Int = 6

泛函运算直接在数据结构内进行,不需要中间变量。

1 scala> val arr1 = arr map { x => if(x == 1) sum else x }
2 arr1: Array[Int] = Array(6, 2, 3)
3
4 scala> arr
5 res10: Array[Int] = Array(1, 2, 3)

arr1是赋值后新的数据结构。arr没有变化。这样我们可以放心使用arr来进行函数组合了。

可能这里会出现一些误解:arr1先复制了arr内的数据后再修改内容,所以arr没有变。这样理解有对也有不对:从效果来说arr1是复制了arr。但从具体做法上系统只是把arr(0)下面节点的指针指向了arr1(0),并没有进行实质的数据复制。

时间: 2024-12-20 18:22:13

泛函编程(5)-数据结构(Functional Data Structures)的相关文章

JAVA数据结构multiply-linked data structures程序代写(服务编号:java00088)

The program The purpose of this assignment is to provide some exercise in using multiply-linked data structures. Your program will be a number grid. It will provide a grid with ten rows and six columns. A cell in the grid can hold and display either

泛函编程(8)-数据结构-Tree

上节介绍了泛函数据结构List及相关的泛函编程函数设计使用,还附带了少许多态类型(Polymorphic Type)及变形(Type Variance)的介绍.有关Polymorphism的详细介绍会放在typeclass讨论中.为了更多了解泛函数据结构(Functional Data Structure),想在这个章节把另一个我们熟悉的数据结构-Tree做些简单介绍. Tree的状态不是枝(Branch)就是叶(Leaf),这个很容易理解.那么就按照上节设计List那样设计Tree类型: 1

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

泛函编程(6)-数据结构-List基础

List是一种最普通的泛函数据结构,比较直观,有良好的示范基础.List就像一个管子,里面可以装载一长条任何类型的东西.如需要对管子里的东西进行处理,则必须在管子内按直线顺序一个一个的来,这符合泛函编程的风格.与其它的泛函数据结构设计思路一样,设计List时先考虑List的两种状态:空或不为空两种类型.这两种类型可以用case class 来表现: 1 trait List[+A] {} 2 case class Cons[+A](head: A, tail: List[A]) extends

无锁数据结构(Lock-Free Data Structures)

原文:无锁数据结构(Lock-Free Data Structures) 一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Server里的共享数据结构,例如缓存池里的页(通过闩锁(Latches)),锁管理器哈希表里的锁(通过自旋锁(Spinlock)).接下里你会看到越来越多的全新同步原语(synchronization primitives),即所谓的

[CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构

10.2 How would you design the data structures for a very large social network like Facebook or Linkedln? Describe how you would design an algorithm to show the connection, or path, between two people (e.g., Me -> Bob -> Susan -> Jason -> You).

泛函编程(7)-数据结构-List-折叠算法

折叠算法是List的典型算法.通过折叠算法可以实现众多函数组合(function composition).所以折叠算法也是泛函编程里的基本组件(function combinator).了解折叠算法的原理对了解泛函组合有着至关紧要的帮助.折叠算法又可分右折叠和左折叠.我们先从右折叠(foldRight)开始: 从以上两图示可以得出对List(a,b,c)的右折叠算法:op(a,op(b,op(c,z))) 可以看出括号是从右开始的.计算方式如图二:op(a,sub), sub是重复子树,可以肯

C++数据结构Data Structures动态数组Dynamic Tetris Arrays程序代写(待解决)

CSCI-1200 Data Structures | Spring 2015Homework 3 | Dynamic Tetris ArraysIn this assignment you will use dynamically-allocated arrays to keep track of blocks on the 2D grid of theclassic Tetris computer game. Follow these links to read about the hist

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l