Test

October 30, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

Welcome to this blog. This blog is created with Docusaurus 2 alpha.

Hello!

Lorem Ipsum

{/ truncate /}

hahahahahahaha
hahahaha

lailailailailai

Lorem Ipsum

lailailailailai

content

Tip

try

Excel有什么,没有什么,没有的时候是如何补足/用什么替代方法的?

spreadsheets lack the most fundamental mechanism that we use to control complexity: the ability to define re-usable abstractions Excel是怎么补足这一点的?怎么定义re-usable abstraction的? You cannot give it a name; result represent body 2)感觉不到重新运行 3)visible results, even intermediate 4) 变量可以有很多;function的体积又浓缩到1个cell,很小 5) 这种D...是由A...和B...来做的

写snippet对比,case by case。不追求所有概念都有完全对应。

https://www.quora.com/What-are-the-characteristics-of-a-functional-programming-language immutable state 从这个角度excel是的

hier order function实现不了

simulate

Function

Let's look at a very basic pattern in Excel:

Excel sample: 一个B1=A1*A1, B2= A2*A2, B3= A3*A3, etc. square function

To achieve the same thing, we use List.map function in OCaml as follows:

# List.map;;
- : ('a -> 'b) -> 'a list -> 'b list = <fun>
# let square x = x * x;;
val square : int -> int = <fun>
# List.map square [1; 2; 3];;
- : int list = [1; 4; 9]

We could notice that, this basic pattern of formulas is not less good than the OCaml one. Alough the caluclation body is repeated several times, it is very hands-on to achieve it by copy-paste.

Excel does not provide a way of possibility to give a name to that calculation body.

The calculation body is repeated in Excel, as many times as necessay. It is not tiridous to do so because of copy-paste Visually, it is not annoying, because it is embedded in a cell. As a result, what is shown is only the input and output, which is all we want to see.

Regarding the last point, it is what people critic most.

List.map (fun x -> Float.pi . (float_of_int x) . (float_of_int x)) [1; 2; 3];;

circlearea

Higher-order functions

function as argument

C, Pascal也没有higher-order functions,也一样用

f(f(x))

# let ho f x = f(x) + f(x+1);;
val twice : ('a -> 'a) -> 'a -> 'a = <fun>
# twice double 3;;
- : int = 15
# twice square 5;;
- : int = 61
int double (x) { return x * 2 }
int twice_double (x) {
return double (double (x + 1) + 1) + 1
}

不好的话,讲出来、show出来哪里不好也是贡献

Excel sample: 一个B1=A1*2, C1= A1*2+(A1+1)*2, B4=A2*A2, C4=A2*A2+(A2+1)*(A2+1) etc.

In Excel, 当然你可以把长的公式分成几步做,但是本质上,像C一样,还是得重写function,而不能用higher-order这种方便的定义方法。而且C里body的写法要更简单,因为用到了double,而Excel中只能用一些现有的值。

reuse function body V.S. reuse intermediate data/results

1种办法是完全expand formula,另一种办法是reuse intermediate data/results

我怎么感觉是用data呢?

map, map * 2 to a list, map 平方 to a list

List.sort is a function whose argument has function.

In functional programming, a function is defined as a "first order citizen". This means it includes all the properties generally available to any other element, such as the possibility of being affected to a name, returned as a result or passed as a parameter.

A higher-order function is a function that accepts other functions as parameters and/or use a function as the return value. Let's try it out! Implement the greeting function to return "Hello, [name]" if the name is defined. Otherwise, return "who are you?".

# let double x = 2 * x
and square x = x * x;;
val double : int -> int = <fun>
val square : int -> int = <fun>
# List.map;;
- : ('a -> 'b) -> 'a list -> 'b list = <fun>
# List.map double [1; 2; 3];;
- : int list = [2; 4; 6]
# List.map square [1; 2; 3];;
- : int list = [1; 4; 9]

也许要说明一下Excel做list function很natural,做map很natural

# let greater x y =
if (x > y) then 1
else if (x < y) then -1
else 0
and lesser x y =
if (x > y) then -1
else if (x < y) then 1
else 0;;
val greater : 'a -> 'a -> int = <fun>
val lesser : 'a -> 'a -> int = <fun>
# List.sort;;
- : ('a -> 'a -> int) -> 'a list -> 'a list = <fun>
# List.sort greater [1; 2; 3; 4; 5];;
- : int list = [1; 2; 3; 4; 5]
# List.sort lesser [1; 2; 3; 4; 5];;
- : int list = [5; 4; 3; 2; 1]

上面这个在Excel中用Sort就可以实现了。 不用c语言,用python做比较

others

Excel是怎么样适合array的操作了?

比如实现Array.map3, Array.map4甚至更多。

OCaml Array.map和Array.map2 (iterate simultaneously),如果再想iterate simultaneously就得用loop over index了。Excel的copy-paste,几乎和map一样简单(copy-paste就是一种手动map),都比loop over index要简单。

# let map3 f a0 a1 a2 =
let l = Array.length a0 in
let e = f (Array.get a0 0) (Array.get a1 0) (Array.get a2 0) in
let result = Array.make l e in
for i = 1 to l - 1 do
let e = f (Array.get a0 i) (Array.get a1 i) (Array.get a2 i) in
Array.set result i e;
done;
result;;
val map3 :
('a -> 'b -> 'c -> 'd) -> 'a array -> 'b array -> 'c array -> 'd array =
<fun>
# let a0 = [|0; 0; 0; 0; 0|]
and a1 = [|1; 2; 3; 4; 5|]
and a2 = [|2; 3; 4; 5; 6|]
and f e0 e1 e2 = e0 + e1 + e2;;
val a0 : int array = [|0; 0; 0; 0; 0|]
val a1 : int array = [|1; 2; 3; 4; 5|]
val a2 : int array = [|2; 3; 4; 5; 6|]
val f : int -> int -> int -> int = <fun>
# map3 f a0 a1 a2;;
- : int array = [|3; 5; 7; 9; 11|]

Python有map3, map4。。。但比Excel缺点是不够visiable,不够平铺(你想挑什么就挑什么)flat, expose to you:

a0 = [0, 0, 0, 0, 0]
a1 = [1, 2, 3, 4, 5]
a2 = [2, 3, 4, 5, 6]
def f(e0, e1, e2):
return e0 + e1 + e2
print(map(f, a0, a1, a2))

Excel sample: B1=A1+1, C1=A1*2, D1=B1+C1

另外一种非常常见的pattern Excel sample: D2=D1*A2/B2, D3=D2*A3/B3 ...

https://stackoverflow.com/questions/57532610/quick-way-to-implement-simultaneous-iterations-folding-in-python

肯定不是map,而是iter或者fold_left

这种情况(就某些array操作)就还是excel方便(又有fold,又有list of intermediate results)

Good: 要求是得重复f这个body

a0 = [6, 6, 6, 6, 6]
a1 = [1, 1, 1, 1, 1]
init = 100
def f(x, e0, e1):
return x * e0 / e1
def myIterFold(f, init, a0, a1):
e_0 = f(init, a0[0], a1[0])
result = [e_0]
for i in range(1, len(a0)):
e_i = f(result[i-1], a0[i], a1[i])
result.append(e_i)
return result
print(myIterFold(f, init, a0, a1)) # [600, 3600, 21600, 129600, 777600]

Polymorphism

Polymorphism, or parametric polymorphism, means that one function can operate on several types. For exemple, the bigger function below can operate over 2 integers or over 2 strings.

# let bigger x y = x > y;;
val bigger : 'a -> 'a -> bool = <fun>
# bigger 1 2;;
- : bool = false
# bigger "b" "a";;
- : bool = true

Because of the weak-type system of spreadsheets, applying one code to different types of data is never a problem.

anonym function, reduce functions are data, functions as input, functions as result/ return a function, store function as data

Tie's blog

https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/excel-1.pdf

How come is Excel a functional programming language?

define a function

https://en.wikipedia.org/wiki/Currying currying不太相关

https://ocaml.org/learn/taste.html function composition: let (>>) f g x = g ( f x) 也不太相关,因为没有function的定义,所以把2个function名字弄一起也莫名其妙。如果说excel中的composition,一种是在1个formula中做很多事,不要intermediate results了,一种是把intermediate results列出来。更像是data的composition.

closure, function closure 没什么关系

Why do we love spreadsheets?

October 10, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)

Why are functions and formulas beloved?

October 9, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)

How will dynamic arrays change spreadsheet modelling?

October 5, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)

Excel's skill maps

October 3, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)

A little bit of history of spreadsheets

October 2, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)

Other topics

October 1, 2019
Tie Cheng

Tie Cheng

CEO @ Matrix Lead

(to be released)