Placeholder Image

字幕列表 影片播放

由 AI 自動生成
  • (bright music)

    (明亮的音樂)

  • - [Shaun] So what is partial application?

    - [Shaun]那麼什麼是部分應用?

  • Well, in simple terms,

    嗯,簡單地說。

  • partial application is when we take a function

    部分應用是指當我們把一個函數

  • that has some number of arguments,

    有一定數量的參數。

  • and we fix some of those arguments to a set value.

    而我們把其中一些參數固定為一個設定值。

  • This function with fixed arguments

    這個帶有固定參數的函數

  • can then be called from anywhere else in the code,

    然後可以從代碼中的其他地方調用。

  • and it'll be as if we had called the original function

    就像我們調用了原始函數一樣

  • with all of its arguments.

    和它的所有參數。

  • Another way to think about it as like this:

    另一種思考方式是這樣的。

  • Normally when we call a function,

    通常情況下,當我們調用一個函數時。

  • we have to pass all the arguments into it

    我們必須把所有的參數傳給它

  • at the same exact time in place.

    在同一個地方的確切時間。

  • What partial application allows us to do,

    部分應用允許我們做什麼。

  • is pass arguments to a function,

    是向一個函數傳遞參數。

  • at different places in the code,

    在代碼的不同地方。

  • and then get the result,

    然後得到結果。

  • once the function gets all of the arguments it needs.

    一旦函數得到它所需要的所有參數。

  • Now, partial application is also a very useful way

    現在,部分應用也是一個非常有用的方法

  • to configure more general functions

    以配置更多的一般功能

  • into more specific ones.

    變成更具體的。

  • In general,

    一般來說。

  • if there's a function that we use often in our code,

    如果有一個我們在代碼中經常使用的函數。

  • where one or more of the arguments that we call it with

    其中的一個或多個參數,我們用它來調用

  • is almost always the same,

    幾乎總是一樣的。

  • this is usually a good candidate

    這通常是一個好的候選人

  • for using partial application.

    為使用部分應用。

  • So now that we know what partial application is,

    是以,現在我們知道什麼是部分應用。

  • what exactly does it look like in code?

    它在代碼中到底是什麼樣子的?

  • Well, let's imagine that we have a simple add function,

    好吧,讓我們想象一下,我們有一個簡單的添加函數。

  • that takes three integers as arguments,

    它需要三個整數作為參數。

  • and adds them together.

    並把它們加在一起。

  • That would look something like this:

    這看起來會是這樣的。

  • We're going to say, try function,

    我們要說的是,嘗試功能。

  • using this try function interface

    使用這個嘗試函數接口

  • that we defined earlier in the course.

    我們在課程早些時候定義的。

  • And it's going to take three integers as arguments.

    它將接受三個整數作為參數。

  • So integer, integer, integer,

    是以,整數、整數、整數。

  • and it's going to return an integer as well.

    並且它也將返回一個整數。

  • We'll just call this function add.

    我們就叫這個函數add。

  • And what it's going to do is,

    而它要做的是。

  • it's going to take its three arguments,

    它要把它的三個論據。

  • and add them all together, okay?

    並把它們都加在一起,好嗎?

  • So now that we have this function,

    所以,現在我們有了這個函數。

  • what if we wanted to fix one of these arguments

    如果我們想修正其中的一個論點呢?

  • to a certain number,

    到一定的數量。

  • so that we only had to pass in the other two arguments

    這樣,我們只需要傳入另外兩個參數

  • later on?

    後來呢?

  • Well, what that would look like is this:

    那麼,這將會是這樣的。

  • We would create another function

    我們將創建另一個函數

  • using the function interface,

    使用函數接口。

  • and this function will take a single integer as an argument,

    而這個函數將接受一個單一的整數作為參數。

  • and it'll return a BiFunction

    並且它將返回一個BiFunction

  • that takes two integers as arguments,

    它需要兩個整數作為參數。

  • and returns an integer.

    並返回一個整數。

  • And we have to import both the BiFunction

    而我們必須同時導入BiFunction

  • and function interfaces up at the top.

    和功能界面在頂部。

  • And for the name of this function,

    而對於這個函數的名稱。

  • we're going to call it addPartial.

    我們要把它叫做addPartial。

  • And what this function is going to do,

    以及這個函數要做什麼。

  • is it's going to take a single argument,

    是它將採取一個單一的參數。

  • and it's going to return another function

    並且它將返回另一個函數

  • that takes the other two arguments.

    讀取其他兩個參數。

  • And finally, once it has all of those three arguments,

    最後,一旦它擁有了所有這三個論據。

  • it's going to call add dot apply, with x, y, and z.

    它將調用add dot apply,其中包括x、y和z。

  • So now that we have this addPartial function,

    所以現在我們有了這個addPartial函數。

  • calling it is going to look like this:

    稱它將會是這樣的。

  • We're going to say BiFunction, integer, integer, integer,

    我們要說的是BiFunction, integer, integer, integer。

  • and we'll say something like

    而我們會說像這樣的話

  • add five equals addPartial dot apply five.

    添加五等於addPartial點應用五。

  • So now the first argument x has been fixed to five,

    所以現在第一個參數x已經被固定為5。

  • by calling add partial dot apply five.

    通過調用add partial dot apply five。

  • And what we can do now is we can call this add five function

    而我們現在可以做的是,我們可以調用這個添加5的函數

  • with the other two remaining arguments.

    與其他兩個剩餘的論據。

  • And I'm going to print this out,

    而我要把這個打印出來。

  • so you can see what it looks like.

    所以你可以看到它是什麼樣子的。

  • So we can say add five dot apply,

    所以我們可以說增加五個點的應用。

  • and we can call it with the remaining two arguments,

    我們可以用剩下的兩個參數來調用它。

  • which are going to be y, and z here.

    其中,這裡將是y,和z。

  • And if we run this code,

    而如果我們運行這段代碼。

  • we'll see that it prints out the correct result.

    我們將看到它打印出了正確的結果。

  • So this is five plus six plus seven,

    所以這是五加六加七。

  • except we passed in the five and the six and seven

    除了我們在五號、六號和七號中通過之外

  • in different places in our code.

    在我們代碼中的不同地方。

  • And that's what partial application is.

    而這就是部分應用的內容。

  • So in this example that we just went through,

    是以,在我們剛剛經歷的這個例子中。

  • we fixed the first argument x,

    我們固定了第一個參數x。

  • and left the other two for later.

    並把另外兩個留到以後。

  • But in reality,

    但在現實中。

  • we can divide up our arguments however we want.

    我們可以隨心所欲地劃分我們的論點。

  • For example,

    比如說。

  • we could pass in the first two arguments,

    我們可以傳入前兩個參數。

  • and leave the last one for later.

    並將最後一項留待以後。

  • And that would look something like this:

    而這看起來會是這樣的。

  • We'd say function,

    我們會說功能。

  • and it would take two arguments and return a function,

    並且它將接受兩個參數並返回一個函數。

  • that takes an integer as an argument,

    它需要一個整數作為參數。

  • and returns an integer.

    並返回一個整數。

  • And what this would look like now,

    而現在這將是什麼樣子。

  • is it would take the first two arguments, x and y,

    是它會接受前兩個參數,x和y。

  • and return a function that takes the last argument.

    並返回一個接受最後一個參數的函數。

  • And then it would call add dot apply,

    然後它將調用添加點應用。

  • with those three arguments we'd accumulated.

    用我們積累的這三個論據。

  • Oh, and this would actually be a BiFunction,

    哦,這實際上將是一個BiFunction。

  • instead of just a regular function.

    而不僅僅是一個普通的函數。

  • So now calling this is going to look a little bit different.

    是以,現在調用這個會看起來有點不同。

  • We're going to say, function,

    我們要說的是,功能。

  • since that's what this returns here,

    因為這就是這裡的回報。

  • function, integer, integer,

    函數、整數、整數。

  • and we'll call this add five and six,

    我們把這稱為添加五和六。

  • equals addPartial dot apply,

    等於addPartial dot apply。

  • with y and z equal to five and six, okay?

    用y和z等於5和6,好嗎?

  • So what we're doing here is,

    所以我們在這裡所做的是。

  • we're setting x to five and y to six,

    我們將X設為5,Y設為6。

  • so that we only have to pass in z later on, okay?

    這樣我們以後就只需要在Z中傳遞,好嗎?

  • So what we can do now,

    所以我們現在能做的是。

  • again, I'm going to print this out to the console,

    再次,我將把這個打印到控制檯。

  • is we can call add five and six dot apply,

    是我們可以調用添加五和六點應用。

  • with the final argument z,

    與最後的參數z。

  • which we're going to say seven.

    其中我們要說的是七個。

  • And if we run this program again,

    而如果我們再次運行這個程序。

  • we see that it prints out the same answer,

    我們看到,它打印出的答案是一樣的。

  • except we're just passing our arguments

    除了我們只是在傳遞我們的參數

  • in a different order now.

    在不同的順序了。

  • And we also don't even have to pass in our arguments

    而且我們甚至不需要傳入我們的論據

  • in the same order as we have here, right?

    順序與我們這裡相同,對嗎?

  • We could pass in z and x here,

    我們可以在這裡傳入z和x。

  • and we could pass in the middle argument y later on,

    而我們可以在後來的中間論證中通過Y。

  • or we could say y and x and pass z later on,

    或者我們可以說y和x,以後再傳z。

  • we could do it in pretty much whatever order we want.

    我們幾乎可以按照我們想要的任何順序來做。

  • And obviously, in this case,

    而顯然,在這種情況下。

  • that wouldn't make much of a difference,

    這不會有太大的區別。

  • since the order doesn't matter of our arguments,

    因為順序對我們的論證並不重要。

  • but there are some cases where that would make a difference.

    但在有些情況下,這將會產生不同的效果。

  • So I'm just going to change these back

    所以我只是要把這些改回來

  • to the way they were before.

    到他們以前的方式。

  • And one last thing that I want to talk about,

    還有最後一件事,我想談一談。

  • is that we could even go a level deeper with our functions,

    是,我們甚至可以在功能上更深一層。

  • and do something like this,

    並做這樣的事情。

  • where we have a function that takes the first argument,

    其中,我們有一個接受第一個參數的函數。

  • and returns a function that takes a second argument,

    並返回一個需要第二個參數的函數。

  • and returns a function that takes the third argument,

    並返回一個接受第三個參數的函數。

  • and finally, that returns the answer.

    最後,這就返回了答案。

  • Now, obviously, talking about these things

    現在,顯然,談論這些事情

  • can sound a little confusing sometimes.

    有時聽起來有點讓人困惑。

  • So what this is going to look like is,

    是以,這將會是什麼樣子。

  • it's going to be a function,

    它將是一個函數。

  • that takes an integer as an argument,

    它需要一個整數作為參數。

  • and returns a function

    並返回一個函數

  • that takes an integer as an argument,

    它需要一個整數作為參數。

  • that returns a function, and takes an integers and argument,

    返回一個函數,並接受一個整數和參數。

  • and finally returns an integer, okay?

    並最後返回一個整數,好嗎?

  • Now that might look really confusing, and it kind of is,

    現在,這可能看起來非常令人困惑,而且確實有點。

  • but this is just a fun example to go through.

    但這只是一個有趣的例子。

  • So bear with me.

    所以請忍耐一下。

  • So what this would look like now,

    是以,這將是現在的樣子。

  • is we would define a function

    是我們將定義一個函數

  • that takes an integer as an argument,

    它需要一個整數作為參數。

  • and returns a function that takes an integers and argument,

    並返回一個接受整數和參數的函數。

  • and returns an integer.

    並返回一個整數。

  • And we could call this add five.

    而我們可以把這稱為加五。

  • And we'd say that that's equal to addPartial dot apply,

    我們會說,這等於addPartial dot apply。

  • and pass in the first argument five.

    並傳入第一個參數5。

  • And then we could say function, integer, integer,

    然後我們可以說函數、整數、整數。

  • add five and six is equal to add five dot apply,

    加5和6等於加5點應用。

  • and pass it in the second argument.

    並在第二個參數中傳遞它。

  • And finally, we could say integer

    最後,我們可以說整數

  • sum equals add five and six dot apply,

    和等於加五和六點適用。

  • and pass in the last remaining argument, which is seven.

    並傳入最後一個剩餘的參數,也就是7。

  • And if we print out the sum here,

    而如果我們在這裡打印出總和。

  • we see that that gives us the exact same answer,

    我們看到,這給了我們完全相同的答案。

  • except obviously this is much more confusing.

    但顯然這更讓人困惑。

  • Now this specific case,

    現在這個具體案例。

  • where we pass in our arguments one at a time,

    在這裡,我們一個一個地傳入我們的參數。

  • is something called currying.

    是一種叫做咖喱的東西。

  • And currying is just a special case

    而咖喱只是一種特殊情況

  • for partial application, again,

    為部分應用,再次。

  • where each of the arguments has passed in one at a time,

    其中每個參數都是一個一個地傳進去的。

  • instead of in groups like we saw before.

    而不是像我們以前看到的那樣成群結隊。

  • (bright music)

    (明亮的音樂)

(bright music)

(明亮的音樂)

字幕與單字
由 AI 自動生成

單字即點即查 點擊單字可以查詢單字解釋