-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,5 @@ | ||
# C | ||
# Cplusplus | ||
|
||
## 参考 | ||
- [What a C programmer should know about memory](https://marek.vavrusa.com/memory/) | ||
## TMP(template metaprogramming) | ||
|
||
## 面向对象编程 | ||
重载 | ||
比较通用的做法是利用C的可变参数va_args,也可以利用宏和预处理、函数指针来实现,不过需要编写很多额外的代码来管理这些重载函数的管理。 | ||
C语言实现继承有很多中方式, | ||
```c | ||
struct base_s { | ||
int id; | ||
}; | ||
struct parent_s { | ||
int id; | ||
int grade; | ||
}; | ||
struct child_s { | ||
int id; | ||
int grade; | ||
int math; | ||
}; | ||
child_s child; | ||
parent_s *parent = (child_s*)child; | ||
base_s *base = (base_s*)parent; | ||
``` | ||
可以看到在保持内存布局一致的情况下,就可以直接进行重新对内存块进行解析,而在更高级的语言中就是通过语言内部的机制来完成这种内存布局上的特定格式。高级语言就是固定某种数据结构模式来形成一个更严格的编程语言模型。这种模式在大量优秀软件中底层都是如此处理的,使用C语言作为底层数据形式,在使用中间层来完成与C语言的数据交互,这就是其他语言模型在特定场合有一定市场的根本原因。如TCP协议的实现使用scheme的函数式语言来处理,就是因为TCP协议的本身特性非常适合它。 | ||
|
||
linux内核中使用的C语言的面向对象的设计思考参考 | ||
- [Object-oriented design patterns in the kernel, part 1](https://lwn.net/Articles/444910/) | ||
- [Object-oriented design patterns in the kernel, part 2](https://lwn.net/Articles/446317/) | ||
C++ allows template metaprogramming (TMP), a technique in which so-called templates are used by a compiler to generate temporary source code that is merged with the rest of the source code and then compiled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Programming Paradigm | ||
|
||
## The Functional Programming Paradigm | ||
函数式编程 | ||
|
||
编程语言有一种分类为 | ||
- Imperative命令式, 更关注具体,更关注于行为的细枝末节,强调过程,偏于微观 | ||
- Declarative声明式,更关注抽象,用起来更简洁,更专注于问题的解决步骤,强调结果,偏于宏观 | ||
|
||
### Cplusplus | ||
范式最多的语言,从面向过程、面向对象、泛型编程、函数式编程,本质就是编程范式的转变,也就是越现代化的Modern C++ | ||
|
||
**Monads**,概念来自范畴论,称为单子,函数式编程当中使用Monads表示一个抽象数据类型,其目的在于表示计算过程。 | ||
|
||
>> A monad is just a monoid in the category of endoffunctors | ||
C++中定义为class来,语法上与Functor仿函数有点像,如果只有一个成员函数就是Functor,多个成员函数就是Monad。 | ||
从范畴论术语,称为函子,在范畴论中,输入的数据和输出的数据称为范畴,将一个范畴转换为另一个范畴方式,是一种比Function更高阶的函数。 | ||
|
||
若输入和输出类型相同,即源范畴和目的范畴相同,称为幺元,此时函子称为自函子endofunctors,是end of functors的组合词。幺元够了一个幺半群,就是Monad,类型满足这些约束就是一个Monad。 | ||
```c++ | ||
template<typename A> | ||
struct Functor { | ||
template<typename B> | ||
Functor<B> transform(std::function<B(A)>); | ||
} | ||
``` | ||
```c++ | ||
template<typename A> | ||
struct Monad { | ||
template<typename B> | ||
Monad<B> transform(std::function<B(A)>); | ||
template<typename B> | ||
Monad<B> and_then(std::function<B(A)>); | ||
} | ||
``` | ||
注意函数名称不是随便起的,一般不同语言有共识的,C++中是transform和and_then,Haskell则是map和fmap。 | ||
[std::optional中的monadic operations](https://en.cppreference.com/w/cpp/utility/optional) | ||
|
||
### 参考 | ||
- [Monads in Modern C++, What, Why, and How](https://www.cppmore.com/2023/08/14/monads-in-modern-c-what-why-and-how/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters