Mathematica is, typically, be seen as a calculator. But it do has some shine feature.
Take this as an example:
is it ? yes, but what defines a function? Do we miss something? Oh, the domain!
so it can be in , but you know, in real world, there aren’t just number, we have more things other than that. For example, String .
get what? oh, "12"^2 , what’s what?
So why does this happen? Because the pattern Blank[] , aka _ in x_ can accept anything. And mma will hold the expression. (calculate a[b] and mma will return itself.
So how do we know the type? In mma, we have Head, Since mma store the 0th element as it’s type. And we can match it with Blank[]
Typed based overload.
But it’s much powerful than that.
we can double anything!
:= because StringJoin need to calculate the expression at once. So we need to delay it.
But if I want to double any number?
notice the difference between NumericQ and NumberQ , NumberQ[Sqrt[2]] return False.
in some aspect, we can think _?NumericQ as a Protocal or Interface or Trait .(For example, num-traits in rust.
So, if you want to match a/some certain type(s), use _X or _X|_Y, else use _?XQ .
That is to say, we want a method to check type meets some conditions.
in rust, we use impl trait or where or dyn trait
in cpp, use sfinae(I don’t know it.) or concept. (Emit)

TODO:
implement this in mma
Since mma can Hold value by default, it can be seem as oop .
 
Loading...
Steven Lynn
Steven Lynn
喂马、劈柴、周游世界
最新发布
我与 Dify 的半年
2025-3-9
我的2022年终小结
2024-11-9
记录雅思考试经历与一点学习心得
2024-11-9
Hackergame 2024 思路小结
2024-11-9
黑客松、日本、入职:我的2024下半年的总结
2024-11-9
NotionNext:基于Notion和NextJS的开源博客
2024-11-9